Skip to content

Commit

Permalink
Merge pull request #364 from boostcampwm2023/server/feature/353
Browse files Browse the repository at this point in the history
아티스트 정보 수정 API 추가
sk000801 authored Jan 8, 2024
2 parents f991ca6 + 03e58a4 commit eec5462
Showing 4 changed files with 62 additions and 20 deletions.
2 changes: 1 addition & 1 deletion server/src/config/errorCode.enum.ts
Original file line number Diff line number Diff line change
@@ -19,7 +19,7 @@ export enum ERROR_CODE {
'INVALID_GREEN_EYE_REQUEST' = 4011,
'FAIL_GREEN_EYE_IMAGE_RECOGNITION' = 4012,
'BAD_IMAGE' = 4013,
'NOT_ADDED_MUSIC' = 4014,
'WRONG_TOKEN' = 4100,
'EXPIRED_TOKEN' = 4101,
'NOT_ADDED_MUSIC' = 4014,
}
18 changes: 18 additions & 0 deletions server/src/dto/userUpdate.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import {
IsNotEmpty,
IsString,
Matches,
MaxLength,
MinLength,
} from 'class-validator';

export class UserUpdateDto {
@MinLength(2, { message: '닉네임이 2글자 미만입니다.' })
@MaxLength(10, { message: '닉네임이 10글자 초과입니다.' })
@Matches(/^[-a-zA-Z0-9_.]+$/)
nickname: string | null;

@IsNotEmpty()
@IsString()
image_url: string;
}
41 changes: 24 additions & 17 deletions server/src/user/user.controller.ts
Original file line number Diff line number Diff line change
@@ -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,19 +20,40 @@ 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 {
private readonly logger = new Logger('User');
constructor(private userService: UserService) {}

@Patch()
@UseGuards(AuthGuard())
@UsePipes(ValidationPipe)
@HttpCode(HTTP_STATUS_CODE.SUCCESS)
async updateUserImage(
@Req() req,
@Body() userUpdateDto: UserUpdateDto,
): Promise<{ user_id: string }> {
this.logger.log(
`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,
userUpdateDto,
),
};
}

@Get('duplicate/:name')
@HttpCode(HTTP_STATUS_CODE.NOT_DUPLICATED_NICKNAME)
async checkDuplicateNickname(
@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,
@@ -58,22 +81,6 @@ export class UserController {
return userMusicData;
}

@Patch('image')
@UseGuards(AuthGuard())
@HttpCode(HTTP_STATUS_CODE.SUCCESS)
async updateUserImage(
@Req() req,
@Body('image_url') image_url,
): Promise<{ user_id: string }> {
this.logger.log(
`PATCH /users/image - nickname=${req.user.nickname}, image_url=${image_url}`,
);
const user_id = req.user.user_id;
return {
user_id: await this.userService.updateUserImage(user_id, image_url),
};
}

@Get('my-info')
@UseGuards(AuthGuard())
@HttpCode(HTTP_STATUS_CODE.SUCCESS)
21 changes: 19 additions & 2 deletions server/src/user/user.service.ts
Original file line number Diff line number Diff line change
@@ -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,7 +18,7 @@ export class UserService {
private recentPlayedRepository: Repository<Recent_Played>,
) {}

async isDuplicatedUserEmail(userNickname: string): Promise<boolean> {
async isDuplicatedUserNickname(userNickname: string): Promise<boolean> {
try {
const user = await this.userRepository.findOneBy({
nickname: userNickname,
@@ -56,7 +57,10 @@ export class UserService {
}
}

async updateUserImage(user_id: string, image_url: string): Promise<string> {
async updateUserInformation(
user_id: string,
userUpdateDto: UserUpdateDto,
): Promise<string> {
try {
const targetUser: User = await this.userRepository.findOne({
where: { user_id },
@@ -71,7 +75,20 @@ export class UserService {
);
}

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,
ERROR_CODE.DUPLICATED_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) {

0 comments on commit eec5462

Please sign in to comment.