From 44b477900bac574501999a42c3cec47d1b043976 Mon Sep 17 00:00:00 2001 From: JIN Date: Thu, 28 Nov 2024 01:11:25 +0900 Subject: [PATCH 1/4] =?UTF-8?q?=E2=9C=A8=20feat:=20=EC=9C=A0=EC=A0=80=20?= =?UTF-8?q?=EB=8B=89=EB=84=A4=EC=9E=84=20=EB=B3=80=EA=B2=BD=20=EB=A1=9C?= =?UTF-8?q?=EC=A7=81=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- BE/src/auth/dto/rename-user.dto.ts | 6 ++++++ BE/src/auth/user.controller.ts | 20 +++++++++++++++++++- BE/src/auth/user.service.ts | 11 ++++++++++- 3 files changed, 35 insertions(+), 2 deletions(-) create mode 100644 BE/src/auth/dto/rename-user.dto.ts diff --git a/BE/src/auth/dto/rename-user.dto.ts b/BE/src/auth/dto/rename-user.dto.ts new file mode 100644 index 00000000..fe809699 --- /dev/null +++ b/BE/src/auth/dto/rename-user.dto.ts @@ -0,0 +1,6 @@ +import { ApiProperty } from '@nestjs/swagger'; + +export class RenameUserDto { + @ApiProperty({ description: '변경할 닉네임' }) + nickname: string; +} diff --git a/BE/src/auth/user.controller.ts b/BE/src/auth/user.controller.ts index 881a2b7c..5b460fe7 100644 --- a/BE/src/auth/user.controller.ts +++ b/BE/src/auth/user.controller.ts @@ -1,7 +1,8 @@ -import { Controller, Get, Req, UseGuards } from '@nestjs/common'; +import { Body, Controller, Get, Post, Req, UseGuards } from '@nestjs/common'; import { Request } from 'express'; import { ApiBearerAuth, + ApiBody, ApiOperation, ApiResponse, ApiTags, @@ -9,6 +10,7 @@ import { import { UserService } from './user.service'; import { JwtAuthGuard } from './jwt-auth-guard'; import { ProfileResponseDto } from './dto/profile-response.dto'; +import { RenameUserDto } from './dto/rename-user.dto'; @Controller('/api/user') @ApiTags('프로필 API') @@ -27,4 +29,20 @@ export class UserController { getProfile(@Req() request: Request) { return this.userService.getProfile(parseInt(request.user.userId, 10)); } + + @Post('/rename') + @UseGuards(JwtAuthGuard) + @ApiOperation({ + summary: '유저 닉네임 변경 API', + }) + @ApiBody({ + type: RenameUserDto, + }) + @ApiBearerAuth() + renameUser(@Req() request: Request, @Body() body: RenameUserDto) { + const userId = parseInt(request.user.userId, 10); + const newName = body.nickname; + + return this.userService.renameUser(userId, newName); + } } diff --git a/BE/src/auth/user.service.ts b/BE/src/auth/user.service.ts index fe860a50..bc17c426 100644 --- a/BE/src/auth/user.service.ts +++ b/BE/src/auth/user.service.ts @@ -1,4 +1,4 @@ -import { Injectable } from '@nestjs/common'; +import { Injectable, NotFoundException } from '@nestjs/common'; import { UserRepository } from './user.repository'; import { ProfileResponseDto } from './dto/profile-response.dto'; @@ -10,4 +10,13 @@ export class UserService { const user = await this.userRepository.findOneBy({ id: userId }); return new ProfileResponseDto(user.nickname, user.email); } + + async renameUser(userId: number, newName: string) { + const user = await this.userRepository.findOneBy({ id: userId }); + if (!user) { + throw new NotFoundException('존재하지 않는 유저입니다.'); + } + + return this.userRepository.update({ id: userId }, { nickname: newName }); + } } From 0e4f1ced4b919b314f8c5208112921c51cf2652c Mon Sep 17 00:00:00 2001 From: JIN Date: Thu, 28 Nov 2024 01:21:52 +0900 Subject: [PATCH 2/4] =?UTF-8?q?=E2=9C=A8=20feat:=20=EC=A4=91=EB=B3=B5?= =?UTF-8?q?=EB=90=9C=20=EB=8B=89=EB=84=A4=EC=9E=84=EC=9C=BC=EB=A1=9C=20?= =?UTF-8?q?=EB=B3=80=EA=B2=BD=ED=95=A0=20=EC=88=98=20=EC=97=86=EA=B2=8C=20?= =?UTF-8?q?=EB=A1=9C=EC=A7=81=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- BE/src/auth/user.service.ts | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/BE/src/auth/user.service.ts b/BE/src/auth/user.service.ts index bc17c426..9c56765e 100644 --- a/BE/src/auth/user.service.ts +++ b/BE/src/auth/user.service.ts @@ -1,4 +1,8 @@ -import { Injectable, NotFoundException } from '@nestjs/common'; +import { + BadRequestException, + Injectable, + NotFoundException, +} from '@nestjs/common'; import { UserRepository } from './user.repository'; import { ProfileResponseDto } from './dto/profile-response.dto'; @@ -17,6 +21,13 @@ export class UserService { throw new NotFoundException('존재하지 않는 유저입니다.'); } + const isDuplicated = await this.userRepository.findBy({ + nickname: newName, + }); + if (isDuplicated.length > 0) { + throw new BadRequestException('이미 존재하는 닉네임입니다.'); + } + return this.userRepository.update({ id: userId }, { nickname: newName }); } } From ea7e2d4f2176218591e5b4be654555eaa1efe73b Mon Sep 17 00:00:00 2001 From: JIN Date: Thu, 28 Nov 2024 10:35:35 +0900 Subject: [PATCH 3/4] =?UTF-8?q?=F0=9F=94=A7=20fix:=20PUT=20->=20PATCH=20?= =?UTF-8?q?=EB=B0=A9=EC=8B=9D=EC=9C=BC=EB=A1=9C=20=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- BE/src/auth/user.controller.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/BE/src/auth/user.controller.ts b/BE/src/auth/user.controller.ts index 5b460fe7..1476f727 100644 --- a/BE/src/auth/user.controller.ts +++ b/BE/src/auth/user.controller.ts @@ -1,4 +1,4 @@ -import { Body, Controller, Get, Post, Req, UseGuards } from '@nestjs/common'; +import { Body, Controller, Get, Patch, Req, UseGuards } from '@nestjs/common'; import { Request } from 'express'; import { ApiBearerAuth, @@ -30,7 +30,7 @@ export class UserController { return this.userService.getProfile(parseInt(request.user.userId, 10)); } - @Post('/rename') + @Patch('/rename') @UseGuards(JwtAuthGuard) @ApiOperation({ summary: '유저 닉네임 변경 API', From b6f84560abd8b05b463debd86f0fea56a62620b6 Mon Sep 17 00:00:00 2001 From: JIN Date: Thu, 28 Nov 2024 10:37:29 +0900 Subject: [PATCH 4/4] =?UTF-8?q?=F0=9F=94=A7=20fix:=20existby=EB=A5=BC=20?= =?UTF-8?q?=EC=82=AC=EC=9A=A9=ED=95=B4=20boolean=20=EA=B0=92=EC=9C=BC?= =?UTF-8?q?=EB=A1=9C=20=EC=A4=91=EB=B3=B5=EC=97=AC=EB=B6=80=20=EC=B2=B4?= =?UTF-8?q?=ED=81=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- BE/src/auth/user.service.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/BE/src/auth/user.service.ts b/BE/src/auth/user.service.ts index 9c56765e..f974e730 100644 --- a/BE/src/auth/user.service.ts +++ b/BE/src/auth/user.service.ts @@ -21,10 +21,10 @@ export class UserService { throw new NotFoundException('존재하지 않는 유저입니다.'); } - const isDuplicated = await this.userRepository.findBy({ + const isDuplicated = await this.userRepository.existsBy({ nickname: newName, }); - if (isDuplicated.length > 0) { + if (isDuplicated) { throw new BadRequestException('이미 존재하는 닉네임입니다.'); }