blob: 1ac817ef2a0b3d146ddb14e8ad6ac98aa2915ef5 [file] [log] [blame]
2230110243e9dfe2025-05-17 16:27:12 +08001package com.pt.service;
2
3import com.pt.entity.Comment;
4import com.pt.repository.CommentRepository;
5import org.springframework.beans.factory.annotation.Autowired;
6import org.springframework.stereotype.Service;
7
8import java.util.List;
9
10@Service
11public class CommentService {
12
13 @Autowired
14 private CommentRepository commentRepository;
15
16 public CommentService(CommentRepository commentRepository) {
17 this.commentRepository = commentRepository;
18 }
19
22301102f5670302025-06-08 14:10:02 +080020 public void addComment(String content, String username, int postId, Integer reviewer) {
21 System.out.println("Adding comment: " + content + " by " + username + " on post ID: " + postId + " with reviewer ID: " + reviewer);
2230110243e9dfe2025-05-17 16:27:12 +080022 Comment comment = new Comment();
23 comment.setContent(content);
24 comment.setWriter(username);
25 comment.setParentPost(postId);
22301102f5670302025-06-08 14:10:02 +080026 if(reviewer != null) {
27 System.out.println("Setting reviewer ID: " + reviewer);
28 comment.setReviewer(reviewer);
29 }
2230110243e9dfe2025-05-17 16:27:12 +080030 commentRepository.save(comment);
31 }
32
33 public void deleteComment(int commentId) {
34 commentRepository.deleteById(commentId);
35 }
36
37 public List<Comment> getCommentsByPostId(int postId) {
38 List<Comment> comments = commentRepository.findByParentPost(postId);
39 comments.sort(
40 // Sort comments by publish date in descending order
41 (c1, c2) -> c2.getPublishDate().compareTo(c1.getPublishDate())
42 );
43 return comments;
44 }
22301102f5670302025-06-08 14:10:02 +080045
46 public Comment getCommentById(int commentId) {
47 return commentRepository.findById(commentId).orElse(null);
48 }
2230110243e9dfe2025-05-17 16:27:12 +080049}