夜雨声烦 | 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 | |
夜雨声烦 | 5c9d131 | 2025-04-23 17:46:19 +0800 | [diff] [blame] | 28 | @GetMapping("/post/{postId}") |
wuchimedes | 83f0e36 | 2025-06-08 19:19:17 +0800 | [diff] [blame] | 29 | public ApiResponse<List<Comment>> getCommentsByPostId(@PathVariable Long postId) { |
| 30 | ApiResponse<List<Comment>> response = new ApiResponse<>(); |
| 31 | response.setCode(200); |
| 32 | response.setMessage("获取评论成功"); |
| 33 | |
| 34 | response.setData(commentService.getCommentsByPostId(postId)); |
| 35 | return response; |
夜雨声烦 | 5c9d131 | 2025-04-23 17:46:19 +0800 | [diff] [blame] | 36 | } |
| 37 | |
| 38 | // 删除评论 |
| 39 | @DeleteMapping("/{commentId}") |
wuchimedes | 83f0e36 | 2025-06-08 19:19:17 +0800 | [diff] [blame] | 40 | public ApiResponse<String> deleteComment(@PathVariable Long commentId) { |
| 41 | try { |
| 42 | commentService.deleteComment(commentId); |
| 43 | return ApiResponse.message("评论删除成功"); |
| 44 | } catch (Exception e) { |
| 45 | return ApiResponse.error(500, "评论删除失败" + e.getMessage()); |
| 46 | } |
夜雨声烦 | 5c9d131 | 2025-04-23 17:46:19 +0800 | [diff] [blame] | 47 | } |
| 48 | } |