blob: 00e091dbc1c45e2414b313532a8cece5c7100c50 [file] [log] [blame]
2230110243e9dfe2025-05-17 16:27:12 +08001package com.pt.controller;
2
3import com.pt.constant.Constants;
4import com.pt.service.CommentService;
5import com.pt.utils.JWTUtils;
6import com.pt.entity.Comment;
7import org.springframework.beans.factory.annotation.Autowired;
8import org.springframework.http.ResponseEntity;
9import org.springframework.web.bind.annotation.*;
10
11import java.util.HashMap;
12import java.util.List;
13import java.util.Map;
14
15@RestController
16@RequestMapping("/api/comment")
17@CrossOrigin(origins = "*")
18public class CommentController {
19
20 @Autowired
21 private CommentService commentService;
22
23 @PostMapping("/add")
24 public ResponseEntity<?> addComment(
25 @RequestHeader("token") String token,
22301102f69709e2025-06-08 14:10:02 +080026 @RequestBody Map<String, String> request
2230110243e9dfe2025-05-17 16:27:12 +080027 ) {
22301102f69709e2025-06-08 14:10:02 +080028 String content = request.get("content");
29 String username = request.get("username");
30 int postId = Integer.parseInt(request.get("postId"));
31
2230110243e9dfe2025-05-17 16:27:12 +080032 Map<String, Object> ans = new HashMap<>();
33
34 if (!JWTUtils.checkToken(token, username, Constants.UserRole.USER)) {
22301102f69709e2025-06-08 14:10:02 +080035 ans.put("message", "Invalid token");
2230110243e9dfe2025-05-17 16:27:12 +080036 return ResponseEntity.badRequest().body(ans);
37 }
38
39 commentService.addComment(content, username, postId);
22301102f69709e2025-06-08 14:10:02 +080040 ans.put("message", "Comment added successfully");
2230110243e9dfe2025-05-17 16:27:12 +080041 return ResponseEntity.ok(ans);
42 }
43
44 @DeleteMapping("/delete")
45 public ResponseEntity<?> deleteComment(
46 @RequestHeader("token") String token,
22301102f69709e2025-06-08 14:10:02 +080047 @RequestBody Map<String, String> request
2230110243e9dfe2025-05-17 16:27:12 +080048 ) {
22301102f69709e2025-06-08 14:10:02 +080049 String username = request.get("username");
50 int commentId = Integer.parseInt(request.get("commentId"));
51
2230110243e9dfe2025-05-17 16:27:12 +080052 Map<String, Object> ans = new HashMap<>();
53
54 if (!JWTUtils.checkToken(token, username, Constants.UserRole.ADMIN)) {
22301102f69709e2025-06-08 14:10:02 +080055 ans.put("message", "Invalid token");
2230110243e9dfe2025-05-17 16:27:12 +080056 return ResponseEntity.badRequest().body(ans);
57 }
58
59 commentService.deleteComment(commentId);
22301102f69709e2025-06-08 14:10:02 +080060 ans.put("message", "Comment deleted successfully");
2230110243e9dfe2025-05-17 16:27:12 +080061 return ResponseEntity.ok(ans);
62 }
63
64 @GetMapping("/get")
65 public ResponseEntity<?> getComments(
66 @RequestHeader("token") String token,
22301102f69709e2025-06-08 14:10:02 +080067 @RequestParam("username") String username,
68 @RequestParam("postId") int postId
2230110243e9dfe2025-05-17 16:27:12 +080069 ) {
70 Map<String, Object> ans = new HashMap<>();
71
72 if(!JWTUtils.checkToken(token, username, Constants.UserRole.USER)) {
22301102f69709e2025-06-08 14:10:02 +080073 ans.put("message", "Invalid token");
2230110243e9dfe2025-05-17 16:27:12 +080074 return ResponseEntity.badRequest().body(ans);
75 }
76
77 List<Comment> comments = commentService.getCommentsByPostId(postId);
22301102f69709e2025-06-08 14:10:02 +080078 ans.put("message", "Comments retrieved successfully");
22301102aadb0ac2025-06-05 18:02:21 +080079 ans.put("data", Map.of(
80 "comments", comments
81 ));
2230110243e9dfe2025-05-17 16:27:12 +080082 return ResponseEntity.ok(ans);
83 }
84}