22301102 | 43e9dfe | 2025-05-17 16:27:12 +0800 | [diff] [blame] | 1 | package com.pt.controller; |
| 2 | |
| 3 | import com.pt.constant.Constants; |
| 4 | import com.pt.service.CommentService; |
| 5 | import com.pt.utils.JWTUtils; |
| 6 | import com.pt.entity.Comment; |
| 7 | import org.springframework.beans.factory.annotation.Autowired; |
| 8 | import org.springframework.http.ResponseEntity; |
| 9 | import org.springframework.web.bind.annotation.*; |
| 10 | |
| 11 | import java.util.HashMap; |
| 12 | import java.util.List; |
| 13 | import java.util.Map; |
| 14 | |
| 15 | @RestController |
| 16 | @RequestMapping("/api/comment") |
| 17 | @CrossOrigin(origins = "*") |
| 18 | public class CommentController { |
| 19 | |
| 20 | @Autowired |
| 21 | private CommentService commentService; |
| 22 | |
| 23 | @PostMapping("/add") |
| 24 | public ResponseEntity<?> addComment( |
| 25 | @RequestHeader("token") String token, |
22301102 | f69709e | 2025-06-08 14:10:02 +0800 | [diff] [blame] | 26 | @RequestBody Map<String, String> request |
22301102 | 43e9dfe | 2025-05-17 16:27:12 +0800 | [diff] [blame] | 27 | ) { |
22301102 | f69709e | 2025-06-08 14:10:02 +0800 | [diff] [blame] | 28 | String content = request.get("content"); |
| 29 | String username = request.get("username"); |
| 30 | int postId = Integer.parseInt(request.get("postId")); |
| 31 | |
22301102 | 43e9dfe | 2025-05-17 16:27:12 +0800 | [diff] [blame] | 32 | Map<String, Object> ans = new HashMap<>(); |
| 33 | |
| 34 | if (!JWTUtils.checkToken(token, username, Constants.UserRole.USER)) { |
22301102 | f69709e | 2025-06-08 14:10:02 +0800 | [diff] [blame] | 35 | ans.put("message", "Invalid token"); |
22301102 | 43e9dfe | 2025-05-17 16:27:12 +0800 | [diff] [blame] | 36 | return ResponseEntity.badRequest().body(ans); |
| 37 | } |
| 38 | |
| 39 | commentService.addComment(content, username, postId); |
22301102 | f69709e | 2025-06-08 14:10:02 +0800 | [diff] [blame] | 40 | ans.put("message", "Comment added successfully"); |
22301102 | 43e9dfe | 2025-05-17 16:27:12 +0800 | [diff] [blame] | 41 | return ResponseEntity.ok(ans); |
| 42 | } |
| 43 | |
| 44 | @DeleteMapping("/delete") |
| 45 | public ResponseEntity<?> deleteComment( |
| 46 | @RequestHeader("token") String token, |
22301102 | f69709e | 2025-06-08 14:10:02 +0800 | [diff] [blame] | 47 | @RequestBody Map<String, String> request |
22301102 | 43e9dfe | 2025-05-17 16:27:12 +0800 | [diff] [blame] | 48 | ) { |
22301102 | f69709e | 2025-06-08 14:10:02 +0800 | [diff] [blame] | 49 | String username = request.get("username"); |
| 50 | int commentId = Integer.parseInt(request.get("commentId")); |
| 51 | |
22301102 | 43e9dfe | 2025-05-17 16:27:12 +0800 | [diff] [blame] | 52 | Map<String, Object> ans = new HashMap<>(); |
| 53 | |
| 54 | if (!JWTUtils.checkToken(token, username, Constants.UserRole.ADMIN)) { |
22301102 | f69709e | 2025-06-08 14:10:02 +0800 | [diff] [blame] | 55 | ans.put("message", "Invalid token"); |
22301102 | 43e9dfe | 2025-05-17 16:27:12 +0800 | [diff] [blame] | 56 | return ResponseEntity.badRequest().body(ans); |
| 57 | } |
| 58 | |
| 59 | commentService.deleteComment(commentId); |
22301102 | f69709e | 2025-06-08 14:10:02 +0800 | [diff] [blame] | 60 | ans.put("message", "Comment deleted successfully"); |
22301102 | 43e9dfe | 2025-05-17 16:27:12 +0800 | [diff] [blame] | 61 | return ResponseEntity.ok(ans); |
| 62 | } |
| 63 | |
| 64 | @GetMapping("/get") |
| 65 | public ResponseEntity<?> getComments( |
| 66 | @RequestHeader("token") String token, |
22301102 | f69709e | 2025-06-08 14:10:02 +0800 | [diff] [blame] | 67 | @RequestParam("username") String username, |
| 68 | @RequestParam("postId") int postId |
22301102 | 43e9dfe | 2025-05-17 16:27:12 +0800 | [diff] [blame] | 69 | ) { |
| 70 | Map<String, Object> ans = new HashMap<>(); |
| 71 | |
| 72 | if(!JWTUtils.checkToken(token, username, Constants.UserRole.USER)) { |
22301102 | f69709e | 2025-06-08 14:10:02 +0800 | [diff] [blame] | 73 | ans.put("message", "Invalid token"); |
22301102 | 43e9dfe | 2025-05-17 16:27:12 +0800 | [diff] [blame] | 74 | return ResponseEntity.badRequest().body(ans); |
| 75 | } |
| 76 | |
| 77 | List<Comment> comments = commentService.getCommentsByPostId(postId); |
22301102 | f69709e | 2025-06-08 14:10:02 +0800 | [diff] [blame] | 78 | ans.put("message", "Comments retrieved successfully"); |
22301102 | aadb0ac | 2025-06-05 18:02:21 +0800 | [diff] [blame] | 79 | ans.put("data", Map.of( |
| 80 | "comments", comments |
| 81 | )); |
22301102 | 43e9dfe | 2025-05-17 16:27:12 +0800 | [diff] [blame] | 82 | return ResponseEntity.ok(ans); |
| 83 | } |
| 84 | } |