blob: d861715b7dcacf82ba123234e6475f258a572d8b [file] [log] [blame]
夜雨声烦5c9d1312025-04-23 17:46:19 +08001package com.example.g8backend.controller;
2
夜雨声烦af4d3972025-06-08 17:52:16 +08003import com.example.g8backend.dto.ApiResponse;
4import com.example.g8backend.dto.CommentDTO;
夜雨声烦5c9d1312025-04-23 17:46:19 +08005import com.example.g8backend.entity.Comment;
6import com.example.g8backend.service.ICommentService;
7import org.springframework.beans.factory.annotation.Autowired;
夜雨声烦af4d3972025-06-08 17:52:16 +08008import org.springframework.http.ResponseEntity;
夜雨声烦5c9d1312025-04-23 17:46:19 +08009import org.springframework.web.bind.annotation.*;
10
11import java.util.List;
12
13@RestController
14@RequestMapping("/api/comments")
15public class CommentController {
16
17 @Autowired
18 private ICommentService commentService;
19
夜雨声烦af4d3972025-06-08 17:52:16 +080020
夜雨声烦5c9d1312025-04-23 17:46:19 +080021 @PostMapping
夜雨声烦af4d3972025-06-08 17:52:16 +080022 public ResponseEntity<ApiResponse<CommentDTO>> createComment(@RequestBody CommentDTO commentDTO) {
23 commentService.createComment(commentDTO);
24 ApiResponse<CommentDTO> response = ApiResponse.message("评论创建成功");
25 return ResponseEntity.ok(response);
夜雨声烦5c9d1312025-04-23 17:46:19 +080026 }
27
夜雨声烦5c9d1312025-04-23 17:46:19 +080028 @GetMapping("/post/{postId}")
wuchimedes83f0e362025-06-08 19:19:17 +080029 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;
夜雨声烦5c9d1312025-04-23 17:46:19 +080036 }
37
38 // 删除评论
39 @DeleteMapping("/{commentId}")
wuchimedes83f0e362025-06-08 19:19:17 +080040 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 }
夜雨声烦5c9d1312025-04-23 17:46:19 +080047 }
48}