22301102 | aa5adbc | 2025-05-18 17:51:55 +0800 | [diff] [blame^] | 1 | package com.pt.service; |
| 2 | |
| 3 | import com.pt.entity.Post; |
| 4 | import com.pt.repository.PostRepository; |
| 5 | import org.springframework.beans.factory.annotation.Autowired; |
| 6 | import org.springframework.stereotype.Service; |
| 7 | |
| 8 | import java.time.LocalDate; |
| 9 | import java.util.List; |
| 10 | |
| 11 | @Service |
| 12 | public class PostService { |
| 13 | |
| 14 | @Autowired |
| 15 | private PostRepository postRepository; |
| 16 | |
| 17 | public void createPost(String title, String content, String author) { |
| 18 | System.out.println("Post created with title: " + title); |
| 19 | postRepository.save(new Post(title, content, author)); |
| 20 | } |
| 21 | |
| 22 | public Post findPostByTitle(String title) { |
| 23 | return postRepository.findByTitle(title); |
| 24 | } |
| 25 | |
| 26 | public List<Post> listByAuthor(String author) { |
| 27 | return postRepository.listByAuthor(author); |
| 28 | } |
| 29 | |
| 30 | public List<Post> listByTitle(String title) { |
| 31 | return postRepository.listByTitle(title); |
| 32 | } |
| 33 | |
| 34 | public List<Post> listByDate(String date) { |
| 35 | LocalDate nDate = LocalDate.parse(date); |
| 36 | return postRepository.listByDate(nDate); |
| 37 | } |
| 38 | |
| 39 | public List<Post> listAll() { |
| 40 | return postRepository.findAll(); |
| 41 | } |
| 42 | |
| 43 | public void deletePost(Post p) { |
| 44 | postRepository.delete(p); |
| 45 | } |
| 46 | |
| 47 | public Post findPostById(int id) { |
| 48 | return postRepository.findById(id).orElse(null); |
| 49 | } |
| 50 | } |