From b8cf57fcbccd5cd78dfc16b13dece9a81a1d0d1f Mon Sep 17 00:00:00 2001 From: SeungWon Date: Sat, 11 May 2024 21:47:25 -0700 Subject: [PATCH] :sparkles: fix get profile --- src/profile/dto/profile.dto.ts | 6 ++++++ src/profile/entities/profile.entity.ts | 3 +++ src/profile/profile.controller.ts | 22 ---------------------- src/profile/profile.service.ts | 24 +++++++++++++++++++++--- src/user/user.service.ts | 1 + 5 files changed, 31 insertions(+), 25 deletions(-) diff --git a/src/profile/dto/profile.dto.ts b/src/profile/dto/profile.dto.ts index 789c6e9139..d3d5705849 100644 --- a/src/profile/dto/profile.dto.ts +++ b/src/profile/dto/profile.dto.ts @@ -30,6 +30,12 @@ export class ProfileDto { example: 'https://github.com/KMUlee', }) readonly githubLink: string; + + @IsString() + @ApiProperty({ + example: 'https://s3.ap-northeast-2.amazonaws.com/...', + }) + readonly image: string; } export class GetProfileDto { diff --git a/src/profile/entities/profile.entity.ts b/src/profile/entities/profile.entity.ts index ac73a26e04..40fb2e2612 100644 --- a/src/profile/entities/profile.entity.ts +++ b/src/profile/entities/profile.entity.ts @@ -32,6 +32,9 @@ export class Profile { @Column() githubLink: string; + @Column() + image: string; + @CreateDateColumn({ type: 'timestamptz' }) @Exclude({ toPlainOnly: true }) createdAt: Date; diff --git a/src/profile/profile.controller.ts b/src/profile/profile.controller.ts index 1a65598369..0827a728c1 100644 --- a/src/profile/profile.controller.ts +++ b/src/profile/profile.controller.ts @@ -25,11 +25,6 @@ import { FileInterceptor } from '@nestjs/platform-express'; export class ProfileController { constructor(private readonly profileService: ProfileService) {} - @Get('/image') - async getProfileImage(@GetUser() user: Payload): Promise<{ src: string }> { - return { src: await this.profileService.getImage(user.userId) }; - } - @Get('/:profileId') @ApiParam({ name: 'profileId', @@ -63,23 +58,6 @@ export class ProfileController { return await this.profileService.getProfile(user.userId); } - @Post('/') - @ApiBody({ - description: 'Create a profile', - type: ProfileDto, - }) - @ApiResponse({ - status: 201, - description: 'Create a profile', - type: ProfileDto, - }) - async createProfile( - @GetUser() user: Payload, - @Body() newProfile: ProfileDto, - ): Promise { - return await this.profileService.createProfile(user.userId, newProfile); - } - @Put('/') @ApiBody({ description: 'Create a profile', diff --git a/src/profile/profile.service.ts b/src/profile/profile.service.ts index 11bd08ce02..66c36a1fb5 100644 --- a/src/profile/profile.service.ts +++ b/src/profile/profile.service.ts @@ -179,8 +179,7 @@ export class ProfileService { ): Promise { await this.checkAndCreateUserFolder(userId); const fileKey = `${userId}/profile.png`; - - return this.s3 + const upload = await this.s3 .upload({ Bucket: this.bucketName, Key: fileKey, @@ -188,9 +187,28 @@ export class ProfileService { ACL: 'public-read', }) .promise(); + const profile = await this.profileRepository.findOne({ + where: { + user: { + id: userId, + }, + }, + relations: ['user'], + }); + try { + await this.profileRepository.save({ + ...profile, + image: upload.Location, + }); + } catch (error) { + console.error(`Error saving image URL: ${error}`); + throw new Error('Unable to save image URL.'); + } + + return upload; } - async getImage(userId: number) { + async getImage(userId: number): Promise { const exists = await this.fileExists(userId); if (!exists) { diff --git a/src/user/user.service.ts b/src/user/user.service.ts index e2ed480efd..4f2dfca7b4 100644 --- a/src/user/user.service.ts +++ b/src/user/user.service.ts @@ -45,6 +45,7 @@ export class UserService { title: '', description: '', githubLink: '', + image: '', }); this.logger.log('profile created');