修改部分接口,方便前后端链接
Change-Id: I698294efbd4c21e53f7895dafd3302f5f29eee72
diff --git a/src/main/java/com/pt/controller/AdminController.java b/src/main/java/com/pt/controller/AdminController.java
index 8af5b56..71159e5 100644
--- a/src/main/java/com/pt/controller/AdminController.java
+++ b/src/main/java/com/pt/controller/AdminController.java
@@ -29,7 +29,11 @@
Admin admin = adminService.findByUsernameAndPassword(username, password);
if (admin != null) {
ans.put("message", "Login successful");
- ans.put("data", JWTUtils.generateToken(username, Constants.UserRole.ADMIN, Constants.DEFAULT_EXPIRE_TIME));
+ ans.put("data", Map.of(
+ "token", JWTUtils.generateToken(username, Constants.UserRole.ADMIN, Constants.DEFAULT_EXPIRE_TIME),
+ "uid", admin.getId(),
+ "username", admin.getUsername()
+ ));
return ResponseEntity.ok().body(ans);
} else {
ans.put("message", "Invalid username or password");
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);
}
diff --git a/src/main/java/com/pt/controller/PostController.java b/src/main/java/com/pt/controller/PostController.java
index 3723dbd..c61bd3e 100644
--- a/src/main/java/com/pt/controller/PostController.java
+++ b/src/main/java/com/pt/controller/PostController.java
@@ -44,12 +44,6 @@
return ResponseEntity.badRequest().body(ans);
}
- Post existingPost = postService.findPostByTitle(title);
- if (existingPost != null) {
- ans.put("result", "Post with this title already exists");
- return ResponseEntity.badRequest().body(ans);
- }
-
postService.createPost(title, content, author);
ans.put("message", "Post created successfully");
return ResponseEntity.ok(ans);
@@ -91,9 +85,15 @@
if(date != null){
posts.removeIf(post -> !post.getPublishDate().toString().equals(date));
}
+
+ posts.sort(
+ // 按发布日期降序排序
+ (p1, p2) -> p2.getPublishDate().compareTo(p1.getPublishDate())
+ );
+
ans.put("message", "Post retrieved successfully");
ans.put("data", Map.of(
- "post", posts
+ "posts", posts
));
return ResponseEntity.ok(ans);
}
@@ -108,11 +108,9 @@
@DeleteMapping("/delete")
public ResponseEntity<?> deletePost(
@RequestHeader("token") String token,
- @RequestBody Map<String, String> request
+ @RequestParam("username") String username,
+ @RequestParam("pid") int pid
) {
- String username = request.get("username");
- int pid = Integer.parseInt(request.get("pid"));
-
Map<String, Object> ans = new HashMap<>();
if(!JWTUtils.checkToken(token, username, Constants.UserRole.ADMIN)){
diff --git a/src/main/java/com/pt/controller/UserController.java b/src/main/java/com/pt/controller/UserController.java
index 7222f36..3bb05c5 100644
--- a/src/main/java/com/pt/controller/UserController.java
+++ b/src/main/java/com/pt/controller/UserController.java
@@ -77,7 +77,14 @@
if (user != null) {
String token = JWTUtils.generateToken(username, Constants.UserRole.USER, Constants.DEFAULT_EXPIRE_TIME);
ans.put("message", "Login successful");
- ans.put("data", token);
+ ans.put("data", Map.of(
+ "token", token,
+ "uid", user.getUid(),
+ "username", user.getUsername(),
+ "email", user.getEmail(),
+ "level", user.getLevel(),
+ "points", user.getPoints()
+ ));
return ResponseEntity.ok().body(ans);
} else {
ans.put("message", "Invalid username or password");
@@ -160,9 +167,8 @@
@DeleteMapping("/delete")
public ResponseEntity<?> deleteUser(@RequestHeader("token") String token,
- @RequestBody Map<String, String> request) {
- String username = request.get("username");
- String targetUsername = request.get("targetUsername");
+ @RequestParam("username") String username,
+ @RequestParam("targetUsername") String targetUsername) {
Map<String, Object> ans = new HashMap<>();
if(!JWTUtils.checkToken(token, username, Constants.UserRole.ADMIN)) {
@@ -183,8 +189,7 @@
@GetMapping("/list")
public ResponseEntity<?> listUsers(@RequestHeader("token") String token,
- @RequestBody Map<String, String> request) {
- String username = request.get("username");
+ @RequestParam("username") String username) {
if(!JWTUtils.checkToken(token, username, Constants.UserRole.ADMIN)) {
return ResponseEntity.badRequest().body("Invalid token");
}
@@ -203,8 +208,10 @@
@RequestParam("username") String username) {
Map<String, Object> ans = new HashMap<>();
+ System.out.println("Enter user info ");
if(!JWTUtils.checkToken(token, username, Constants.UserRole.USER)) {
ans.put("message", "Invalid token");
+ System.out.println("Invalid token");
return ResponseEntity.badRequest().body(ans);
}
@@ -212,9 +219,11 @@
if (user != null) {
ans.put("message", "User info retrieved successfully");
ans.put("data", user);
+ System.out.println("User info retrieved successfully");
return ResponseEntity.ok(ans);
} else {
ans.put("message", "User not found");
+ System.out.println("User not found");
return ResponseEntity.badRequest().body(ans);
}
}