Skip to content
This repository has been archived by the owner on Apr 19, 2023. It is now read-only.

Commit

Permalink
✨ Use DTO in PATCH method
Browse files Browse the repository at this point in the history
  • Loading branch information
AnandChowdhary committed Oct 22, 2020
1 parent 6e0dff4 commit 6d43fcf
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 2 deletions.
5 changes: 3 additions & 2 deletions src/modules/user/user.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,13 @@ import {
Patch,
Query,
} from '@nestjs/common';
import { users, usersUpdateInput } from '@prisma/client';
import { users } from '@prisma/client';
import { OmitSecrets } from 'src/modules/prisma/prisma.interface';
import { CursorPipe } from 'src/pipes/cursor.pipe';
import { OptionalIntPipe } from 'src/pipes/optional-int.pipe';
import { OrderByPipe } from 'src/pipes/order-by.pipe';
import { WherePipe } from 'src/pipes/where.pipe';
import { UpdateUserDto } from './user.dto';
import { UsersService } from './user.service';

@Controller('users')
Expand Down Expand Up @@ -41,7 +42,7 @@ export class UserController {
@Patch(':id')
async update(
@Param('id', ParseIntPipe) id: number,
@Body() data: usersUpdateInput,
@Body() data: UpdateUserDto,
): Promise<OmitSecrets<users>> {
return this.usersService.updateUser({ where: { id: Number(id) }, data });
}
Expand Down
56 changes: 56 additions & 0 deletions src/modules/user/user.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import {
IsBoolean,
IsIn,
IsLocale,
IsObject,
IsString,
IsUrl,
Length,
MinLength,
} from 'class-validator';

export class UpdateUserDto {
@IsBoolean()
checkLocationOnLogin: boolean;

@IsString()
@Length(2, 2)
countryCode: string;

@IsString()
@IsIn(['MALE', 'FEMALE', 'NONBINARY', 'UNKNOWN'])
gender: 'MALE' | 'FEMALE' | 'NONBINARY' | 'UNKNOWN';

@IsString()
@MinLength(3)
name: string;

@IsIn(['ACCOUNT', 'UPDATES', 'PROMOTIONS'])
notificationEmails: 'ACCOUNT' | 'UPDATES' | 'PROMOTIONS';

@IsString()
password: string | null;

@IsLocale()
prefersLanguage: string;

@IsString()
@IsIn(['NO_PREFERENCE', 'LIGHT', 'DARK'])
prefersColorScheme: 'NO_PREFERENCE' | 'LIGHT' | 'DARK';

@IsString()
@IsIn(['NO_PREFERENCE', 'REDUCE'])
prefersReducedMotion: 'NO_PREFERENCE' | 'REDUCE';

@IsUrl()
profilePictureUrl: string;

@IsString()
timezone: string;

@IsBoolean()
twoFactorEnabled: boolean;

@IsObject()
attributes: Record<string, any>;
}

0 comments on commit 6d43fcf

Please sign in to comment.