blob: f8eb13548807a0345d53c6afb3e2c88c76317031 [file] [log] [blame]
223011385e9c35a2025-06-04 15:52:45 +08001package com.example.myproject.controller;
2
3import com.example.myproject.entity.Comments;
4import com.example.myproject.service.CommentService;
5import org.springframework.beans.factory.annotation.Autowired;
6import org.springframework.web.bind.annotation.*;
7
8import java.util.List;
9import java.util.Map;
10
11@RestController
12@RequestMapping("/echo/forum/posts")
13public 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}