|
1 | | -import { Controller } from '@nestjs/common' |
| 1 | +import { Body, Controller, Delete, Get, HttpStatus, Param, Patch, Post, Res } from '@nestjs/common' |
2 | 2 | import { IdentitiesService } from './identities.service' |
3 | 3 | import { AbstractController } from '~/_common/abstracts/abstract.controller' |
4 | | -import { ApiTags } from '@nestjs/swagger' |
| 4 | +import { ApiParam, ApiTags } from '@nestjs/swagger' |
| 5 | +import { PartialProjectionType } from '~/_common/types/partial-projection.type' |
| 6 | +import { SearchFilterSchema, FilterSchema, SearchFilterOptions, FilterOptions, ObjectIdValidationPipe } from '@streamkits/nestjs_module_scrud' |
| 7 | +import { Types } from 'mongoose' |
| 8 | +import { ApiCreateDecorator } from '~/_common/decorators/api-create.decorator' |
| 9 | +import { ApiDeletedResponseDecorator } from '~/_common/decorators/api-deleted-response.decorator' |
| 10 | +import { ApiPaginatedDecorator } from '~/_common/decorators/api-paginated.decorator' |
| 11 | +import { ApiReadResponseDecorator } from '~/_common/decorators/api-read-response.decorator' |
| 12 | +import { ApiUpdateDecorator } from '~/_common/decorators/api-update.decorator' |
| 13 | +import { PickProjectionHelper } from '~/_common/helpers/pick-projection.helper' |
| 14 | +import { IdentitiesCreateDto, IdentitiesDto, IdentitiesUpdateDto } from '../identities/_dto/identities.dto' |
| 15 | +import { Response } from 'express' |
5 | 16 |
|
6 | 17 | @ApiTags('core') |
7 | 18 | @Controller('identities') |
8 | 19 | export class IdentitiesController extends AbstractController { |
9 | | - constructor(private readonly service: IdentitiesService) { |
| 20 | + protected static readonly projection: PartialProjectionType<IdentitiesDto> = { |
| 21 | + username: 1, |
| 22 | + } |
| 23 | + |
| 24 | + constructor(private readonly _service: IdentitiesService) { |
10 | 25 | super() |
11 | 26 | } |
| 27 | + |
| 28 | + @Post() |
| 29 | + @ApiCreateDecorator(IdentitiesCreateDto, IdentitiesDto) |
| 30 | + public async create(@Res() res: Response, @Body() body: IdentitiesCreateDto): Promise<Response> { |
| 31 | + const data = await this._service.create(body) |
| 32 | + return res.status(HttpStatus.CREATED).json({ |
| 33 | + statusCode: HttpStatus.CREATED, |
| 34 | + data, |
| 35 | + }) |
| 36 | + } |
| 37 | + |
| 38 | + @Get() |
| 39 | + @ApiPaginatedDecorator(PickProjectionHelper(IdentitiesDto, IdentitiesController.projection)) |
| 40 | + public async search(@Res() res: Response, @SearchFilterSchema() searchFilterSchema: FilterSchema, @SearchFilterOptions() searchFilterOptions: FilterOptions): Promise<Response> { |
| 41 | + //TODO: search tree by parentId |
| 42 | + const [data, total] = await this._service.findAndCount(searchFilterSchema, IdentitiesController.projection, searchFilterOptions) |
| 43 | + return res.status(HttpStatus.OK).json({ |
| 44 | + statusCode: HttpStatus.OK, |
| 45 | + total, |
| 46 | + data, |
| 47 | + }) |
| 48 | + } |
| 49 | + |
| 50 | + @Get(':_id([0-9a-fA-F]{24})') |
| 51 | + @ApiParam({ name: '_id', type: String }) |
| 52 | + @ApiReadResponseDecorator(IdentitiesDto) |
| 53 | + public async read(@Param('_id', ObjectIdValidationPipe) _id: Types.ObjectId, @Res() res: Response): Promise<Response> { |
| 54 | + const data = await this._service.findById(_id) |
| 55 | + return res.status(HttpStatus.OK).json({ |
| 56 | + statusCode: HttpStatus.OK, |
| 57 | + data, |
| 58 | + }) |
| 59 | + } |
| 60 | + |
| 61 | + @Patch(':_id([0-9a-fA-F]{24})') |
| 62 | + @ApiParam({ name: '_id', type: String }) |
| 63 | + @ApiUpdateDecorator(IdentitiesUpdateDto, IdentitiesDto) |
| 64 | + public async update(@Param('_id', ObjectIdValidationPipe) _id: Types.ObjectId, @Body() body: IdentitiesUpdateDto, @Res() res: Response): Promise<Response> { |
| 65 | + const data = await this._service.update(_id, body) |
| 66 | + return res.status(HttpStatus.OK).json({ |
| 67 | + statusCode: HttpStatus.OK, |
| 68 | + data, |
| 69 | + }) |
| 70 | + } |
| 71 | + |
| 72 | + @Delete(':_id([0-9a-fA-F]{24})') |
| 73 | + @ApiParam({ name: '_id', type: String }) |
| 74 | + @ApiDeletedResponseDecorator(IdentitiesDto) |
| 75 | + public async remove(@Param('_id', ObjectIdValidationPipe) _id: Types.ObjectId, @Res() res: Response): Promise<Response> { |
| 76 | + const data = await this._service.delete(_id) |
| 77 | + return res.status(HttpStatus.OK).json({ |
| 78 | + statusCode: HttpStatus.OK, |
| 79 | + data, |
| 80 | + }) |
| 81 | + } |
12 | 82 | } |
0 commit comments