From 3cd93800f6e22d630e42e361e6e3526bed37fb64 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B9=80=EB=AF=BC=EC=88=98=20MINSOO=20KIM?= Date: Fri, 10 Mar 2023 11:22:51 +0900 Subject: [PATCH] refactor: dto class transformer (#45) --- .../modules/comments/comments.controller.ts | 10 ++---- .../comments/dto/comment-list-response.dto.ts | 3 +- .../src/modules/comments/dto/comment.dto.ts | 3 +- ...y.dtp.ts => get-search-posts-query.dto.ts} | 0 .../server/src/modules/posts/dto/index.ts | 2 +- .../modules/posts/dto/paginated-posts.dto.ts | 3 +- .../src/modules/posts/dto/post-stats.dto.ts | 3 +- .../server/src/modules/posts/dto/post.dto.ts | 3 +- .../src/modules/posts/dto/search-posts.dto.ts | 3 +- .../src/modules/posts/posts.controller.ts | 36 +++++-------------- .../src/modules/series/dto/series-post.dto.ts | 3 +- .../src/modules/series/dto/series.dto.ts | 3 +- .../src/modules/series/series.controller.ts | 24 ++++--------- .../server/src/modules/users/dto/user.dto.ts | 3 +- .../src/modules/users/users.controller.ts | 8 ++--- 15 files changed, 38 insertions(+), 69 deletions(-) rename packages/server/src/modules/posts/dto/{get-search-posts-query.dtp.ts => get-search-posts-query.dto.ts} (100%) diff --git a/packages/server/src/modules/comments/comments.controller.ts b/packages/server/src/modules/comments/comments.controller.ts index e5fc10c..5be07a2 100644 --- a/packages/server/src/modules/comments/comments.controller.ts +++ b/packages/server/src/modules/comments/comments.controller.ts @@ -4,15 +4,11 @@ import { GetCurrentUser } from '~/common/decorators'; import { ApiTags } from '@nestjs/swagger'; import { CommentDto, CreateCommentDto } from './dto'; import { plainToInstance } from 'class-transformer'; -import { SESService } from '~/providers/aws/ses/ses.service'; @ApiTags('comments') @Controller('comments') export class CommentsController { - constructor( - private readonly commentsService: CommentsService, - private readonly sesService: SESService, - ) {} + constructor(private readonly commentsService: CommentsService) {} @Post() async createComment( @@ -20,9 +16,7 @@ export class CommentsController { @GetCurrentUser('userId') userId: string, ): Promise { const comment = await this.commentsService.createComment(dto, userId); - return plainToInstance(CommentDto, comment, { - excludeExtraneousValues: true, - }); + return plainToInstance(CommentDto, comment); } @Delete(':id') diff --git a/packages/server/src/modules/comments/dto/comment-list-response.dto.ts b/packages/server/src/modules/comments/dto/comment-list-response.dto.ts index dee786e..6af27ca 100644 --- a/packages/server/src/modules/comments/dto/comment-list-response.dto.ts +++ b/packages/server/src/modules/comments/dto/comment-list-response.dto.ts @@ -1,6 +1,7 @@ -import { Expose, Type } from 'class-transformer'; +import { Exclude, Expose, Type } from 'class-transformer'; import { CommentDto } from './comment.dto'; +@Exclude() export class CommentListResponseDto { @Expose() @Type(() => CommentDto) diff --git a/packages/server/src/modules/comments/dto/comment.dto.ts b/packages/server/src/modules/comments/dto/comment.dto.ts index 6ecaa1c..d2fa4c6 100644 --- a/packages/server/src/modules/comments/dto/comment.dto.ts +++ b/packages/server/src/modules/comments/dto/comment.dto.ts @@ -1,6 +1,7 @@ -import { Expose, Type } from 'class-transformer'; +import { Exclude, Expose, Type } from 'class-transformer'; import { UserDto } from '~/modules/users/dto'; +@Exclude() export class CommentDto { @Expose() id: string; diff --git a/packages/server/src/modules/posts/dto/get-search-posts-query.dtp.ts b/packages/server/src/modules/posts/dto/get-search-posts-query.dto.ts similarity index 100% rename from packages/server/src/modules/posts/dto/get-search-posts-query.dtp.ts rename to packages/server/src/modules/posts/dto/get-search-posts-query.dto.ts diff --git a/packages/server/src/modules/posts/dto/index.ts b/packages/server/src/modules/posts/dto/index.ts index d8e54f0..84a8ce1 100644 --- a/packages/server/src/modules/posts/dto/index.ts +++ b/packages/server/src/modules/posts/dto/index.ts @@ -4,5 +4,5 @@ export * from './get-post-query.dto'; export * from './paginated-posts.dto'; export * from './post-stats.dto'; export * from './get-post-query.dto'; -export * from './get-search-posts-query.dtp'; +export * from './get-search-posts-query.dto'; export * from './search-posts.dto'; diff --git a/packages/server/src/modules/posts/dto/paginated-posts.dto.ts b/packages/server/src/modules/posts/dto/paginated-posts.dto.ts index f60c995..cecefc1 100644 --- a/packages/server/src/modules/posts/dto/paginated-posts.dto.ts +++ b/packages/server/src/modules/posts/dto/paginated-posts.dto.ts @@ -1,6 +1,7 @@ -import { Expose, Type } from 'class-transformer'; +import { Exclude, Expose, Type } from 'class-transformer'; import { PostDto } from './post.dto'; +@Exclude() export class PaginatedPostsDto { @Expose() @Type(() => PostDto) diff --git a/packages/server/src/modules/posts/dto/post-stats.dto.ts b/packages/server/src/modules/posts/dto/post-stats.dto.ts index 4851f92..1aac656 100644 --- a/packages/server/src/modules/posts/dto/post-stats.dto.ts +++ b/packages/server/src/modules/posts/dto/post-stats.dto.ts @@ -1,5 +1,6 @@ -import { Expose } from 'class-transformer'; +import { Exclude, Expose } from 'class-transformer'; +@Exclude() export class PostStatsDto { @Expose() likes: number; diff --git a/packages/server/src/modules/posts/dto/post.dto.ts b/packages/server/src/modules/posts/dto/post.dto.ts index 8388ab2..c5416da 100644 --- a/packages/server/src/modules/posts/dto/post.dto.ts +++ b/packages/server/src/modules/posts/dto/post.dto.ts @@ -1,8 +1,9 @@ -import { Expose, Type } from 'class-transformer'; +import { Exclude, Expose, Type } from 'class-transformer'; import { SeriesDto } from '~/modules/series/dto'; import { UserDto } from '~/modules/users/dto'; import { PostStatsDto } from './post-stats.dto'; +@Exclude() export class PostDto { @Expose() id: string; diff --git a/packages/server/src/modules/posts/dto/search-posts.dto.ts b/packages/server/src/modules/posts/dto/search-posts.dto.ts index 6461408..48cfc04 100644 --- a/packages/server/src/modules/posts/dto/search-posts.dto.ts +++ b/packages/server/src/modules/posts/dto/search-posts.dto.ts @@ -1,6 +1,7 @@ -import { Expose, Type } from 'class-transformer'; +import { Exclude, Expose, Type } from 'class-transformer'; import { PostDto } from './post.dto'; +@Exclude() export class SearchPostsDto { @Expose() @Type(() => PostDto) diff --git a/packages/server/src/modules/posts/posts.controller.ts b/packages/server/src/modules/posts/posts.controller.ts index 8149321..7b8d0b1 100644 --- a/packages/server/src/modules/posts/posts.controller.ts +++ b/packages/server/src/modules/posts/posts.controller.ts @@ -38,9 +38,7 @@ export class PostsController { @GetCurrentUser('userId') userId: string | null, ): Promise { const paginatedPosts = await this.postsService.getPosts(dto, userId); - return plainToInstance(PaginatedPostsDto, paginatedPosts, { - excludeExtraneousValues: true, - }); + return plainToInstance(PaginatedPostsDto, paginatedPosts); } @Public() @@ -55,9 +53,7 @@ export class PostsController { username, userId, ); - return plainToInstance(PaginatedPostsDto, paginatedPosts, { - excludeExtraneousValues: true, - }); + return plainToInstance(PaginatedPostsDto, paginatedPosts); } @Public() @@ -72,9 +68,7 @@ export class PostsController { slug, userId, }); - return plainToInstance(PostDto, post, { - excludeExtraneousValues: true, - }); + return plainToInstance(PostDto, post); } @Public() @@ -83,9 +77,7 @@ export class PostsController { @Query() dto: GetSearchPostsQueryDto, ): Promise { const posts = await this.postsService.getSearchPosts(dto); - return plainToInstance(SearchPostsDto, posts, { - excludeExtraneousValues: true, - }); + return plainToInstance(SearchPostsDto, posts); } @Public() @@ -95,9 +87,7 @@ export class PostsController { @Param('id') id: string, ) { const post = await this.postsService.getPost(id, userId); - return plainToInstance(PostDto, post, { - excludeExtraneousValues: true, - }); + return plainToInstance(PostDto, post); } @Public() @@ -107,9 +97,7 @@ export class PostsController { @GetCurrentUser('userId') userId: string | null, ): Promise { const commentList = await this.postsService.getCommentList(id, userId); - return plainToInstance(CommentListResponseDto, commentList, { - excludeExtraneousValues: true, - }); + return plainToInstance(CommentListResponseDto, commentList); } @Post() @@ -119,9 +107,7 @@ export class PostsController { ): Promise { const post = await this.postsService.createPost(dto, user); - return plainToInstance(PostDto, post, { - excludeExtraneousValues: true, - }); + return plainToInstance(PostDto, post); } @Post(':postId/likes') @@ -130,9 +116,7 @@ export class PostsController { @GetCurrentUser('userId') userId: string, ): Promise { const postStats = await this.postsService.likePost({ postId, userId }); - return plainToInstance(PostStatsDto, postStats, { - excludeExtraneousValues: true, - }); + return plainToInstance(PostStatsDto, postStats); } @Delete(':postId/likes') @@ -141,9 +125,7 @@ export class PostsController { @GetCurrentUser('userId') userId: string, ): Promise { const postStats = await this.postsService.unlikePost({ postId, userId }); - return plainToInstance(PostStatsDto, postStats, { - excludeExtraneousValues: true, - }); + return plainToInstance(PostStatsDto, postStats); } @Delete(':postId') diff --git a/packages/server/src/modules/series/dto/series-post.dto.ts b/packages/server/src/modules/series/dto/series-post.dto.ts index 1c66677..44d4d5f 100644 --- a/packages/server/src/modules/series/dto/series-post.dto.ts +++ b/packages/server/src/modules/series/dto/series-post.dto.ts @@ -1,5 +1,6 @@ -import { Expose } from 'class-transformer'; +import { Exclude, Expose } from 'class-transformer'; +@Exclude() export class SeriesPostDto { @Expose() id: string; diff --git a/packages/server/src/modules/series/dto/series.dto.ts b/packages/server/src/modules/series/dto/series.dto.ts index ae7cfc0..6e0a343 100644 --- a/packages/server/src/modules/series/dto/series.dto.ts +++ b/packages/server/src/modules/series/dto/series.dto.ts @@ -1,6 +1,7 @@ -import { Expose, Type } from 'class-transformer'; +import { Exclude, Expose, Type } from 'class-transformer'; import { SeriesPostDto } from './series-post.dto'; +@Exclude() export class SeriesDto { @Expose() id: string; diff --git a/packages/server/src/modules/series/series.controller.ts b/packages/server/src/modules/series/series.controller.ts index e13c811..cd249c3 100644 --- a/packages/server/src/modules/series/series.controller.ts +++ b/packages/server/src/modules/series/series.controller.ts @@ -26,9 +26,7 @@ export class SeriesController { @Param('username') username: string, ): Promise { const seriesList = await this.seriesService.getUserSeriesList(username); - return plainToInstance(SeriesDto, seriesList, { - excludeExtraneousValues: true, - }); + return plainToInstance(SeriesDto, seriesList); } // Get a specific series by name. @@ -42,9 +40,7 @@ export class SeriesController { username, seriesName, ); - return plainToInstance(SeriesDto, series, { - excludeExtraneousValues: true, - }); + return plainToInstance(SeriesDto, series); } @Post() @@ -53,9 +49,7 @@ export class SeriesController { @GetCurrentUser('userId') userId: string, ): Promise { const series = await this.seriesService.createSeries(dto, userId); - return plainToInstance(SeriesDto, series, { - excludeExtraneousValues: true, - }); + return plainToInstance(SeriesDto, series); } @Delete(':seriesId') @@ -64,9 +58,7 @@ export class SeriesController { @GetCurrentUser('userId') userId: string, ): Promise { const series = await this.seriesService.deleteSeries(seriesId, userId); - return plainToInstance(SeriesDto, series, { - excludeExtraneousValues: true, - }); + return plainToInstance(SeriesDto, series); } @Post(':seriesId/postId/:postId') @@ -80,9 +72,7 @@ export class SeriesController { postId, userId, ); - return plainToInstance(SeriesPostDto, seriesPost, { - excludeExtraneousValues: true, - }); + return plainToInstance(SeriesPostDto, seriesPost); } // patch는 일부만 수정할 때 사용, put은 전체를 수정할 때 사용 @@ -98,8 +88,6 @@ export class SeriesController { userId, ); - return plainToInstance(SeriesDto, editedSeries, { - excludeExtraneousValues: true, - }); + return plainToInstance(SeriesDto, editedSeries); } } diff --git a/packages/server/src/modules/users/dto/user.dto.ts b/packages/server/src/modules/users/dto/user.dto.ts index 03eb55a..d280806 100644 --- a/packages/server/src/modules/users/dto/user.dto.ts +++ b/packages/server/src/modules/users/dto/user.dto.ts @@ -1,5 +1,6 @@ -import { Expose } from 'class-transformer'; +import { Exclude, Expose } from 'class-transformer'; +@Exclude() export class UserDto { @Expose() id: string; diff --git a/packages/server/src/modules/users/users.controller.ts b/packages/server/src/modules/users/users.controller.ts index 29254ac..aca9271 100644 --- a/packages/server/src/modules/users/users.controller.ts +++ b/packages/server/src/modules/users/users.controller.ts @@ -19,9 +19,7 @@ export class UsersController { return null; } const user = await this.usersService.getUserById(userId); - return plainToInstance(UserDto, user, { - excludeExtraneousValues: true, - }); + return plainToInstance(UserDto, user); } @Public() @@ -30,9 +28,7 @@ export class UsersController { @Param('username') username: string, ): Promise { const user = await this.usersService.getUserByUsername(username); - return plainToInstance(UserDto, user, { - excludeExtraneousValues: true, - }); + return plainToInstance(UserDto, user); } @Patch('email')