You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
dsl3/controller/CategoryController.java

75 lines
3.7 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

package com.aurora.controller;
import com.aurora.annotation.OptLog;
import com.aurora.model.dto.CategoryAdminDTO;
import com.aurora.model.dto.CategoryDTO;
import com.aurora.model.dto.CategoryOptionDTO;
import com.aurora.service.CategoryService;
import com.aurora.model.vo.CategoryVO;
import com.aurora.model.vo.ConditionVO;
import com.aurora.model.dto.PageResultDTO;
import com.aurora.model.vo.ResultVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import javax.validation.Valid;
import java.util.List;
import static com.aurora.constant.OptTypeConstant.*;
@Api(tags = "分类模块")
@RestController// 标识本类为RESTful控制器组合了@Controller和@ResponseBody所有方法返回值直接序列化为JSON响应
public class CategoryController {
@Autowired
private CategoryService categoryService; //用于处理分类相关的业务逻辑
@ApiOperation("获取所有分类")// Swagger注解描述API功能获取所有分类通常用于前台展示
@GetMapping("/categories/all")
// 返回值ResultVO<List<CategoryDTO>>是统一的响应包装类(用来规范接口返回的格式,比如包含响应状态、提示信息、数据等)
//List<CategoryDTO>是分类数据的列表CategoryDTO是分类相关的数据传输对象用来封装分类的信息
public ResultVO<List<CategoryDTO>> listCategories() {
// 调用Service层方法获取所有分类列表并用ResultVO包装成成功响应
return ResultVO.ok(categoryService.listCategories());
}
@ApiOperation(value = "查看后台分类列表")// 查看后台分类列表(通常包含分页和查询条件)
@GetMapping("/admin/categories")
// 返回分页结果包含CategoryAdminDTO列表可能比前台DTO包含更多管理字段
public ResultVO<PageResultDTO<CategoryAdminDTO>> listCategoriesAdmin(ConditionVO conditionVO) {
//ConditionVO是条件封装类用来接收前端传递的查询条件比如分类名称、分页信息等
return ResultVO.ok(categoryService.listCategoriesAdmin(conditionVO));
}
@ApiOperation(value = "搜索文章分类")//后台管理的搜索文章分类的接口
@GetMapping("/admin/categories/search")
// 返回CategoryOptionDTO列表通常用于下拉选择框等场景
//CategoryOptionDTO是适配“选项选择”场景的分类传输对象比如下拉选择框里的分类选项通常只封装分类ID和分类名称等
//List<CategoryOptionDTO>是符合搜索条件的分类选项列表
public ResultVO<List<CategoryOptionDTO>> listCategoriesAdminBySearch(ConditionVO conditionVO) {
return ResultVO.ok(categoryService.listCategoriesBySearch(conditionVO));
}
@OptLog(optType = DELETE)
@ApiOperation(value = "删除分类")
@DeleteMapping("/admin/categories")
// @RequestBody接收前端以JSON格式传递的请求体数据转换成要删除的分类ID列表ResultVO<?>表示无具体返回数据,仅返回操作成功的状态提示
public ResultVO<?> deleteCategories(@RequestBody List<Integer> categoryIds) {
categoryService.deleteCategories(categoryIds);
return ResultVO.ok();
}
@OptLog(optType = SAVE_OR_UPDATE)
@ApiOperation(value = "添加或修改分类")
@PostMapping("/admin/categories")
public ResultVO<?> saveOrUpdateCategory(@Valid @RequestBody CategoryVO categoryVO) {
// Service层会通过CategorVO中是否包含分类ID等信息判断是执行添加还是修改操作
categoryService.saveOrUpdateCategory(categoryVO);
return ResultVO.ok();
}
}