Преглед изворни кода

feature: add simple comments apis

ggbocoder пре 2 година
родитељ
комит
f7cb5f539d

+ 7 - 0
db/comments.sql

@@ -0,0 +1,7 @@
+CREATE TABLE comments (
+                          comment_id BIGINT PRIMARY KEY AUTO_INCREMENT,
+                          post_id BIGINT NOT NULL,
+                          user_id BIGINT NOT NULL,
+                          content TEXT,
+                          created_at TIMESTAMP NOT NULL
+);

+ 32 - 0
src/main/java/cn/seecoder/fdroidrepository/Controller/CommentController.java

@@ -0,0 +1,32 @@
+package cn.seecoder.fdroidrepository.Controller;
+
+import cn.seecoder.fdroidrepository.DataObject.Comment;
+import cn.seecoder.fdroidrepository.Service.CommentService;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.http.HttpStatus;
+import org.springframework.http.ResponseEntity;
+import org.springframework.web.bind.annotation.*;
+
+import java.util.List;
+
+// CommentController.java
+@RestController
+@RequestMapping("/api/comments")
+public class CommentController {
+    @Autowired
+    private CommentService commentService;
+
+    @GetMapping("/post/{postId}")
+    public ResponseEntity<List<Comment>> getCommentsByPostId(@PathVariable Long postId) {
+        List<Comment> comments = commentService.getCommentsByPostId(postId);
+        return new ResponseEntity<>(comments, HttpStatus.OK);
+    }
+
+    @PostMapping("/post/{postId}")
+    public ResponseEntity<String> saveComment(@PathVariable Long postId, @RequestBody Comment comment) {
+        comment.setPostId(postId);
+        commentService.saveComment(comment);
+        return new ResponseEntity<>("Comment saved successfully", HttpStatus.OK);
+    }
+}
+

+ 21 - 0
src/main/java/cn/seecoder/fdroidrepository/DataObject/Comment.java

@@ -0,0 +1,21 @@
+package cn.seecoder.fdroidrepository.DataObject;
+
+import lombok.AllArgsConstructor;
+import lombok.Getter;
+import lombok.NoArgsConstructor;
+import lombok.Setter;
+
+import java.time.LocalDateTime;
+
+@Getter
+@Setter
+@NoArgsConstructor
+@AllArgsConstructor
+public class Comment {
+    private Long commentId;
+    private Long postId;
+    private Long userId;
+    private String content;
+    private LocalDateTime createdAt;
+}
+

+ 34 - 0
src/main/java/cn/seecoder/fdroidrepository/Mapper/CommentMapper.java

@@ -0,0 +1,34 @@
+package cn.seecoder.fdroidrepository.Mapper;
+
+import cn.seecoder.fdroidrepository.DataObject.Comment;
+import cn.seecoder.fdroidrepository.Service.CommentService;
+import org.apache.ibatis.annotations.*;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.http.HttpStatus;
+import org.springframework.http.ResponseEntity;
+import org.springframework.web.bind.annotation.*;
+
+import java.util.List;
+
+// CommentController.java
+// CommentMapper.java
+@Mapper
+public interface CommentMapper {
+
+    @Select("SELECT * FROM comments WHERE post_id = #{postId}")
+    @Results({
+        @Result(property = "commentId", column = "comment_id"),
+        @Result(property = "postId", column = "post_id"),
+        @Result(property = "userId", column = "user_id"),
+        @Result(property = "content", column = "content"),
+        @Result(property = "createdAt", column = "created_at")
+        // 添加其他属性映射关系
+    })
+    List<Comment> findByPostId(Long postId);
+
+    @Insert("INSERT INTO comments (post_id, user_id, content, created_at) " +
+        "VALUES (#{postId}, #{userId}, #{content}, #{createdAt})")
+    void save(Comment comment);
+}
+
+

+ 13 - 0
src/main/java/cn/seecoder/fdroidrepository/Service/CommentService.java

@@ -0,0 +1,13 @@
+package cn.seecoder.fdroidrepository.Service;
+
+import cn.seecoder.fdroidrepository.DataObject.Comment;
+
+import java.util.List;
+
+// CommentService.java
+public interface CommentService {
+    List<Comment> getCommentsByPostId(Long postId);
+
+    void saveComment(Comment comment);
+}
+

+ 28 - 0
src/main/java/cn/seecoder/fdroidrepository/Service/ServiceImpl/CommentServiceImpl.java

@@ -0,0 +1,28 @@
+package cn.seecoder.fdroidrepository.Service.ServiceImpl;
+
+import cn.seecoder.fdroidrepository.DataObject.Comment;
+import cn.seecoder.fdroidrepository.Mapper.CommentMapper;
+import cn.seecoder.fdroidrepository.Service.CommentService;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Service;
+
+import java.time.LocalDateTime;
+import java.util.List;
+
+// CommentServiceImpl.java
+@Service
+public class CommentServiceImpl implements CommentService {
+    @Autowired
+    private CommentMapper commentMapper;
+
+    @Override
+    public List<Comment> getCommentsByPostId(Long postId) {
+        return commentMapper.findByPostId(postId);
+    }
+
+    @Override
+    public void saveComment(Comment comment) {
+        comment.setCreatedAt(LocalDateTime.now());
+        commentMapper.save(comment);
+    }
+}