修改部分接口,方便前后端链接
Change-Id: Icce71f5085084a4185cb11fe3313d618dfb01177
diff --git a/src/main/java/com/pt/controller/CommentController.java b/src/main/java/com/pt/controller/CommentController.java
index 17dabb3..00e091d 100644
--- a/src/main/java/com/pt/controller/CommentController.java
+++ b/src/main/java/com/pt/controller/CommentController.java
@@ -23,55 +23,59 @@
@PostMapping("/add")
public ResponseEntity<?> addComment(
@RequestHeader("token") String token,
- @RequestParam("content") String content,
- @RequestParam("username") String username,
- @RequestParam("postId") int postId
+ @RequestBody Map<String, String> request
) {
+ String content = request.get("content");
+ String username = request.get("username");
+ int postId = Integer.parseInt(request.get("postId"));
+
Map<String, Object> ans = new HashMap<>();
if (!JWTUtils.checkToken(token, username, Constants.UserRole.USER)) {
- ans.put("result", "Invalid token");
+ ans.put("message", "Invalid token");
return ResponseEntity.badRequest().body(ans);
}
commentService.addComment(content, username, postId);
- ans.put("result", "Comment added successfully");
+ ans.put("message", "Comment added successfully");
return ResponseEntity.ok(ans);
}
@DeleteMapping("/delete")
public ResponseEntity<?> deleteComment(
@RequestHeader("token") String token,
- @RequestParam("commentId") int commentId,
- @RequestParam("username") String username
+ @RequestBody Map<String, String> request
) {
+ String username = request.get("username");
+ int commentId = Integer.parseInt(request.get("commentId"));
+
Map<String, Object> ans = new HashMap<>();
if (!JWTUtils.checkToken(token, username, Constants.UserRole.ADMIN)) {
- ans.put("result", "Invalid token");
+ ans.put("message", "Invalid token");
return ResponseEntity.badRequest().body(ans);
}
commentService.deleteComment(commentId);
- ans.put("result", "Comment deleted successfully");
+ ans.put("message", "Comment deleted successfully");
return ResponseEntity.ok(ans);
}
@GetMapping("/get")
public ResponseEntity<?> getComments(
@RequestHeader("token") String token,
- @RequestParam("postId") int postId,
- @RequestParam("username") String username
+ @RequestParam("username") String username,
+ @RequestParam("postId") int postId
) {
Map<String, Object> ans = new HashMap<>();
if(!JWTUtils.checkToken(token, username, Constants.UserRole.USER)) {
- ans.put("result", "Invalid token");
+ ans.put("message", "Invalid token");
return ResponseEntity.badRequest().body(ans);
}
List<Comment> comments = commentService.getCommentsByPostId(postId);
- ans.put("result", "Comments retrieved successfully");
+ ans.put("message", "Comments retrieved successfully");
ans.put("data", Map.of(
"comments", comments
));