夜雨声烦 | 5c9d131 | 2025-04-23 17:46:19 +0800 | [diff] [blame] | 1 | package com.example.g8backend.controller; |
| 2 | |
| 3 | import com.example.g8backend.entity.Comment; |
| 4 | import com.example.g8backend.service.ICommentService; |
| 5 | import org.springframework.beans.factory.annotation.Autowired; |
| 6 | import org.springframework.web.bind.annotation.*; |
| 7 | |
| 8 | import java.util.List; |
| 9 | |
| 10 | @RestController |
| 11 | @RequestMapping("/api/comments") |
| 12 | public class CommentController { |
| 13 | |
| 14 | @Autowired |
| 15 | private ICommentService commentService; |
| 16 | |
| 17 | // 创建评论 |
| 18 | @PostMapping |
| 19 | public void createComment(@RequestParam Long postId, @RequestParam Long userId, |
| 20 | @RequestParam String content, @RequestParam(required = false) Long parentCommentId) { |
| 21 | commentService.createComment(postId, userId, content, parentCommentId); |
| 22 | } |
| 23 | |
| 24 | // 获取某帖子下的所有评论,包括顶级评论及其子评论 |
| 25 | @GetMapping("/post/{postId}") |
| 26 | public List<Comment> getCommentsByPostId(@PathVariable Long postId) { |
| 27 | return commentService.getCommentsByPostId(postId); |
| 28 | } |
| 29 | |
| 30 | // 删除评论 |
| 31 | @DeleteMapping("/{commentId}") |
| 32 | public void deleteComment(@PathVariable Long commentId) { |
| 33 | commentService.deleteComment(commentId); |
| 34 | } |
| 35 | } |