feat: 完善后端架构 - 标准化Controller层和Service层
✨ 新功能: - 创建了完整的Service层架构,包含所有业务实体的Service接口和实现类 - 新增8个标准化的Controller类,支持完整的CRUD操作 - 实现了统一的Request/Response模式和分页查询功能 - 创建了认证服务(AuthService)和令牌服务(TokenService) - 添加了Redis配置和认证拦截器 🏗️ 架构优化: - 移除Controller层所有try-catch块,使用全局异常处理机制 - 创建了专门的异常类(AuthException, TokenException, CaptchaException) - 统一了API返回格式,完善了Result类的方法 - 实现了标准的分页查询和参数校验 📦 新增文件: - 8个Controller类: Achievement, Comment, CommunityPost, Conversation, CozeApiCall, EmotionAnalysis, Reward, UserStats - 12个Service接口和对应的实现类 - 标准化的DTO类(Request/Response) - 异常处理类和拦截器 - 测试用例 🔧 重构优化: - 重写了AuthController,移除所有业务逻辑到Service层 - 优化了MessageController,使用标准的Request/Response格式 - 更新了全局异常处理器,支持多种异常类型 - 完善了WebConfig配置,添加认证拦截器 📊 代码统计: - 新增文件: 60+个 - 新增代码行数: 8000+行 - 重构代码行数: 1000+行 - 移除过时接口: 4个
This commit is contained in:
@@ -0,0 +1,249 @@
|
||||
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<PageResult<CommentResponse>> getPage(@Validated PageRequest request) {
|
||||
IPage<Comment> page = commentService.getPage(request);
|
||||
List<CommentResponse> responses = page.getRecords().stream()
|
||||
.map(this::convertToResponse)
|
||||
.collect(Collectors.toList());
|
||||
|
||||
PageResult<CommentResponse> 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<PageResult<CommentResponse>> getPageByPostId(@PathVariable String postId, @Validated PageRequest request) {
|
||||
IPage<Comment> page = commentService.getPageByPostId(request, postId);
|
||||
List<CommentResponse> responses = page.getRecords().stream()
|
||||
.map(this::convertToResponse)
|
||||
.collect(Collectors.toList());
|
||||
|
||||
PageResult<CommentResponse> 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<PageResult<CommentResponse>> getPageByUserId(@PathVariable String userId, @Validated PageRequest request) {
|
||||
IPage<Comment> page = commentService.getPageByUserId(request, userId);
|
||||
List<CommentResponse> responses = page.getRecords().stream()
|
||||
.map(this::convertToResponse)
|
||||
.collect(Collectors.toList());
|
||||
|
||||
PageResult<CommentResponse> 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<CommentResponse> getById(@PathVariable String id) {
|
||||
Comment comment = commentService.getById(id);
|
||||
if (comment == null) {
|
||||
return Result.notFound("评论不存在");
|
||||
}
|
||||
return Result.success(convertToResponse(comment));
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建评论
|
||||
*/
|
||||
@PostMapping
|
||||
public Result<CommentResponse> 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<CommentResponse> 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<Void> 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<List<CommentResponse>> getTopLevelCommentsByPostId(@PathVariable String postId) {
|
||||
List<Comment> comments = commentService.getTopLevelCommentsByPostId(postId);
|
||||
List<CommentResponse> responses = comments.stream()
|
||||
.map(this::convertToResponse)
|
||||
.collect(Collectors.toList());
|
||||
return Result.success(responses);
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据评论ID查询回复
|
||||
*/
|
||||
@GetMapping("/{commentId}/replies")
|
||||
public Result<List<CommentResponse>> getRepliesByCommentId(@PathVariable String commentId) {
|
||||
List<Comment> replies = commentService.getRepliesByCommentId(commentId);
|
||||
List<CommentResponse> responses = replies.stream()
|
||||
.map(this::convertToResponse)
|
||||
.collect(Collectors.toList());
|
||||
return Result.success(responses);
|
||||
}
|
||||
|
||||
/**
|
||||
* 统计帖子的评论数量
|
||||
*/
|
||||
@GetMapping("/post/{postId}/count")
|
||||
public Result<Long> countByPostId(@PathVariable String postId) {
|
||||
Long count = commentService.countByPostId(postId);
|
||||
return Result.success(count);
|
||||
}
|
||||
|
||||
/**
|
||||
* 点赞评论
|
||||
*/
|
||||
@PutMapping("/{id}/like")
|
||||
public Result<Void> likeComment(@PathVariable String id) {
|
||||
boolean updated = commentService.updateLikes(id, 1);
|
||||
if (!updated) {
|
||||
return Result.error("点赞失败");
|
||||
}
|
||||
return Result.success();
|
||||
}
|
||||
|
||||
/**
|
||||
* 取消点赞评论
|
||||
*/
|
||||
@PutMapping("/{id}/unlike")
|
||||
public Result<Void> 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;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user