Skip to content

Commit 562905b

Browse files
WIP Preferences DTO
1 parent f27b337 commit 562905b

File tree

2 files changed

+44
-11
lines changed

2 files changed

+44
-11
lines changed
Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,28 @@
1-
import { PartialType } from "@nestjs/swagger"
1+
import { ApiProperty, PartialType } from '@nestjs/swagger'
2+
import { IsString, IsNotEmpty, IsObject, IsOptional, ValidateNested, IsMongoId } from 'class-validator'
23

34
export class PreferencesCreateDto {
5+
@IsString()
6+
@IsNotEmpty()
7+
@ApiProperty()
8+
public name: string
49

10+
@IsMongoId()
11+
@IsOptional()
12+
@ApiProperty()
13+
public entityId?: string
14+
15+
@IsObject()
16+
@IsOptional()
17+
@ValidateNested({ each: true })
18+
@ApiProperty()
19+
public data?: { [key: string]: any }
20+
}
21+
22+
export class PreferencesDto extends PreferencesCreateDto {
23+
@IsMongoId()
24+
@ApiProperty()
25+
public _id: string
526
}
627

7-
export class PreferencesUpdateDto extends PartialType(PreferencesCreateDto) {}
28+
export class PreferencesUpdateDto extends PartialType(PreferencesCreateDto) {}

service/src/core/preferences/preferences.controller.ts

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,35 @@
11
import { Body, Controller, Delete, Get, HttpStatus, Param, Patch, Post, Req, Res } from '@nestjs/common'
2-
import { PreferencesCreateDto, PreferencesUpdateDto } from './_dto/preferences.dto'
2+
import { PreferencesCreateDto, PreferencesDto, PreferencesUpdateDto } from './_dto/preferences.dto'
33
import { PreferencesService } from './preferences.service'
44
import { AbstractController } from '~/_common/abstracts/abstract.controller'
55
import { ApiParam, ApiTags } from '@nestjs/swagger'
66
import { SearchFilterSchema, FilterSchema, SearchFilterOptions, FilterOptions, ObjectIdValidationPipe } from '@streamkits/nestjs_module_scrud'
77
import { Types } from 'mongoose'
88
import { Request, Response } from 'express'
9+
import { PartialProjectionType } from '~/_common/types/partial-projection.type'
10+
import { ApiCreateDecorator } from '~/_common/decorators/api-create.decorator'
11+
import { ApiPaginatedDecorator } from '~/_common/decorators/api-paginated.decorator'
12+
import { PickProjectionHelper } from '~/_common/helpers/pick-projection.helper'
13+
import { ApiReadResponseDecorator } from '~/_common/decorators/api-read-response.decorator'
14+
import { ApiUpdateDecorator } from '~/_common/decorators/api-update.decorator'
15+
import { ApiDeletedResponseDecorator } from '~/_common/decorators/api-deleted-response.decorator'
916

1017
@ApiTags('core')
1118
@Controller('preferences')
1219
export class PreferencesController extends AbstractController {
13-
public readonly projection = {
20+
public static readonly projection: PartialProjectionType<PreferencesDto> = {
1421
name: 1,
1522
data: 1,
16-
personId: 1,
23+
entityId: 1,
1724
}
1825

1926
constructor(private readonly _service: PreferencesService) {
2027
super()
2128
}
2229

2330
@Post()
24-
public async create(@Req() req: Request, @Res() res: Response, @Body() body: PreferencesCreateDto) {
31+
@ApiCreateDecorator(PreferencesCreateDto, PreferencesDto)
32+
public async create(@Req() req: Request, @Res() res: Response, @Body() body: PreferencesCreateDto): Promise<Response> {
2533
const data = await this._service.create(body)
2634
return res.status(HttpStatus.CREATED).json({
2735
statusCode: HttpStatus.CREATED,
@@ -30,8 +38,9 @@ export class PreferencesController extends AbstractController {
3038
}
3139

3240
@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)
41+
@ApiPaginatedDecorator(PickProjectionHelper(PreferencesDto, PreferencesController.projection))
42+
public async search(@Res() res: Response, @SearchFilterSchema() searchFilterSchema: FilterSchema, @SearchFilterOptions() searchFilterOptions: FilterOptions): Promise<Response> {
43+
const [data, total] = await this._service.findAndCount(searchFilterSchema, PreferencesController.projection, searchFilterOptions)
3544
return res.status(HttpStatus.OK).json({
3645
statusCode: HttpStatus.OK,
3746
total,
@@ -41,7 +50,8 @@ export class PreferencesController extends AbstractController {
4150

4251
@Get(':_id([0-9a-fA-F]{24})')
4352
@ApiParam({ name: '_id', type: String })
44-
public async read(@Param('_id', ObjectIdValidationPipe) _id: Types.ObjectId, @Res() res: Response) {
53+
@ApiReadResponseDecorator(PreferencesDto)
54+
public async read(@Param('_id', ObjectIdValidationPipe) _id: Types.ObjectId, @Res() res: Response): Promise<Response> {
4555
const data = await this._service.findById(_id)
4656
return res.status(HttpStatus.OK).json({
4757
statusCode: HttpStatus.OK,
@@ -51,7 +61,8 @@ export class PreferencesController extends AbstractController {
5161

5262
@Patch(':_id([0-9a-fA-F]{24})')
5363
@ApiParam({ name: '_id', type: String })
54-
public async update(@Param('_id', ObjectIdValidationPipe) _id: Types.ObjectId, @Body() body: PreferencesUpdateDto, @Res() res: Response) {
64+
@ApiUpdateDecorator(PreferencesUpdateDto, PreferencesDto)
65+
public async update(@Param('_id', ObjectIdValidationPipe) _id: Types.ObjectId, @Body() body: PreferencesUpdateDto, @Res() res: Response): Promise<Response> {
5566
const data = await this._service.update(_id, body)
5667
return res.status(HttpStatus.OK).json({
5768
statusCode: HttpStatus.OK,
@@ -61,7 +72,8 @@ export class PreferencesController extends AbstractController {
6172

6273
@Delete(':_id([0-9a-fA-F]{24})')
6374
@ApiParam({ name: '_id', type: String })
64-
public async remove(@Param('_id', ObjectIdValidationPipe) _id: Types.ObjectId, @Res() res: Response) {
75+
@ApiDeletedResponseDecorator(PreferencesDto)
76+
public async remove(@Param('_id', ObjectIdValidationPipe) _id: Types.ObjectId, @Res() res: Response): Promise<Response> {
6577
const data = await this._service.delete(_id)
6678
return res.status(HttpStatus.OK).json({
6779
statusCode: HttpStatus.OK,

0 commit comments

Comments
 (0)