feat(分类模块): 实现分类管理功能

新增分类模块相关代码,包括实体类、DTO、Repository、Service和Controller
添加分类数据初始化逻辑和日志记录
实现分类的增删改查及搜索功能
This commit is contained in:
qingfeng1121
2025-10-12 14:23:42 +08:00
parent 299c9a57ec
commit 2809837422
9 changed files with 2475 additions and 5331 deletions

View File

@@ -0,0 +1,97 @@
package com.qf.myafterprojecy.controller;
import com.qf.myafterprojecy.pojo.Category;
import com.qf.myafterprojecy.pojo.ResponseMessage;
import com.qf.myafterprojecy.pojo.dto.CategoryDto;
import com.qf.myafterprojecy.service.ICategoryService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import javax.validation.Valid;
import java.util.List;
/**
* 分类控制器类处理分类相关的HTTP请求
* 提供分类的增删改查功能
*/
@RestController
@RequestMapping("/api/categories")
@Validated
public class CategoryController {
private static final Logger log = LoggerFactory.getLogger(CategoryController.class);
@Autowired
private ICategoryService categoryService;
/**
* 根据ID获取分类信息
* @param id 分类ID
* @return 返回分类信息
*/
@GetMapping("/{id}")
public ResponseMessage<Category> getCategoryById(@PathVariable Integer id) {
log.info("接收根据ID获取分类的请求: {}", id);
return categoryService.getCategoryById(id);
}
/**
* 获取所有分类列表
* @return 返回分类列表
*/
@GetMapping
public ResponseMessage<List<Category>> getAllCategories() {
log.info("接收获取所有分类列表的请求");
return categoryService.getAllCategories();
}
/**
* 创建新分类
* @param categoryDto 分类数据传输对象
* @return 返回创建结果
*/
@PostMapping
public ResponseMessage<Category> createCategory(@Valid @RequestBody CategoryDto categoryDto) {
log.info("接收创建分类的请求: {}", categoryDto.getTypename());
return categoryService.saveCategory(categoryDto);
}
/**
* 更新分类信息
* @param id 分类ID
* @param categoryDto 分类数据传输对象
* @return 返回更新结果
*/
@PutMapping("/{id}")
public ResponseMessage<Category> updateCategory(
@PathVariable Integer id,
@Valid @RequestBody CategoryDto categoryDto) {
log.info("接收更新分类的请求: ID={}, 分类名称={}", id, categoryDto.getTypename());
return categoryService.updateCategory(id, categoryDto);
}
/**
* 删除分类
* @param id 分类ID
* @return 返回删除结果
*/
@DeleteMapping("/{id}")
public ResponseMessage<Boolean> deleteCategory(@PathVariable Integer id) {
log.info("接收删除分类的请求: {}", id);
return categoryService.deleteCategory(id);
}
/**
* 根据分类名称搜索分类
* @param typename 分类名称
* @return 返回符合条件的分类列表
*/
@GetMapping("/search")
public ResponseMessage<List<Category>> searchCategoriesByTypename(@RequestParam String typename) {
log.info("接收根据名称搜索分类的请求: {}", typename);
return categoryService.searchCategoriesByTypename(typename);
}
}