| package com.example.g8backend.controller; |
| |
| import com.example.g8backend.dto.ApiResponse; |
| import com.example.g8backend.dto.CommentDTO; |
| import com.example.g8backend.entity.Comment; |
| import com.example.g8backend.service.ICommentService; |
| import org.springframework.beans.factory.annotation.Autowired; |
| import org.springframework.http.ResponseEntity; |
| import org.springframework.web.bind.annotation.*; |
| |
| import java.util.List; |
| |
| @RestController |
| @RequestMapping("/api/comments") |
| public class CommentController { |
| |
| @Autowired |
| private ICommentService commentService; |
| |
| |
| @PostMapping |
| public ResponseEntity<ApiResponse<CommentDTO>> createComment(@RequestBody CommentDTO commentDTO) { |
| commentService.createComment(commentDTO); |
| ApiResponse<CommentDTO> response = ApiResponse.message("评论创建成功"); |
| return ResponseEntity.ok(response); |
| } |
| |
| @GetMapping("/post/{postId}") |
| public ApiResponse<List<Comment>> getCommentsByPostId(@PathVariable Long postId) { |
| ApiResponse<List<Comment>> response = new ApiResponse<>(); |
| response.setCode(200); |
| response.setMessage("获取评论成功"); |
| |
| response.setData(commentService.getCommentsByPostId(postId)); |
| return response; |
| } |
| |
| // 删除评论 |
| @DeleteMapping("/{commentId}") |
| public ApiResponse<String> deleteComment(@PathVariable Long commentId) { |
| try { |
| commentService.deleteComment(commentId); |
| return ApiResponse.message("评论删除成功"); |
| } catch (Exception e) { |
| return ApiResponse.error(500, "评论删除失败" + e.getMessage()); |
| } |
| } |
| } |