ym923 | 55ec83a | 2025-06-03 17:15:13 +0800 | [diff] [blame] | 1 | package com.pt5.pthouduan.controller; |
| 2 | |
| 3 | import com.pt5.pthouduan.entity.Comment; |
| 4 | import com.pt5.pthouduan.service.CommentService; |
| 5 | import org.springframework.beans.factory.annotation.Autowired; |
| 6 | import org.springframework.web.bind.annotation.*; |
| 7 | import org.springframework.stereotype.Controller; |
| 8 | |
| 9 | import 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") |
| 24 | public 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 | } |