package com.emotion.controller; import com.baomidou.mybatisplus.core.metadata.IPage; import com.emotion.common.PageResult; import com.emotion.common.Result; import com.emotion.dto.request.PageRequest; import com.emotion.dto.response.BaseResponse; import com.emotion.entity.Comment; import com.emotion.service.CommentService; import org.springframework.beans.BeanUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.validation.annotation.Validated; import org.springframework.web.bind.annotation.*; import javax.validation.constraints.NotBlank; import java.time.format.DateTimeFormatter; import java.util.List; import java.util.stream.Collectors; /** * 评论控制器 * * @author emotion-museum * @date 2025-07-23 */ @RestController @RequestMapping("/comment") public class CommentController { @Autowired private CommentService commentService; private static final DateTimeFormatter DATE_TIME_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); /** * 分页查询评论 */ @GetMapping("/page") public Result> getPage(@Validated PageRequest request) { IPage page = commentService.getPage(request); List responses = page.getRecords().stream() .map(this::convertToResponse) .collect(Collectors.toList()); PageResult pageResult = new PageResult<>(); pageResult.setCurrent(page.getCurrent()); pageResult.setSize(page.getSize()); pageResult.setTotal(page.getTotal()); pageResult.setPages(page.getPages()); pageResult.setRecords(responses); return Result.success(pageResult); } /** * 根据帖子ID分页查询评论 */ @GetMapping("/post/{postId}/page") public Result> getPageByPostId(@PathVariable String postId, @Validated PageRequest request) { IPage page = commentService.getPageByPostId(request, postId); List responses = page.getRecords().stream() .map(this::convertToResponse) .collect(Collectors.toList()); PageResult pageResult = new PageResult<>(); pageResult.setCurrent(page.getCurrent()); pageResult.setSize(page.getSize()); pageResult.setTotal(page.getTotal()); pageResult.setPages(page.getPages()); pageResult.setRecords(responses); return Result.success(pageResult); } /** * 根据用户ID分页查询评论 */ @GetMapping("/user/{userId}/page") public Result> getPageByUserId(@PathVariable String userId, @Validated PageRequest request) { IPage page = commentService.getPageByUserId(request, userId); List responses = page.getRecords().stream() .map(this::convertToResponse) .collect(Collectors.toList()); PageResult pageResult = new PageResult<>(); pageResult.setCurrent(page.getCurrent()); pageResult.setSize(page.getSize()); pageResult.setTotal(page.getTotal()); pageResult.setPages(page.getPages()); pageResult.setRecords(responses); return Result.success(pageResult); } /** * 根据ID获取评论 */ @GetMapping("/{id}") public Result getById(@PathVariable String id) { Comment comment = commentService.getById(id); if (comment == null) { return Result.notFound("评论不存在"); } return Result.success(convertToResponse(comment)); } /** * 创建评论 */ @PostMapping public Result create(@RequestBody @Validated CommentCreateRequest request) { Comment comment = commentService.createComment( request.getPostId(), request.getUserId(), request.getContent(), request.getReplyToId() ); return Result.success(convertToResponse(comment)); } /** * 更新评论 */ @PutMapping("/{id}") public Result update(@PathVariable String id, @RequestBody Comment comment) { comment.setId(id); boolean updated = commentService.updateById(comment); if (!updated) { return Result.error("更新失败"); } Comment updatedComment = commentService.getById(id); return Result.success(convertToResponse(updatedComment)); } /** * 删除评论 */ @DeleteMapping("/{id}") public Result delete(@PathVariable String id) { boolean deleted = commentService.removeById(id); if (!deleted) { return Result.error("删除失败"); } return Result.success(); } /** * 根据帖子ID查询顶级评论 */ @GetMapping("/post/{postId}/top-level") public Result> getTopLevelCommentsByPostId(@PathVariable String postId) { List comments = commentService.getTopLevelCommentsByPostId(postId); List responses = comments.stream() .map(this::convertToResponse) .collect(Collectors.toList()); return Result.success(responses); } /** * 根据评论ID查询回复 */ @GetMapping("/{commentId}/replies") public Result> getRepliesByCommentId(@PathVariable String commentId) { List replies = commentService.getRepliesByCommentId(commentId); List responses = replies.stream() .map(this::convertToResponse) .collect(Collectors.toList()); return Result.success(responses); } /** * 统计帖子的评论数量 */ @GetMapping("/post/{postId}/count") public Result countByPostId(@PathVariable String postId) { Long count = commentService.countByPostId(postId); return Result.success(count); } /** * 点赞评论 */ @PutMapping("/{id}/like") public Result likeComment(@PathVariable String id) { boolean updated = commentService.updateLikes(id, 1); if (!updated) { return Result.error("点赞失败"); } return Result.success(); } /** * 取消点赞评论 */ @PutMapping("/{id}/unlike") public Result unlikeComment(@PathVariable String id) { boolean updated = commentService.updateLikes(id, -1); if (!updated) { return Result.error("取消点赞失败"); } return Result.success(); } /** * 转换为响应对象 */ private CommentResponse convertToResponse(Comment comment) { CommentResponse response = new CommentResponse(); BeanUtils.copyProperties(comment, response); response.setId(comment.getId()); if (comment.getCreateTime() != null) { response.setCreateTime(comment.getCreateTime().format(DATE_TIME_FORMATTER)); } if (comment.getUpdateTime() != null) { response.setUpdateTime(comment.getUpdateTime().format(DATE_TIME_FORMATTER)); } return response; } /** * 评论创建请求 */ @lombok.Data public static class CommentCreateRequest { @NotBlank(message = "帖子ID不能为空") private String postId; @NotBlank(message = "用户ID不能为空") private String userId; @NotBlank(message = "评论内容不能为空") private String content; private String replyToId; } /** * 评论响应类 */ @lombok.Data @lombok.EqualsAndHashCode(callSuper = true) public static class CommentResponse extends BaseResponse { private String postId; private String userId; private String content; private String replyToId; private Integer likes; } }