| package com.example.myproject.controller; |
| |
| import com.example.myproject.entity.Comments; |
| import com.example.myproject.service.CommentService; |
| import org.springframework.beans.factory.annotation.Autowired; |
| import org.springframework.web.bind.annotation.*; |
| |
| import java.util.List; |
| import java.util.Map; |
| |
| @RestController |
| @RequestMapping("/echo/forum/posts") |
| public class CommentController { |
| |
| @Autowired |
| private CommentService commentService; |
| |
| // 添加评论 |
| @PostMapping("/{post_id}/comments") |
| public String addComment(@PathVariable("post_id") Long postId, @RequestBody Comments comment) { |
| // 添加评论 |
| commentService.addComment(postId, comment); |
| return "Comment added successfully!"; |
| } |
| |
| //获取所有评论 |
| @GetMapping("/{post_id}/getAllComments") |
| public List<Map<String, Object>> getCommentsByPostId(@PathVariable("post_id") Long postId) { |
| // 获取评论并填充用户信息 |
| return commentService.getCommentsByPostId(postId); |
| } |
| |
| } |