category.controller.ts 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. import {
  2. Body,
  3. Controller,
  4. Delete,
  5. Get,
  6. Param,
  7. Post,
  8. Put,
  9. Query,
  10. } from '@nestjs/common';
  11. import { CategoryService } from '../service/category.service';
  12. import { CreateCategoryRequest } from '../dto/create-category.request';
  13. import { ApiBearerAuth, ApiOkResponse, getSchemaPath } from '@nestjs/swagger';
  14. import { CategoryVo } from '../vo/category.vo';
  15. import { CategoryMapper } from '../mapper/category.mapper';
  16. import { SearchCategoryFilter } from '../dto/search-category.filter';
  17. import { PageResultMapper } from '../../core/mapper/page-result.mapper';
  18. import { PageResult } from '../../core/vo/page-result';
  19. import { UpdateCategoryRequest } from '../dto/update-category.request';
  20. @Controller('categories')
  21. export class CategoryController {
  22. constructor(private readonly categoryService: CategoryService) {}
  23. @Post()
  24. @ApiOkResponse({
  25. description: 'Category',
  26. type: CategoryVo,
  27. })
  28. @ApiBearerAuth()
  29. async create(
  30. @Body() createCategoryRequest: CreateCategoryRequest,
  31. ): Promise<CategoryVo> {
  32. return CategoryMapper.toVo(
  33. await this.categoryService.create(createCategoryRequest),
  34. );
  35. }
  36. @Get()
  37. @ApiOkResponse({
  38. description: '分类分页列表',
  39. schema: {
  40. allOf: [
  41. { $ref: getSchemaPath(PageResult) }, // 引用 PageResult 模型
  42. {
  43. properties: {
  44. data: {
  45. type: 'array',
  46. items: { $ref: getSchemaPath(CategoryVo) }, // 泛型内容具体化
  47. },
  48. },
  49. },
  50. ],
  51. },
  52. })
  53. @ApiBearerAuth()
  54. async search(@Query() searchCategoryFilter: SearchCategoryFilter) {
  55. const [data, total] =
  56. await this.categoryService.search(searchCategoryFilter);
  57. return PageResultMapper.toPageResult<CategoryVo>(
  58. CategoryMapper.toVos(data),
  59. searchCategoryFilter.getPage(),
  60. searchCategoryFilter.getSize(),
  61. total,
  62. );
  63. }
  64. @ApiBearerAuth()
  65. @Get('/list')
  66. @ApiOkResponse({
  67. schema: {
  68. type: 'array',
  69. items: { $ref: getSchemaPath(CategoryVo) },
  70. },
  71. })
  72. async list() {
  73. return CategoryMapper.toVos(await this.categoryService.list());
  74. }
  75. @Get(':id')
  76. @ApiBearerAuth()
  77. @ApiOkResponse({
  78. type: CategoryVo,
  79. })
  80. async get(@Param('id') id: string) {
  81. return CategoryMapper.toVo(await this.categoryService.get(id));
  82. }
  83. @Put(':id')
  84. @ApiBearerAuth()
  85. @ApiOkResponse({
  86. type: CategoryVo,
  87. })
  88. async update(
  89. @Param('id') id: string,
  90. @Body() updateCategoryRequest: UpdateCategoryRequest,
  91. ) {
  92. return CategoryMapper.toVo(
  93. await this.categoryService.update(id, updateCategoryRequest),
  94. );
  95. }
  96. @Delete(':id')
  97. @ApiBearerAuth()
  98. async delete(@Param('id') id: string) {
  99. await this.categoryService.delete(id);
  100. }
  101. }