|
1 | | -import { Controller } from '@nestjs/common' |
2 | | -import { CategoriesCreateDto } from './dto/categories.dto' |
| 1 | +import { Body, Controller, Delete, Get, HttpStatus, Param, Patch, Post, Req, Res } from '@nestjs/common' |
| 2 | +import { CategoriesCreateDto, CategoriesUpdateDto } from './_dto/categories.dto' |
3 | 3 | import { CategoriesService } from './categories.service' |
4 | 4 | import { AbstractController } from '~/_common/abstracts/abstract.controller' |
| 5 | +import { ApiParam } from '@nestjs/swagger' |
| 6 | +import { SearchFilterSchema, FilterSchema, SearchFilterOptions, FilterOptions, ObjectIdValidationPipe } from '@streamkits/nestjs_module_scrud' |
| 7 | +import { Types } from 'mongoose' |
| 8 | +import { Request, Response } from 'express' |
5 | 9 |
|
6 | 10 | @Controller('categories') |
7 | 11 | export class CategoriesController extends AbstractController { |
8 | | - constructor(private readonly service: CategoriesService) { |
| 12 | + protected readonly projection = { |
| 13 | + name: 1, |
| 14 | + color: 1, |
| 15 | + icon: 1, |
| 16 | + } |
| 17 | + |
| 18 | + constructor(private readonly _service: CategoriesService) { |
9 | 19 | super() |
10 | 20 | } |
| 21 | + |
| 22 | + @Post() |
| 23 | + public async create(@Req() req: Request, @Res() res: Response, @Body() body: CategoriesCreateDto) { |
| 24 | + const data = await this._service.create(body) |
| 25 | + return res.status(HttpStatus.CREATED).json({ |
| 26 | + statusCode: HttpStatus.CREATED, |
| 27 | + data, |
| 28 | + }) |
| 29 | + } |
| 30 | + |
| 31 | + @Get() |
| 32 | + public async search(@Res() res: Response, @SearchFilterSchema() searchFilterSchema: FilterSchema, @SearchFilterOptions() searchFilterOptions: FilterOptions) { |
| 33 | + const [data, total] = await this._service.findAndCount(searchFilterSchema, this.projection, searchFilterOptions) |
| 34 | + return res.status(HttpStatus.OK).json({ |
| 35 | + statusCode: HttpStatus.OK, |
| 36 | + total, |
| 37 | + data, |
| 38 | + }) |
| 39 | + } |
| 40 | + |
| 41 | + @Get(':_id([0-9a-fA-F]{24})') |
| 42 | + @ApiParam({ name: '_id', type: String }) |
| 43 | + public async read(@Param('_id', ObjectIdValidationPipe) _id: Types.ObjectId, @Res() res: Response) { |
| 44 | + const data = await this._service.findById(_id) |
| 45 | + return res.status(HttpStatus.OK).json({ |
| 46 | + statusCode: HttpStatus.OK, |
| 47 | + data, |
| 48 | + }) |
| 49 | + } |
| 50 | + |
| 51 | + @Patch(':_id([0-9a-fA-F]{24})') |
| 52 | + @ApiParam({ name: '_id', type: String }) |
| 53 | + public async update(@Param('_id', ObjectIdValidationPipe) _id: Types.ObjectId, @Body() body: CategoriesUpdateDto, @Res() res: Response) { |
| 54 | + const data = await this._service.update(_id, body) |
| 55 | + return res.status(HttpStatus.OK).json({ |
| 56 | + statusCode: HttpStatus.OK, |
| 57 | + data, |
| 58 | + }) |
| 59 | + } |
| 60 | + |
| 61 | + @Delete(':_id([0-9a-fA-F]{24})') |
| 62 | + @ApiParam({ name: '_id', type: String }) |
| 63 | + public async remove(@Param('_id', ObjectIdValidationPipe) _id: Types.ObjectId, @Res() res: Response) { |
| 64 | + const data = await this._service.delete(_id) |
| 65 | + return res.status(HttpStatus.OK).json({ |
| 66 | + statusCode: HttpStatus.OK, |
| 67 | + data, |
| 68 | + }) |
| 69 | + } |
11 | 70 | } |
0 commit comments