22301138 | f682451 | 2025-06-04 02:03:13 +0800 | [diff] [blame] | 1 | package com.example.myproject.controller; |
| 2 | |
| 3 | import com.example.myproject.entity.Comments; |
| 4 | import com.example.myproject.service.CommentService; |
| 5 | import org.springframework.beans.factory.annotation.Autowired; |
| 6 | import org.springframework.web.bind.annotation.*; |
| 7 | |
| 8 | import java.util.List; |
| 9 | import java.util.Map; |
| 10 | |
| 11 | @RestController |
| 12 | @RequestMapping("/echo/forum/posts") |
| 13 | public class CommentController { |
| 14 | |
| 15 | @Autowired |
| 16 | private CommentService commentService; |
| 17 | |
| 18 | // 添加评论 |
| 19 | @PostMapping("/{post_id}/comments") |
| 20 | public String addComment(@PathVariable("post_id") Long postId, @RequestBody Comments comment) { |
| 21 | // 添加评论 |
| 22 | commentService.addComment(postId, comment); |
| 23 | return "Comment added successfully!"; |
| 24 | } |
| 25 | |
| 26 | //获取所有评论 |
| 27 | @GetMapping("/{post_id}/getAllComments") |
| 28 | public List<Map<String, Object>> getCommentsByPostId(@PathVariable("post_id") Long postId) { |
| 29 | // 获取评论并填充用户信息 |
| 30 | return commentService.getCommentsByPostId(postId); |
| 31 | } |
| 32 | |
| 33 | } |