Skip to content

Commit

Permalink
refactor: dto class transformer (#45)
Browse files Browse the repository at this point in the history
  • Loading branch information
alstn113 committed Mar 10, 2023
1 parent d35affa commit 3cd9380
Show file tree
Hide file tree
Showing 15 changed files with 38 additions and 69 deletions.
10 changes: 2 additions & 8 deletions packages/server/src/modules/comments/comments.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,19 @@ 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(
@Body() dto: CreateCommentDto,
@GetCurrentUser('userId') userId: string,
): Promise<CommentDto> {
const comment = await this.commentsService.createComment(dto, userId);
return plainToInstance(CommentDto, comment, {
excludeExtraneousValues: true,
});
return plainToInstance(CommentDto, comment);
}

@Delete(':id')
Expand Down
Original file line number Diff line number Diff line change
@@ -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)
Expand Down
3 changes: 2 additions & 1 deletion packages/server/src/modules/comments/dto/comment.dto.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
2 changes: 1 addition & 1 deletion packages/server/src/modules/posts/dto/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
3 changes: 2 additions & 1 deletion packages/server/src/modules/posts/dto/paginated-posts.dto.ts
Original file line number Diff line number Diff line change
@@ -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)
Expand Down
3 changes: 2 additions & 1 deletion packages/server/src/modules/posts/dto/post-stats.dto.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Expose } from 'class-transformer';
import { Exclude, Expose } from 'class-transformer';

@Exclude()
export class PostStatsDto {
@Expose()
likes: number;
Expand Down
3 changes: 2 additions & 1 deletion packages/server/src/modules/posts/dto/post.dto.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
3 changes: 2 additions & 1 deletion packages/server/src/modules/posts/dto/search-posts.dto.ts
Original file line number Diff line number Diff line change
@@ -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)
Expand Down
36 changes: 9 additions & 27 deletions packages/server/src/modules/posts/posts.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,7 @@ export class PostsController {
@GetCurrentUser('userId') userId: string | null,
): Promise<PaginatedPostsDto> {
const paginatedPosts = await this.postsService.getPosts(dto, userId);
return plainToInstance(PaginatedPostsDto, paginatedPosts, {
excludeExtraneousValues: true,
});
return plainToInstance(PaginatedPostsDto, paginatedPosts);
}

@Public()
Expand All @@ -55,9 +53,7 @@ export class PostsController {
username,
userId,
);
return plainToInstance(PaginatedPostsDto, paginatedPosts, {
excludeExtraneousValues: true,
});
return plainToInstance(PaginatedPostsDto, paginatedPosts);
}

@Public()
Expand All @@ -72,9 +68,7 @@ export class PostsController {
slug,
userId,
});
return plainToInstance(PostDto, post, {
excludeExtraneousValues: true,
});
return plainToInstance(PostDto, post);
}

@Public()
Expand All @@ -83,9 +77,7 @@ export class PostsController {
@Query() dto: GetSearchPostsQueryDto,
): Promise<SearchPostsDto> {
const posts = await this.postsService.getSearchPosts(dto);
return plainToInstance(SearchPostsDto, posts, {
excludeExtraneousValues: true,
});
return plainToInstance(SearchPostsDto, posts);
}

@Public()
Expand All @@ -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()
Expand All @@ -107,9 +97,7 @@ export class PostsController {
@GetCurrentUser('userId') userId: string | null,
): Promise<CommentListResponseDto> {
const commentList = await this.postsService.getCommentList(id, userId);
return plainToInstance(CommentListResponseDto, commentList, {
excludeExtraneousValues: true,
});
return plainToInstance(CommentListResponseDto, commentList);
}

@Post()
Expand All @@ -119,9 +107,7 @@ export class PostsController {
): Promise<PostDto> {
const post = await this.postsService.createPost(dto, user);

return plainToInstance(PostDto, post, {
excludeExtraneousValues: true,
});
return plainToInstance(PostDto, post);
}

