From 03e58a4ee35f0786ee821746f8d768c5d4431ee7 Mon Sep 17 00:00:00 2001 From: seokyung Date: Mon, 8 Jan 2024 16:56:32 +0900 Subject: [PATCH] =?UTF-8?q?feat=20:=20=EB=8B=89=EB=84=A4=EC=9E=84=20?= =?UTF-8?q?=EC=9C=A0=ED=9A=A8=EC=84=B1=20=EA=B2=80=EC=82=AC=20=ED=8C=8C?= =?UTF-8?q?=EC=9D=B4=ED=94=84=EB=A1=9C=20=EB=BA=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * 유효성 검사는 파이프 로직에서 담당하는게 맞아, Dto를 생성한 후 --- server/src/config/errorCode.enum.ts | 2 -- server/src/user/user.controller.ts | 14 +++++----- server/src/user/user.service.ts | 42 ++++++----------------------- 3 files changed, 16 insertions(+), 42 deletions(-) diff --git a/server/src/config/errorCode.enum.ts b/server/src/config/errorCode.enum.ts index 6015a67..22805e9 100644 --- a/server/src/config/errorCode.enum.ts +++ b/server/src/config/errorCode.enum.ts @@ -20,8 +20,6 @@ export enum ERROR_CODE { 'FAIL_GREEN_EYE_IMAGE_RECOGNITION' = 4012, 'BAD_IMAGE' = 4013, 'NOT_ADDED_MUSIC' = 4014, - 'NOT_IN_RANGE_OF_NICKNAME_LENGTH' = 4015, - 'INVALID_NICKNAME_PATTERN' = 4016, 'WRONG_TOKEN' = 4100, 'EXPIRED_TOKEN' = 4101, } diff --git a/server/src/user/user.controller.ts b/server/src/user/user.controller.ts index 3098918..108c741 100644 --- a/server/src/user/user.controller.ts +++ b/server/src/user/user.controller.ts @@ -5,11 +5,13 @@ import { HttpCode, Param, UseGuards, + UsePipes, Patch, Body, Query, Logger, Put, + ValidationPipe, } from '@nestjs/common'; import { UserService } from './user.service'; import { HTTP_STATUS_CODE } from 'src/httpStatusCode.enum'; @@ -18,6 +20,7 @@ import { Music } from 'src/entity/music.entity'; import { CatchyException } from 'src/config/catchyException'; import { ERROR_CODE } from 'src/config/errorCode.enum'; import { User } from 'src/entity/user.entity'; +import { UserUpdateDto } from 'src/dto/userUpdate.dto'; @Controller('users') export class UserController { @@ -26,21 +29,20 @@ export class UserController { @Patch() @UseGuards(AuthGuard()) + @UsePipes(ValidationPipe) @HttpCode(HTTP_STATUS_CODE.SUCCESS) async updateUserImage( @Req() req, - @Body('image_url') image_url: string, - @Body('nickname') nickname: string | null, + @Body() userUpdateDto: UserUpdateDto, ): Promise<{ user_id: string }> { this.logger.log( - `PATCH /users - nickname=${req.user.nickname}->${nickname}, image_url=${image_url}`, + `PATCH /users - nickname=${req.user.nickname}->${userUpdateDto.nickname}, image_url=${userUpdateDto.image_url}`, ); const user_id = req.user.user_id; return { user_id: await this.userService.updateUserInformation( user_id, - image_url, - nickname, + userUpdateDto, ), }; } @@ -51,7 +53,7 @@ export class UserController { @Param('name') name: string, ): Promise<{ nickname: string }> { this.logger.log(`GET /users/duplicate/${name}`); - if (await this.userService.isDuplicatedUserEmail(name)) { + if (await this.userService.isDuplicatedUserNickname(name)) { throw new CatchyException( 'DUPLICATED_NICKNAME', HTTP_STATUS_CODE.DUPLICATED_NICKNAME, diff --git a/server/src/user/user.service.ts b/server/src/user/user.service.ts index 250d240..8edb5c7 100644 --- a/server/src/user/user.service.ts +++ b/server/src/user/user.service.ts @@ -7,6 +7,7 @@ import { InjectRepository } from '@nestjs/typeorm'; import { CatchyException } from 'src/config/catchyException'; import { ERROR_CODE } from 'src/config/errorCode.enum'; import { Recent_Played } from 'src/entity/recent_played.entity'; +import { UserUpdateDto } from './../dto/userUpdate.dto'; @Injectable() export class UserService { @@ -17,35 +18,7 @@ export class UserService { private recentPlayedRepository: Repository, ) {} - validateNickname(inputNickname: string): void { - if (!inputNickname) { - return; - } - - if (inputNickname.length < 2 || inputNickname.length > 10) { - this.logger.error( - `user.service - validateNickname : NOT_IN_RANGE_OF_NICKNAME_LENGTH`, - ); - throw new CatchyException( - 'NOT_IN_RANGE_OF_NICKNAME_LENGTH', - HTTP_STATUS_CODE.SERVER_ERROR, - ERROR_CODE.NOT_IN_RANGE_OF_NICKNAME_LENGTH, - ); - } - - if (!inputNickname.match(/^[가-힣a-zA-Z0-9_.]+$/)) { - this.logger.error( - `user.service - validateNickname : INVALID_NICKNAME_PATTERN`, - ); - throw new CatchyException( - 'INVALID_NICKNAME_PATTERN', - HTTP_STATUS_CODE.SERVER_ERROR, - ERROR_CODE.INVALID_NICKNAME_PATTERN, - ); - } - } - - async isDuplicatedUserEmail(userNickname: string): Promise { + async isDuplicatedUserNickname(userNickname: string): Promise { try { const user = await this.userRepository.findOneBy({ nickname: userNickname, @@ -86,8 +59,7 @@ export class UserService { async updateUserInformation( user_id: string, - image_url: string, - nickname: string | null, + userUpdateDto: UserUpdateDto, ): Promise { try { const targetUser: User = await this.userRepository.findOne({ @@ -103,7 +75,10 @@ export class UserService { ); } - if (await this.isDuplicatedUserEmail(nickname)) { + const nickname = userUpdateDto.nickname; + const image_url = userUpdateDto.image_url; + + if (await this.isDuplicatedUserNickname(nickname)) { throw new CatchyException( 'DUPLICATED_NICKNAME', HTTP_STATUS_CODE.DUPLICATED_NICKNAME, @@ -111,10 +86,9 @@ export class UserService { ); } - this.validateNickname(nickname); - targetUser.photo = image_url; targetUser.nickname = nickname ? nickname : targetUser.nickname; + const savedUser: User = await this.userRepository.save(targetUser); return savedUser.user_id; } catch (err) {