diff --git a/server/src/upload/upload.controller.ts b/server/src/upload/upload.controller.ts index 6500628..5fa4c74 100644 --- a/server/src/upload/upload.controller.ts +++ b/server/src/upload/upload.controller.ts @@ -1,21 +1,15 @@ import { - Post, Controller, Get, Req, Query, HttpCode, UseGuards, - UseInterceptors, - UploadedFile, - ParseFilePipe, - FileTypeValidator, } from '@nestjs/common'; import { UploadService } from './upload.service'; import { HTTP_STATUS_CODE } from 'src/httpStatusCode.enum'; import { AuthGuard } from '@nestjs/passport'; import { v4 } from 'uuid'; -import { FileInterceptor } from '@nestjs/platform-express'; import { CatchyException } from 'src/config/catchyException'; import { ERROR_CODE } from 'src/config/errorCode.enum'; @@ -57,38 +51,4 @@ export class UploadController { throw error; } } - - @Post('/music') - @UseInterceptors(FileInterceptor('file')) - async uploadMusic( - @UploadedFile( - new ParseFilePipe({ - validators: [ - // new MaxFileSizeValidator({ maxSize: fileSize.MUSIC_FILE_LIMIT_SIZE }), - new FileTypeValidator({ fileType: 'audio/mpeg' }), - ], - }), - ) - file: Express.Multer.File, - ) { - const { url } = await this.uploadService.uploadMusic(file); - return { url }; - } - - @Post('/image') - @UseInterceptors(FileInterceptor('file')) - async uploadImage( - @UploadedFile( - new ParseFilePipe({ - validators: [ - // new MaxFileSizeValidator({ maxSize: fileSize.IMAGE_FILE_LIMIT_SIZE }), - new FileTypeValidator({ fileType: 'image/png' }), - ], - }), - ) - file: Express.Multer.File, - ) { - const { url } = await this.uploadService.uploadImage(file); - return { url }; - } } diff --git a/server/src/user/user.controller.ts b/server/src/user/user.controller.ts index 13f95b9..77d896a 100644 --- a/server/src/user/user.controller.ts +++ b/server/src/user/user.controller.ts @@ -5,6 +5,8 @@ import { HttpCode, Param, UseGuards, + Patch, + Body, } from '@nestjs/common'; import { UserService } from './user.service'; import { HTTP_STATUS_CODE } from 'src/httpStatusCode.enum'; @@ -26,7 +28,7 @@ export class UserController { throw new CatchyException( 'DUPLICATED_NICKNAME', HTTP_STATUS_CODE.DUPLICATED_NICKNAME, - ERROR_CODE.DUPLICATED_NICKNAME + ERROR_CODE.DUPLICATED_NICKNAME, ); } @@ -43,4 +45,17 @@ 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 }> { + const user_id = req.user.userId; + return { + user_id: await this.userService.updateUserImage(user_id, image_url), + }; + } } diff --git a/server/src/user/user.service.ts b/server/src/user/user.service.ts index 1dc6504..d8dd21f 100644 --- a/server/src/user/user.service.ts +++ b/server/src/user/user.service.ts @@ -27,11 +27,45 @@ export class UserService { return false; } catch { - throw new CatchyException('SERVER ERROR', HTTP_STATUS_CODE.SERVER_ERROR, ERROR_CODE.SERVICE_ERROR); + throw new CatchyException( + 'SERVER ERROR', + HTTP_STATUS_CODE.SERVER_ERROR, + ERROR_CODE.SERVICE_ERROR, + ); } } async getRecentPlayedMusicByUserId(userId: string): Promise { return await this.playlistService.getRecentMusicsByUserId(userId); } + + async updateUserImage(user_id: string, image_url: string): Promise { + try { + const targetUser: User = await this.userRepository.findOne({ + where: { user_id }, + }); + + if (!targetUser) { + throw new CatchyException( + 'NOT_EXIST_USER', + HTTP_STATUS_CODE.BAD_REQUEST, + ERROR_CODE.NOT_EXIST_USER, + ); + } + + targetUser.photo = image_url; + const savedUser: User = await this.userRepository.save(targetUser); + return savedUser.user_id; + } catch (err) { + if (err instanceof CatchyException) { + throw err; + } + + throw new CatchyException( + 'SERVER_ERROR', + HTTP_STATUS_CODE.SERVER_ERROR, + ERROR_CODE.SERVICE_ERROR, + ); + } + } }