blob: ae80c3018f8465fe66569b5339f5a354a2b110da [file] [log] [blame]
ym92355ec83a2025-06-03 17:15:13 +08001package com.pt5.pthouduan.controller;
2
3import com.pt5.pthouduan.entity.Comment;
4import com.pt5.pthouduan.service.CommentService;
5import org.springframework.beans.factory.annotation.Autowired;
6import org.springframework.web.bind.annotation.*;
7import org.springframework.stereotype.Controller;
8
9import java.util.List;
10
11/**
12 * <p>
13 * 评论前端控制器
14 * </p>
15 *
16 * 功能:增、删、改、查(按帖子ID)
17 *
18 * @author ym
19 * @since 2025-04-14
20 */
21@CrossOrigin(origins = "http://localhost:5173")
22@Controller
23@RequestMapping("/comment")
24public class CommentController {
25
26 @Autowired
27 private CommentService commentService;
28
29 // 创建评论
30 @PostMapping("/create")
31 @ResponseBody
32 public Comment createComment(@RequestBody Comment comment) {
33 System.out.println("Received comment: " + comment); // 输出接收到的评论数据
34 return commentService.createComment(comment);
35}
36
37 // 删除评论(根据commentid)
38 @DeleteMapping("/delete/{commentid}")
39 @ResponseBody
40 public boolean deleteComment(@PathVariable Integer commentid) {
41 return commentService.deleteComment(commentid);
42 }
43
44 // 更新评论
45 @PutMapping("/update")
46 @ResponseBody
47 public boolean updateComment(@RequestBody Comment comment) {
48 return commentService.updateComment(comment);
49 }
50
51 // 获取某个帖子的所有评论
52 @GetMapping("/post/{postid}")
53 @ResponseBody
54 public List<Comment> getCommentsByPostId(@PathVariable Integer postid) {
55 return commentService.getCommentsByPostId(postid);
56 }
57
58 // 点赞评论
59 @PostMapping("/like/{commentid}")
60 @ResponseBody
61 public boolean likeComment(@PathVariable Integer commentid) {
62 return commentService.likeComment(commentid);
63 }
64
65 // 取消点赞评论
66 @PostMapping("/unlike/{commentid}")
67 @ResponseBody
68 public boolean unlikeComment(@PathVariable Integer commentid) {
69 return commentService.unlikeComment(commentid);
70 }
71}