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