22301138 | 1c35910 | 2025-06-03 15:19:59 +0800 | [diff] [blame^] | 1 | package com.example.myproject.controller; |
| 2 | |
| 3 | import com.example.myproject.entity.SeedComment; |
| 4 | import com.example.myproject.service.SeedCommentService; |
| 5 | import org.springframework.beans.factory.annotation.Autowired; |
| 6 | import org.springframework.web.bind.annotation.*; |
| 7 | |
| 8 | import java.util.Map; |
| 9 | |
| 10 | @RestController |
| 11 | @RequestMapping("/echo/seeds") |
| 12 | public class SeedCommentController { |
| 13 | |
| 14 | @Autowired |
| 15 | private SeedCommentService seedCommentService; |
| 16 | |
| 17 | @PostMapping("/{seed_id}/comments") |
| 18 | public String publishComment(@PathVariable("seed_id") int seedId, |
| 19 | @RequestBody SeedComment seedComment) { |
| 20 | // 调用服务类的逻辑处理评论发布 |
| 21 | return seedCommentService.publishComment(seedId, seedComment); |
| 22 | } |
| 23 | |
| 24 | @GetMapping("/{seed_id}/getAllComments") |
| 25 | public Map<String, Object> getAllComments(@PathVariable("seed_id") int seedId) { |
| 26 | // 调用服务层获取所有评论数据 |
| 27 | return seedCommentService.getAllCommentsForSeed(seedId); |
| 28 | } |
| 29 | |
| 30 | // 点赞切换接口 |
| 31 | @PutMapping("/{comment_id}/like-toggle") |
| 32 | public Map<String, Object> toggleLike(@PathVariable("comment_id") Long commentId, |
| 33 | @RequestBody Map<String, Object> request) { |
| 34 | Long userId = Long.parseLong(request.get("user_id").toString()); |
| 35 | return seedCommentService.toggleLike(commentId, userId); |
| 36 | } |
| 37 | |
| 38 | |
| 39 | } |