修改部分接口,方便前后端链接
Change-Id: I698294efbd4c21e53f7895dafd3302f5f29eee72
diff --git a/src/main/java/com/pt/controller/CommentController.java b/src/main/java/com/pt/controller/CommentController.java
index 00e091d..623d94a 100644
--- a/src/main/java/com/pt/controller/CommentController.java
+++ b/src/main/java/com/pt/controller/CommentController.java
@@ -1,13 +1,19 @@
package com.pt.controller;
+import com.pt.Item.CommentInfo;
import com.pt.constant.Constants;
+import com.pt.entity.Post;
+import com.pt.entity.User;
import com.pt.service.CommentService;
+import com.pt.service.PostService;
+import com.pt.service.UserService;
import com.pt.utils.JWTUtils;
import com.pt.entity.Comment;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
+import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@@ -20,6 +26,12 @@
@Autowired
private CommentService commentService;
+ @Autowired
+ private PostService postService;
+
+ @Autowired
+ private UserService userService;
+
@PostMapping("/add")
public ResponseEntity<?> addComment(
@RequestHeader("token") String token,
@@ -28,6 +40,7 @@
String content = request.get("content");
String username = request.get("username");
int postId = Integer.parseInt(request.get("postId"));
+ Integer reviewer = request.get("reviewer") != null ? Integer.parseInt(request.get("reviewer")) : null;
Map<String, Object> ans = new HashMap<>();
@@ -36,7 +49,7 @@
return ResponseEntity.badRequest().body(ans);
}
- commentService.addComment(content, username, postId);
+ commentService.addComment(content, username, postId, reviewer);
ans.put("message", "Comment added successfully");
return ResponseEntity.ok(ans);
}
@@ -44,10 +57,9 @@
@DeleteMapping("/delete")
public ResponseEntity<?> deleteComment(
@RequestHeader("token") String token,
- @RequestBody Map<String, String> request
+ @RequestParam("username") String username,
+ @RequestParam("commentId") int commentId
) {
- String username = request.get("username");
- int commentId = Integer.parseInt(request.get("commentId"));
Map<String, Object> ans = new HashMap<>();
@@ -74,10 +86,37 @@
return ResponseEntity.badRequest().body(ans);
}
+ Post post = postService.findPostById(postId);
List<Comment> comments = commentService.getCommentsByPostId(postId);
+ List<CommentInfo> commentInfos = new ArrayList<>();
+ for(Comment comment : comments) {
+ CommentInfo commentInfo = new CommentInfo();
+ commentInfo.setCommentId(comment.getCommentId());
+ commentInfo.setContent(comment.getContent());
+ commentInfo.setWriter(comment.getWriter());
+ commentInfo.setPublishDate(comment.getPublishDate());
+
+ if(comment.getReviewer() != null) {
+ Comment r = commentService.getCommentById(comment.getReviewer());
+ if(r != null) {
+ commentInfo.setReviewer(r.getWriter());
+ commentInfo.setReviewerId(comment.getReviewer());
+ } else {
+ commentInfo.setReviewer("Unknown Reviewer");
+ commentInfo.setReviewerId(0);
+ }
+ } else {
+ commentInfo.setReviewer("");
+ commentInfo.setReviewerId(0);
+ }
+
+ commentInfos.add(commentInfo);
+ }
+
ans.put("message", "Comments retrieved successfully");
ans.put("data", Map.of(
- "comments", comments
+ "content", post.getContent(),
+ "comments", commentInfos
));
return ResponseEntity.ok(ans);
}