blob: 623d94ade2a25d10cc39319425c0e8d78621c4fd [file] [log] [blame]
2230110243e9dfe2025-05-17 16:27:12 +08001package com.pt.controller;
2
22301102f5670302025-06-08 14:10:02 +08003import com.pt.Item.CommentInfo;
2230110243e9dfe2025-05-17 16:27:12 +08004import com.pt.constant.Constants;
22301102f5670302025-06-08 14:10:02 +08005import com.pt.entity.Post;
6import com.pt.entity.User;
2230110243e9dfe2025-05-17 16:27:12 +08007import com.pt.service.CommentService;
22301102f5670302025-06-08 14:10:02 +08008import com.pt.service.PostService;
9import com.pt.service.UserService;
2230110243e9dfe2025-05-17 16:27:12 +080010import com.pt.utils.JWTUtils;
11import com.pt.entity.Comment;
12import org.springframework.beans.factory.annotation.Autowired;
13import org.springframework.http.ResponseEntity;
14import org.springframework.web.bind.annotation.*;
15
22301102f5670302025-06-08 14:10:02 +080016import java.util.ArrayList;
2230110243e9dfe2025-05-17 16:27:12 +080017import java.util.HashMap;
18import java.util.List;
19import java.util.Map;
20
21@RestController
22@RequestMapping("/api/comment")
23@CrossOrigin(origins = "*")
24public class CommentController {
25
26 @Autowired
27 private CommentService commentService;
28
22301102f5670302025-06-08 14:10:02 +080029 @Autowired
30 private PostService postService;
31
32 @Autowired
33 private UserService userService;
34
2230110243e9dfe2025-05-17 16:27:12 +080035 @PostMapping("/add")
36 public ResponseEntity<?> addComment(
37 @RequestHeader("token") String token,
22301102f69709e2025-06-08 14:10:02 +080038 @RequestBody Map<String, String> request
2230110243e9dfe2025-05-17 16:27:12 +080039 ) {
22301102f69709e2025-06-08 14:10:02 +080040 String content = request.get("content");
41 String username = request.get("username");
42 int postId = Integer.parseInt(request.get("postId"));
22301102f5670302025-06-08 14:10:02 +080043 Integer reviewer = request.get("reviewer") != null ? Integer.parseInt(request.get("reviewer")) : null;
22301102f69709e2025-06-08 14:10:02 +080044
2230110243e9dfe2025-05-17 16:27:12 +080045 Map<String, Object> ans = new HashMap<>();
46
47 if (!JWTUtils.checkToken(token, username, Constants.UserRole.USER)) {
22301102f69709e2025-06-08 14:10:02 +080048 ans.put("message", "Invalid token");
2230110243e9dfe2025-05-17 16:27:12 +080049 return ResponseEntity.badRequest().body(ans);
50 }
51
22301102f5670302025-06-08 14:10:02 +080052 commentService.addComment(content, username, postId, reviewer);
22301102f69709e2025-06-08 14:10:02 +080053 ans.put("message", "Comment added successfully");
2230110243e9dfe2025-05-17 16:27:12 +080054 return ResponseEntity.ok(ans);
55 }
56
57 @DeleteMapping("/delete")
58 public ResponseEntity<?> deleteComment(
59 @RequestHeader("token") String token,
22301102f5670302025-06-08 14:10:02 +080060 @RequestParam("username") String username,
61 @RequestParam("commentId") int commentId
2230110243e9dfe2025-05-17 16:27:12 +080062 ) {
22301102f69709e2025-06-08 14:10:02 +080063
2230110243e9dfe2025-05-17 16:27:12 +080064 Map<String, Object> ans = new HashMap<>();
65
66 if (!JWTUtils.checkToken(token, username, Constants.UserRole.ADMIN)) {
22301102f69709e2025-06-08 14:10:02 +080067 ans.put("message", "Invalid token");
2230110243e9dfe2025-05-17 16:27:12 +080068 return ResponseEntity.badRequest().body(ans);
69 }
70
71 commentService.deleteComment(commentId);
22301102f69709e2025-06-08 14:10:02 +080072 ans.put("message", "Comment deleted successfully");
2230110243e9dfe2025-05-17 16:27:12 +080073 return ResponseEntity.ok(ans);
74 }
75
76 @GetMapping("/get")
77 public ResponseEntity<?> getComments(
78 @RequestHeader("token") String token,
22301102f69709e2025-06-08 14:10:02 +080079 @RequestParam("username") String username,
80 @RequestParam("postId") int postId
2230110243e9dfe2025-05-17 16:27:12 +080081 ) {
82 Map<String, Object> ans = new HashMap<>();
83
84 if(!JWTUtils.checkToken(token, username, Constants.UserRole.USER)) {
22301102f69709e2025-06-08 14:10:02 +080085 ans.put("message", "Invalid token");
2230110243e9dfe2025-05-17 16:27:12 +080086 return ResponseEntity.badRequest().body(ans);
87 }
88
22301102f5670302025-06-08 14:10:02 +080089 Post post = postService.findPostById(postId);
2230110243e9dfe2025-05-17 16:27:12 +080090 List<Comment> comments = commentService.getCommentsByPostId(postId);
22301102f5670302025-06-08 14:10:02 +080091 List<CommentInfo> commentInfos = new ArrayList<>();
92 for(Comment comment : comments) {
93 CommentInfo commentInfo = new CommentInfo();
94 commentInfo.setCommentId(comment.getCommentId());
95 commentInfo.setContent(comment.getContent());
96 commentInfo.setWriter(comment.getWriter());
97 commentInfo.setPublishDate(comment.getPublishDate());
98
99 if(comment.getReviewer() != null) {
100 Comment r = commentService.getCommentById(comment.getReviewer());
101 if(r != null) {
102 commentInfo.setReviewer(r.getWriter());
103 commentInfo.setReviewerId(comment.getReviewer());
104 } else {
105 commentInfo.setReviewer("Unknown Reviewer");
106 commentInfo.setReviewerId(0);
107 }
108 } else {
109 commentInfo.setReviewer("");
110 commentInfo.setReviewerId(0);
111 }
112
113 commentInfos.add(commentInfo);
114 }
115
22301102f69709e2025-06-08 14:10:02 +0800116 ans.put("message", "Comments retrieved successfully");
22301102aadb0ac2025-06-05 18:02:21 +0800117 ans.put("data", Map.of(
22301102f5670302025-06-08 14:10:02 +0800118 "content", post.getContent(),
119 "comments", commentInfos
22301102aadb0ac2025-06-05 18:02:21 +0800120 ));
2230110243e9dfe2025-05-17 16:27:12 +0800121 return ResponseEntity.ok(ans);
122 }
123}