| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108 |
- import {
- Body,
- Controller,
- Delete,
- Get,
- Param,
- Post,
- Put,
- Query,
- } from '@nestjs/common';
- import { CategoryService } from '../service/category.service';
- import { CreateCategoryRequest } from '../dto/create-category.request';
- import { ApiBearerAuth, ApiOkResponse, getSchemaPath } from '@nestjs/swagger';
- import { CategoryVo } from '../vo/category.vo';
- import { CategoryMapper } from '../mapper/category.mapper';
- import { SearchCategoryFilter } from '../dto/search-category.filter';
- import { PageResultMapper } from '../../core/mapper/page-result.mapper';
- import { PageResult } from '../../core/vo/page-result';
- import { UpdateCategoryRequest } from '../dto/update-category.request';
- @Controller('categories')
- export class CategoryController {
- constructor(private readonly categoryService: CategoryService) {}
- @Post()
- @ApiOkResponse({
- description: 'Category',
- type: CategoryVo,
- })
- @ApiBearerAuth()
- async create(
- @Body() createCategoryRequest: CreateCategoryRequest,
- ): Promise<CategoryVo> {
- return CategoryMapper.toVo(
- await this.categoryService.create(createCategoryRequest),
- );
- }
- @Get()
- @ApiOkResponse({
- description: '分类分页列表',
- schema: {
- allOf: [
- { $ref: getSchemaPath(PageResult) }, // 引用 PageResult 模型
- {
- properties: {
- data: {
- type: 'array',
- items: { $ref: getSchemaPath(CategoryVo) }, // 泛型内容具体化
- },
- },
- },
- ],
- },
- })
- @ApiBearerAuth()
- async search(@Query() searchCategoryFilter: SearchCategoryFilter) {
- const [data, total] =
- await this.categoryService.search(searchCategoryFilter);
- return PageResultMapper.toPageResult<CategoryVo>(
- CategoryMapper.toVos(data),
- searchCategoryFilter.getPage(),
- searchCategoryFilter.getSize(),
- total,
- );
- }
- @ApiBearerAuth()
- @Get('/list')
- @ApiOkResponse({
- schema: {
- type: 'array',
- items: { $ref: getSchemaPath(CategoryVo) },
- },
- })
- async list() {
- return CategoryMapper.toVos(await this.categoryService.list());
- }
- @Get(':id')
- @ApiBearerAuth()
- @ApiOkResponse({
- type: CategoryVo,
- })
- async get(@Param('id') id: string) {
- return CategoryMapper.toVo(await this.categoryService.get(id));
- }
- @Put(':id')
- @ApiBearerAuth()
- @ApiOkResponse({
- type: CategoryVo,
- })
- async update(
- @Param('id') id: string,
- @Body() updateCategoryRequest: UpdateCategoryRequest,
- ) {
- return CategoryMapper.toVo(
- await this.categoryService.update(id, updateCategoryRequest),
- );
- }
- @Delete(':id')
- @ApiBearerAuth()
- async delete(@Param('id') id: string) {
- await this.categoryService.delete(id);
- }
- }
|