From 0d9b372f709b00ee44aab346ef803183202b9472 Mon Sep 17 00:00:00 2001 From: 2paperstar Date: Fri, 7 Jun 2024 10:38:32 +0900 Subject: [PATCH] fix: maintain post dto with previous --- src/post/dto/req/CreatePost.dto.ts | 6 +++--- src/post/dto/res/postRes.dto.ts | 10 +++++----- src/post/post.controller.ts | 2 +- src/post/post.mapper.ts | 2 +- src/post/post.repository.ts | 5 +++-- src/post/post.service.ts | 30 ++++++++++++++++++++++++++---- 6 files changed, 39 insertions(+), 16 deletions(-) diff --git a/src/post/dto/req/CreatePost.dto.ts b/src/post/dto/req/CreatePost.dto.ts index 7a1bd88..8a4b843 100644 --- a/src/post/dto/req/CreatePost.dto.ts +++ b/src/post/dto/req/CreatePost.dto.ts @@ -22,11 +22,11 @@ export class CreatePostDto { body: string; @ApiProperty({ - example: ['image_uuid1,', 'image_uuid2', 'image_uuid3'], - description: '이미지 uuid list', + example: ['image_url1,', 'image_url2', 'image_url3'], + description: '이미지 url list', required: true, }) @IsString({ each: true }) @IsNotEmpty() - imageUuid: string[] = []; + imageUrls: string[] = []; } diff --git a/src/post/dto/res/postRes.dto.ts b/src/post/dto/res/postRes.dto.ts index f6c0545..0798d1f 100644 --- a/src/post/dto/res/postRes.dto.ts +++ b/src/post/dto/res/postRes.dto.ts @@ -22,9 +22,9 @@ export class PostDto { contents: { title: string; body: string }; @ApiProperty({ - example: ['image_uuid1,', 'image_uuid2', 'image_uuid3'], + example: ['image_url1,', 'image_url2', 'image_url3'], }) - imageUuid: string[]; + imageUrls: string[]; } export class PostListDto { @@ -48,7 +48,7 @@ export class PostListDto { title: 'This is title', body: 'This is body', }, - imageUuid: ['image_uuid1,', 'image_uuid2', 'image_uuid3'], + imageUrls: ['image_url1,', 'image_url2', 'image_url3'], }, { id: 1, @@ -63,7 +63,7 @@ export class PostListDto { title: 'This is title', body: 'This is body', }, - imageUuid: ['image_uuid1,', 'image_uuid2', 'image_uuid3'], + imageUrls: ['image_url1,', 'image_url2', 'image_url3'], }, { id: 1, @@ -78,7 +78,7 @@ export class PostListDto { title: 'This is title', body: 'This is body', }, - imageUuid: ['image_uuid1,', 'image_uuid2', 'image_uuid3'], + imageUrls: ['image_url1,', 'image_url2', 'image_url3'], }, ], }) diff --git a/src/post/post.controller.ts b/src/post/post.controller.ts index 0152aec..a5537e3 100644 --- a/src/post/post.controller.ts +++ b/src/post/post.controller.ts @@ -60,7 +60,7 @@ export class PostController { }) @ApiInternalServerErrorResponse({ description: 'Internal Server Error' }) @Get(':id') - getPost(@Param('id', ParseIntPipe) id: number) { + getPost(@Param('id', ParseIntPipe) id: number): Promise { return this.postService.getPost(id); } diff --git a/src/post/post.mapper.ts b/src/post/post.mapper.ts index e47ddeb..6ff6120 100644 --- a/src/post/post.mapper.ts +++ b/src/post/post.mapper.ts @@ -19,7 +19,7 @@ export class PostMapper { updatedAt, author, contents: contents[0], - imageUuid: files.map(({ uuid }: any) => `${uuid}`), + imageUuids: files.map(({ imageUuid }) => imageUuid), }; } } diff --git a/src/post/post.repository.ts b/src/post/post.repository.ts index 47fc826..4f92fba 100644 --- a/src/post/post.repository.ts +++ b/src/post/post.repository.ts @@ -92,8 +92,9 @@ export class PostRepository { } async createPost( - { title, body, imageUuid }: CreatePostDto, + { title, body }: CreatePostDto, userUuid: string, + imageUuids: string[], ) { this.logger.log('createPost'); return this.prismaService.post @@ -113,7 +114,7 @@ export class PostRepository { createdAt: new Date(), files: { create: [ - ...imageUuid.map((uuid, idx) => ({ + ...imageUuids.map((uuid, idx) => ({ order: idx, name: title, imageUuid: uuid, diff --git a/src/post/post.service.ts b/src/post/post.service.ts index f6ac18c..e134976 100644 --- a/src/post/post.service.ts +++ b/src/post/post.service.ts @@ -3,24 +3,43 @@ import { CreatePostDto } from './dto/req/CreatePost.dto'; import { PostRepository } from './post.repository'; import { PostMapper } from './post.mapper'; import { UpdatePostDto } from './dto/req/UpdatePost.dto'; +import { FileService } from 'src/file/file.service'; @Injectable() export class PostService { constructor( private readonly postRepository: PostRepository, private readonly postMapper: PostMapper, + private readonly fileService: FileService, ) {} async getPost(id: number) { const rawPost = await this.postRepository.getPost(id); - return this.postMapper.processPost(rawPost); + const { imageUuids, ...post } = this.postMapper.processPost(rawPost); + return { + ...post, + imageUrls: await Promise.all( + imageUuids.map(async (imageUuid) => + this.fileService.getSignedUrl(imageUuid), + ), + ), + }; } async getPostList(page: number, pageSize: number) { const rawPostList = await this.postRepository.getPostList(page, pageSize); - const processPostList = rawPostList.map((rawPost) => - this.postMapper.processPost(rawPost), + const processPostList = await Promise.all( + rawPostList + .map((rawPost) => this.postMapper.processPost(rawPost)) + .map(async ({ imageUuids, ...post }) => ({ + ...post, + imageUrls: await Promise.all( + imageUuids.map(async (imageUuid) => + this.fileService.getSignedUrl(imageUuid), + ), + ), + })), ); return { total: pageSize, list: processPostList }; @@ -30,9 +49,12 @@ export class PostService { const rawPost = await this.postRepository.createPost( createPostDto, userUuid, + createPostDto.imageUrls.map((url) => + new URL(url).pathname.split('/').slice(1).join('/'), + ), ); - return this.postMapper.processPost(rawPost); + return this.getPost(rawPost.id); } async updatePost(updatePostDto: UpdatePostDto, id: number, userUuid: string) {