blob: fec8e68866ea2a8d6081605c8b3839fcff483998 [file] [log] [blame]
Seamher01406122025-06-08 19:44:36 +08001package com.g9.g9backend.controller;
2
3import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
4import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
5import com.g9.g9backend.pojo.Comment;
6import com.g9.g9backend.pojo.DTO.GetCommentDTO;
7import com.g9.g9backend.service.CommentService;
8import org.jetbrains.annotations.NotNull;
9import org.slf4j.Logger;
10import org.slf4j.LoggerFactory;
11import org.springframework.http.ResponseEntity;
12import org.springframework.web.bind.annotation.*;
13
14import java.util.List;
15import java.util.Objects;
16import java.util.function.Function;
17
18@RestController
19@RequestMapping("/comment")
20public 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}