Skip to content

Commit

Permalink
✨ fix get profile
Browse files Browse the repository at this point in the history
  • Loading branch information
KMUlee committed May 12, 2024
1 parent 3ed6495 commit b8cf57f
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 25 deletions.
6 changes: 6 additions & 0 deletions src/profile/dto/profile.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
3 changes: 3 additions & 0 deletions src/profile/entities/profile.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ export class Profile {
@Column()
githubLink: string;

@Column()
image: string;

@CreateDateColumn({ type: 'timestamptz' })
@Exclude({ toPlainOnly: true })
createdAt: Date;
Expand Down
22 changes: 0 additions & 22 deletions src/profile/profile.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -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<ProfileDto> {
return await this.profileService.createProfile(user.userId, newProfile);
}

@Put('/')
@ApiBody({
description: 'Create a profile',
Expand Down
24 changes: 21 additions & 3 deletions src/profile/profile.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -179,18 +179,36 @@ export class ProfileService {
): Promise<AWS.S3.ManagedUpload.SendData> {
await this.checkAndCreateUserFolder(userId);
const fileKey = `${userId}/profile.png`;

return this.s3
const upload = await this.s3
.upload({
Bucket: this.bucketName,
Key: fileKey,
Body: file.buffer,
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<string> {
const exists = await this.fileExists(userId);

if (!exists) {
Expand Down
1 change: 1 addition & 0 deletions src/user/user.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ export class UserService {
title: '',
description: '',
githubLink: '',
image: '',
});
this.logger.log('profile created');

Expand Down

0 comments on commit b8cf57f

Please sign in to comment.