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..1476f727 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, Patch, 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)); } + + @Patch('/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..f974e730 100644 --- a/BE/src/auth/user.service.ts +++ b/BE/src/auth/user.service.ts @@ -1,4 +1,8 @@ -import { Injectable } from '@nestjs/common'; +import { + BadRequestException, + Injectable, + NotFoundException, +} from '@nestjs/common'; import { UserRepository } from './user.repository'; import { ProfileResponseDto } from './dto/profile-response.dto'; @@ -10,4 +14,20 @@ 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('존재하지 않는 유저입니다.'); + } + + const isDuplicated = await this.userRepository.existsBy({ + nickname: newName, + }); + if (isDuplicated) { + throw new BadRequestException('이미 존재하는 닉네임입니다.'); + } + + return this.userRepository.update({ id: userId }, { nickname: newName }); + } }