Skip to content

Commit

Permalink
fix: maintain post dto with previous
Browse files Browse the repository at this point in the history
  • Loading branch information
2paperstar committed Jun 7, 2024
1 parent d578c91 commit 0d9b372
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 16 deletions.
6 changes: 3 additions & 3 deletions src/post/dto/req/CreatePost.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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[] = [];
}
10 changes: 5 additions & 5 deletions src/post/dto/res/postRes.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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'],
},
],
})
Expand Down
2 changes: 1 addition & 1 deletion src/post/post.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<PostDto> {
return this.postService.getPost(id);
}

Expand Down
2 changes: 1 addition & 1 deletion src/post/post.mapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export class PostMapper {
updatedAt,
author,
contents: contents[0],
imageUuid: files.map(({ uuid }: any) => `${uuid}`),
imageUuids: files.map(({ imageUuid }) => imageUuid),
};
}
}
5 changes: 3 additions & 2 deletions src/post/post.repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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,
Expand Down
30 changes: 26 additions & 4 deletions src/post/post.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 };
Expand All @@ -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) {
Expand Down

0 comments on commit 0d9b372

Please sign in to comment.