完成评论的基本的功能
Change-Id: I798126b0411b49b67111420247cb9884f1b41411
diff --git a/src/main/java/com/pt/controller/CommentController.java b/src/main/java/com/pt/controller/CommentController.java
new file mode 100644
index 0000000..a85d131
--- /dev/null
+++ b/src/main/java/com/pt/controller/CommentController.java
@@ -0,0 +1,78 @@
+package com.pt.controller;
+
+import com.pt.constant.Constants;
+import com.pt.service.CommentService;
+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.HashMap;
+import java.util.List;
+import java.util.Map;
+
+@RestController
+@RequestMapping("/api/comment")
+@CrossOrigin(origins = "*")
+public class CommentController {
+
+ @Autowired
+ private CommentService commentService;
+
+ @PostMapping("/add")
+ public ResponseEntity<?> addComment(
+ @RequestHeader("token") String token,
+ @RequestParam("content") String content,
+ @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");
+ return ResponseEntity.badRequest().body(ans);
+ }
+
+ commentService.addComment(content, username, postId);
+ ans.put("result", "Comment added successfully");
+ return ResponseEntity.ok(ans);
+ }
+
+ @DeleteMapping("/delete")
+ public ResponseEntity<?> deleteComment(
+ @RequestHeader("token") String token,
+ @RequestParam("commentId") int commentId,
+ @RequestParam("username") String username
+ ) {
+ Map<String, Object> ans = new HashMap<>();
+
+ if (!JWTUtils.checkToken(token, username, Constants.UserRole.ADMIN)) {
+ ans.put("result", "Invalid token");
+ return ResponseEntity.badRequest().body(ans);
+ }
+
+ commentService.deleteComment(commentId);
+ ans.put("result", "Comment deleted successfully");
+ return ResponseEntity.ok(ans);
+ }
+
+ @GetMapping("/get")
+ public ResponseEntity<?> getComments(
+ @RequestHeader("token") String token,
+ @RequestParam("postId") int postId,
+ @RequestParam("username") String username
+ ) {
+ Map<String, Object> ans = new HashMap<>();
+
+ if(!JWTUtils.checkToken(token, username, Constants.UserRole.USER)) {
+ ans.put("result", "Invalid token");
+ return ResponseEntity.badRequest().body(ans);
+ }
+
+ List<Comment> comments = commentService.getCommentsByPostId(postId);
+ ans.put("result", "Comments retrieved successfully");
+ ans.put("comments", comments);
+ 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 844dc65..387cc2d 100644
--- a/src/main/java/com/pt/controller/PostController.java
+++ b/src/main/java/com/pt/controller/PostController.java
@@ -61,7 +61,6 @@
* @param title 帖子标题(可选)
* @param author 帖子作者(可选)
* @param date 帖子发布日期(可选)
- * @return 帖子列表的响应
*/
@GetMapping("/list")
public ResponseEntity<?> getPost(
diff --git a/src/main/java/com/pt/entity/Comment.java b/src/main/java/com/pt/entity/Comment.java
new file mode 100644
index 0000000..b40100b
--- /dev/null
+++ b/src/main/java/com/pt/entity/Comment.java
@@ -0,0 +1,75 @@
+package com.pt.entity;
+
+import jakarta.persistence.Entity;
+import jakarta.persistence.GeneratedValue;
+import jakarta.persistence.GenerationType;
+import jakarta.persistence.Id;
+
+import java.time.LocalDateTime;
+
+@Entity
+public class Comment {
+
+ @Id
+ @GeneratedValue(strategy = GenerationType.IDENTITY)
+ private int commentId;
+
+ private String content;
+ private String writer;
+ private int parentPost;
+
+ private LocalDateTime publishDate;
+
+ public Comment() {
+ publishDate = LocalDateTime.now();
+ }
+
+ public Comment(String content, String writer, int parentPost) {
+ this.content = content;
+ this.writer = writer;
+ this.parentPost = parentPost;
+ this.publishDate = LocalDateTime.now();
+ }
+
+ public int getCommentId() {
+ return commentId;
+ }
+
+ public void setCommentId(int commentId) {
+ this.commentId = commentId;
+ }
+
+ public String getContent() {
+ return content;
+ }
+
+ public void setContent(String content) {
+ this.content = content;
+ }
+
+ public String getWriter() {
+ return writer;
+ }
+
+ public void setWriter(String writer) {
+ this.writer = writer;
+ }
+
+ public int getParentPost() {
+ return parentPost;
+ }
+
+ public void setParentPost(int parentPost) {
+ this.parentPost = parentPost;
+ }
+
+ public LocalDateTime getPublishDate() {
+ return publishDate;
+ }
+
+ public void setPublishDate(LocalDateTime publishDate) {
+ this.publishDate = publishDate;
+ }
+}
+
+
diff --git a/src/main/java/com/pt/repository/CommentRepository.java b/src/main/java/com/pt/repository/CommentRepository.java
new file mode 100644
index 0000000..3f5f88e
--- /dev/null
+++ b/src/main/java/com/pt/repository/CommentRepository.java
@@ -0,0 +1,24 @@
+package com.pt.repository;
+
+import com.pt.entity.Comment;
+import org.springframework.data.jpa.repository.JpaRepository;
+import org.springframework.data.jpa.repository.Query;
+import org.springframework.data.repository.query.Param;
+
+import java.time.LocalDate;
+import java.time.LocalDateTime;
+import java.util.List;
+
+public interface CommentRepository extends JpaRepository<Comment, Integer> {
+ Comment findByContent(String content);
+
+ Comment findByWriter(String writer);
+
+ @Query(value = "SELECT c FROM Comment c WHERE c.writer = :writer")
+ List<Comment> listByWriter(@Param("writer") String writer);
+
+ @Query(value = "SELECT c FROM Comment c WHERE c.publishDate = :date")
+ List<Comment> listByDate(@Param("date") LocalDateTime date);
+
+ List<Comment> findByParentPost(int parentPost);
+}
diff --git a/src/main/java/com/pt/service/CommentService.java b/src/main/java/com/pt/service/CommentService.java
new file mode 100644
index 0000000..3d8b7dd
--- /dev/null
+++ b/src/main/java/com/pt/service/CommentService.java
@@ -0,0 +1,40 @@
+package com.pt.service;
+
+import com.pt.entity.Comment;
+import com.pt.repository.CommentRepository;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Service;
+
+import java.util.List;
+
+@Service
+public class CommentService {
+
+ @Autowired
+ private CommentRepository commentRepository;
+
+ public CommentService(CommentRepository commentRepository) {
+ this.commentRepository = commentRepository;
+ }
+
+ public void addComment(String content, String username, int postId) {
+ Comment comment = new Comment();
+ comment.setContent(content);
+ comment.setWriter(username);
+ comment.setParentPost(postId);
+ commentRepository.save(comment);
+ }
+
+ public void deleteComment(int commentId) {
+ commentRepository.deleteById(commentId);
+ }
+
+ public List<Comment> getCommentsByPostId(int postId) {
+ List<Comment> comments = commentRepository.findByParentPost(postId);
+ comments.sort(
+ // Sort comments by publish date in descending order
+ (c1, c2) -> c2.getPublishDate().compareTo(c1.getPublishDate())
+ );
+ return comments;
+ }
+}