refactor(消息模块): 重构消息服务及控制器功能
重构消息模块,包括以下主要变更: 1. 将MessageRepository从CrudRepository扩展改为JpaRepository 2. 新增消息查询方法,支持按文章ID、父消息ID和昵称查询 3. 完善消息服务层逻辑,增加日志记录和错误处理 4. 扩展消息控制器API,新增获取根消息、回复消息等端点 5. 添加消息数据初始化组件和检查器 6. 优化全局异常处理,增加请求路径日志 同时调整文章模块: 1. 移除按作者查询文章功能 2. 统一分类查询参数命名 3. 优化文章服务层代码结构 配置变更: 1. 添加缓存相关依赖 2. 调整数据库连接配置 3. 暂时禁用Hibernate二级缓存
This commit is contained in:
@@ -32,6 +32,7 @@ public class ArticleController {
|
||||
@GetMapping("/{id}")
|
||||
public ResponseMessage<Article> getArticle(@PathVariable Integer id) {
|
||||
return articleService.getArticleById(id);
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -82,24 +83,15 @@ public class ArticleController {
|
||||
return articleService.deleteArticle(id);
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据作者ID获取其所有文章
|
||||
* @param authorId 作者ID
|
||||
* @return 返回包含文章列表的ResponseMessage对象
|
||||
*/
|
||||
@GetMapping("/author/{authorId}")
|
||||
public ResponseMessage<List<Article>> getArticlesByAuthor(@PathVariable Integer authorId) {
|
||||
return articleService.getArticlesByAuthor(authorId);
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据分类ID获取该分类下的所有文章
|
||||
* @param categoryId 分类ID
|
||||
* @param typeid 分类ID
|
||||
* @return 返回包含文章列表的ResponseMessage对象
|
||||
*/
|
||||
@GetMapping("/category/{categoryId}")
|
||||
public ResponseMessage<List<Article>> getArticlesByCategory(@PathVariable Integer categoryId) {
|
||||
return articleService.getArticlesByCategory(categoryId);
|
||||
public ResponseMessage<List<Article>> getArticlesByCategory(@PathVariable Integer typeid) {
|
||||
return articleService.getArticlesByCategory(typeid);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -4,32 +4,102 @@ import com.qf.myafterprojecy.pojo.Message;
|
||||
import com.qf.myafterprojecy.pojo.ResponseMessage;
|
||||
import com.qf.myafterprojecy.pojo.dto.MessageDto;
|
||||
import com.qf.myafterprojecy.service.IMessageService;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/api/messages")
|
||||
public class MessageController {
|
||||
|
||||
private static final Logger logger = LoggerFactory.getLogger(MessageController.class);
|
||||
|
||||
@Autowired
|
||||
private IMessageService messageService;
|
||||
|
||||
/**
|
||||
* 获取所有消息
|
||||
*/
|
||||
@GetMapping
|
||||
public ResponseMessage<Iterable<Message>> getAllMessages() {
|
||||
logger.info("接收获取所有消息的请求");
|
||||
return messageService.getAllMessages();
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据ID获取消息
|
||||
*/
|
||||
@GetMapping("/{id}")
|
||||
public ResponseMessage<Message> getMessage(@PathVariable Integer id) {
|
||||
logger.info("接收根据ID获取消息的请求: {}", id);
|
||||
return messageService.getMessageById(id);
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建新消息
|
||||
*/
|
||||
@PostMapping
|
||||
public ResponseMessage<Message> createMessage(@RequestBody MessageDto message) {
|
||||
logger.info("接收创建消息的请求: {}", message != null ? message.getNickname() : "null");
|
||||
return messageService.saveMessage(message);
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据ID删除消息
|
||||
*/
|
||||
@DeleteMapping("/{id}")
|
||||
public ResponseMessage<Message> deleteMessage(@PathVariable Integer id) {
|
||||
logger.info("接收删除消息的请求: {}", id);
|
||||
return messageService.deleteMessage(id);
|
||||
}
|
||||
|
||||
// 新增API端点
|
||||
|
||||
/**
|
||||
* 根据文章ID获取消息列表
|
||||
*/
|
||||
@GetMapping("/article/{articleId}")
|
||||
public ResponseMessage<List<Message>> getMessagesByArticleId(@PathVariable Integer articleId) {
|
||||
logger.info("接收根据文章ID获取消息的请求: {}", articleId);
|
||||
return messageService.getMessagesByArticleId(articleId);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取所有根消息(非回复的消息)
|
||||
*/
|
||||
@GetMapping("/root")
|
||||
public ResponseMessage<List<Message>> getRootMessages() {
|
||||
logger.info("接收获取所有根消息的请求");
|
||||
return messageService.getRootMessages();
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据父消息ID获取回复列表
|
||||
*/
|
||||
@GetMapping("/{parentId}/replies")
|
||||
public ResponseMessage<List<Message>> getRepliesByParentId(@PathVariable Integer parentId) {
|
||||
logger.info("接收根据父消息ID获取回复的请求: {}", parentId);
|
||||
return messageService.getRepliesByParentId(parentId);
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据昵称搜索消息
|
||||
*/
|
||||
@GetMapping("/search")
|
||||
public ResponseMessage<List<Message>> searchMessagesByNickname(@RequestParam String nickname) {
|
||||
logger.info("接收根据昵称搜索消息的请求: {}", nickname);
|
||||
return messageService.searchMessagesByNickname(nickname);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取指定文章的评论数量
|
||||
*/
|
||||
@GetMapping("/count/article/{articleId}")
|
||||
public ResponseMessage<Long> getMessageCountByArticleId(@PathVariable Integer articleId) {
|
||||
logger.info("接收获取文章评论数量的请求: {}", articleId);
|
||||
return messageService.getMessageCountByArticleId(articleId);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user