@Post(':postId/likes')
Expand All @@ -130,9 +116,7 @@ export class PostsController {
@GetCurrentUser('userId') userId: string,
): Promise<PostStatsDto> {
const postStats = await this.postsService.likePost({ postId, userId });
return plainToInstance(PostStatsDto, postStats, {
excludeExtraneousValues: true,
});
return plainToInstance(PostStatsDto, postStats);
}

@Delete(':postId/likes')
Expand All @@ -141,9 +125,7 @@ export class PostsController {
@GetCurrentUser('userId') userId: string,
): Promise<PostStatsDto> {
const postStats = await this.postsService.unlikePost({ postId, userId });
return plainToInstance(PostStatsDto, postStats, {
excludeExtraneousValues: true,
});
return plainToInstance(PostStatsDto, postStats);
}

@Delete(':postId')
Expand Down
3 changes: 2 additions & 1 deletion packages/server/src/modules/series/dto/series-post.dto.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Expose } from 'class-transformer';
import { Exclude, Expose } from 'class-transformer';

@Exclude()
export class SeriesPostDto {
@Expose()
id: string;
Expand Down
3 changes: 2 additions & 1 deletion packages/server/src/modules/series/dto/series.dto.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
24 changes: 6 additions & 18 deletions packages/server/src/modules/series/series.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,7 @@ export class SeriesController {
@Param('username') username: string,
): Promise<SeriesDto[]> {
const seriesList = await this.seriesService.getUserSeriesList(username);
return plainToInstance(SeriesDto, seriesList, {
excludeExtraneousValues: true,
});
return plainToInstance(SeriesDto, seriesList);
}

// Get a specific series by name.
Expand All @@ -42,9 +40,7 @@ export class SeriesController {
username,
seriesName,
);
return plainToInstance(SeriesDto, series, {
excludeExtraneousValues: true,
});
return plainToInstance(SeriesDto, series);
}

@Post()
Expand All @@ -53,9 +49,7 @@ export class SeriesController {
@GetCurrentUser('userId') userId: string,
): Promise<SeriesDto> {
const series = await this.seriesService.createSeries(dto, userId);
return plainToInstance(SeriesDto, series, {
excludeExtraneousValues: true,
});
return plainToInstance(SeriesDto, series);
}

@Delete(':seriesId')
Expand All @@ -64,9 +58,7 @@ export class SeriesController {
@GetCurrentUser('userId') userId: string,
): Promise<SeriesDto> {
const series = await this.seriesService.deleteSeries(seriesId, userId);
return plainToInstance(SeriesDto, series, {
excludeExtraneousValues: true,
});
return plainToInstance(SeriesDto, series);
}

@Post(':seriesId/postId/:postId')
Expand All @@ -80,9 +72,7 @@ export class SeriesController {
postId,
userId,
);
return plainToInstance(SeriesPostDto, seriesPost, {
excludeExtraneousValues: true,
});
return plainToInstance(SeriesPostDto, seriesPost);
}

// patch는 일부만 수정할 때 사용, put은 전체를 수정할 때 사용
Expand All @@ -98,8 +88,6 @@ export class SeriesController {
userId,
);

return plainToInstance(SeriesDto, editedSeries, {
excludeExtraneousValues: true,
});
return plainToInstance(SeriesDto, editedSeries);
}
}
3 changes: 2 additions & 1 deletion packages/server/src/modules/users/dto/user.dto.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Expose } from 'class-transformer';
import { Exclude, Expose } from 'class-transformer';

@Exclude()
export class UserDto {
@Expose()
id: string;
Expand Down
8 changes: 2 additions & 6 deletions packages/server/src/modules/users/users.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -30,9 +28,7 @@ export class UsersController {
@Param('username') username: string,
): Promise<UserDto> {
const user = await this.usersService.getUserByUsername(username);
return plainToInstance(UserDto, user, {
excludeExtraneousValues: true,
});
return plainToInstance(UserDto, user);
}

@Patch('email')
Expand Down

0 comments on commit 3cd9380

Please sign in to comment.