Seamher | 0140612 | 2025-06-08 19:44:36 +0800 | [diff] [blame^] | 1 | package com.g9.g9backend.controller; |
| 2 | |
| 3 | import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; |
| 4 | import com.baomidou.mybatisplus.extension.plugins.pagination.Page; |
| 5 | import com.g9.g9backend.pojo.Comment; |
| 6 | import com.g9.g9backend.pojo.DTO.GetCommentDTO; |
| 7 | import com.g9.g9backend.service.CommentService; |
| 8 | import org.jetbrains.annotations.NotNull; |
| 9 | import org.slf4j.Logger; |
| 10 | import org.slf4j.LoggerFactory; |
| 11 | import org.springframework.http.ResponseEntity; |
| 12 | import org.springframework.web.bind.annotation.*; |
| 13 | |
| 14 | import java.util.List; |
| 15 | import java.util.Objects; |
| 16 | import java.util.function.Function; |
| 17 | |
| 18 | @RestController |
| 19 | @RequestMapping("/comment") |
| 20 | public class CommentController { |
| 21 | |
| 22 | private final CommentService commentService; |
| 23 | |
| 24 | private final Logger logger = LoggerFactory.getLogger(CommentController.class); |
| 25 | |
| 26 | public CommentController(CommentService commentService) { |
| 27 | this.commentService = commentService; |
| 28 | } |
| 29 | |
| 30 | @PostMapping |
| 31 | public ResponseEntity<String> postComment(@RequestBody Comment comment) { |
| 32 | System.out.println(comment.toString()); |
| 33 | commentService.save(comment); |
| 34 | |
| 35 | logger.info("评论已发布"); |
| 36 | |
| 37 | return ResponseEntity.ok(""); |
| 38 | } |
| 39 | |
| 40 | @DeleteMapping |
| 41 | public ResponseEntity<String> deleteComment(@RequestParam Integer commentId) { |
| 42 | commentService.removeById(commentId); |
| 43 | |
| 44 | return ResponseEntity.noContent().build(); |
| 45 | } |
| 46 | |
| 47 | @GetMapping |
| 48 | public ResponseEntity<GetCommentDTO> getComment(@RequestParam Integer id, |
| 49 | @RequestParam Integer pageNumber, |
| 50 | @RequestParam Integer rows, |
| 51 | @RequestParam String type) { |
| 52 | Page<Comment> commentPage = new Page<>(pageNumber, rows); |
| 53 | LambdaQueryWrapper<Comment> rewardQuery = new LambdaQueryWrapper<Comment>() |
| 54 | .orderByDesc(Comment::getCreateAt); |
| 55 | |
| 56 | |
| 57 | if (Objects.equals(type, "资源")) { |
| 58 | |
| 59 | rewardQuery.eq(Comment::getResourceId, id); |
| 60 | } |
| 61 | |
| 62 | if (Objects.equals(type, "帖子")) { |
| 63 | |
| 64 | rewardQuery.eq(Comment::getThreadId, id); |
| 65 | } |
| 66 | |
| 67 | if (Objects.equals(type, "悬赏")) { |
| 68 | |
| 69 | rewardQuery.eq(Comment::getRewardId, id); |
| 70 | } |
| 71 | |
| 72 | Page<Comment> result = commentService.page(commentPage, rewardQuery); |
| 73 | |
| 74 | GetCommentDTO getCommentDTO = wrapCommentPage(result, item -> { |
| 75 | GetCommentDTO.Comment comment = new GetCommentDTO.Comment(); |
| 76 | comment.setCommentId(item.getCommentId()); |
| 77 | comment.setUserId(item.getUserId()); |
| 78 | comment.setReplyId(item.getReplyId() != null ? item.getReplyId() : 0); |
| 79 | comment.setContent(item.getContent()); |
| 80 | comment.setCreateAt(item.getCreateAt()); |
| 81 | return comment; |
| 82 | }); |
| 83 | |
| 84 | return ResponseEntity.ok(getCommentDTO); |
| 85 | } |
| 86 | |
| 87 | @NotNull |
| 88 | private <T> GetCommentDTO wrapCommentPage(Page<T> page, Function<T, GetCommentDTO.Comment> mapper) { |
| 89 | List<GetCommentDTO.Comment> records = page.getRecords().stream().map(mapper).toList(); |
| 90 | |
| 91 | return new GetCommentDTO(records, (int) page.getTotal(), (int) page.getPages(), (int) page.getCurrent(), (int) page.getSize()); |
| 92 | } |
| 93 | |
| 94 | } |