Skip to content

Commit

Permalink
feat : 사용자 정보 수정 관련 코드 추가
Browse files Browse the repository at this point in the history
* 기존 PATCH `/users/image`를 PATH `/users`로 변경
* 이미지, 닉네임 수정을 한번에 실시
  • Loading branch information
sk000801 committed Jan 4, 2024
1 parent df19bfc commit bffd252
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 17 deletions.
37 changes: 21 additions & 16 deletions server/src/user/user.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,27 @@ export class UserController {
private readonly logger = new Logger('User');
constructor(private userService: UserService) {}

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

@Get('duplicate/:name')
@HttpCode(HTTP_STATUS_CODE.NOT_DUPLICATED_NICKNAME)
async checkDuplicateNickname(
Expand Down Expand Up @@ -58,22 +79,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)
Expand Down
48 changes: 47 additions & 1 deletion server/src/user/user.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,45 @@ export class UserService {
private recentPlayedRepository: Repository<Recent_Played>,
) {}

validateNickname(inputNickname: string, originalNickname: 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,
);
}

if (inputNickname === originalNickname) {
this.logger.error(
`user.service - validateNickname : SAME_AGAIN_NICKNAME`,
);
throw new CatchyException(
'SAME_AGAIN_NICKNAME',
HTTP_STATUS_CODE.SERVER_ERROR,
ERROR_CODE.SAME_AGAIN_NICKNAME,
);
}
}

async isDuplicatedUserEmail(userNickname: string): Promise<boolean> {
try {
const user = await this.userRepository.findOneBy({
Expand Down Expand Up @@ -56,7 +95,11 @@ export class UserService {
}
}

async updateUserImage(user_id: string, image_url: string): Promise<string> {
async updateUserInformation(
user_id: string,
image_url: string,
nickname: string | null,
): Promise<string> {
try {
const targetUser: User = await this.userRepository.findOne({
where: { user_id },
Expand All @@ -71,7 +114,10 @@ export class UserService {
);
}

this.validateNickname(nickname, targetUser.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) {
Expand Down

0 comments on commit bffd252

Please sign in to comment.