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