|
1 | 1 | import { NextFunction, Request, RequestHandler, Response } from 'express'; |
2 | | -import logger from '../configs/logger.config'; |
3 | | -import { PostService } from '../services/post.service'; |
4 | | -import { GetAllPostsQuery, PostResponse } from '../types'; |
5 | | -import { GetPostQuery } from '../types/requests/getPostQuery.type'; |
| 2 | +import logger from '@/configs/logger.config'; |
| 3 | +import { PostService } from '@/services/post.service'; |
| 4 | +import { |
| 5 | + GetAllPostsQuery, |
| 6 | + PostsResponseDto, |
| 7 | + PostResponseDto, |
| 8 | + GetPostQuery, |
| 9 | + PostParam, |
| 10 | + PostStatisticsResponseDto, |
| 11 | +} from '@/types'; |
6 | 12 |
|
7 | 13 | export class PostController { |
8 | 14 | constructor(private postService: PostService) {} |
9 | 15 |
|
10 | | - private validateQueryParams(query: GetAllPostsQuery): { |
11 | | - cursor: string | undefined; |
12 | | - sort: string; |
13 | | - isAsc: boolean; |
14 | | - } { |
15 | | - return { |
16 | | - cursor: query.cursor, |
17 | | - sort: query.sort || '', |
18 | | - isAsc: query.asc === 'true', |
19 | | - }; |
20 | | - } |
21 | | - private validateQueryParams2(query: Partial<GetPostQuery>): { |
22 | | - start: string; |
23 | | - end: string; |
24 | | - } { |
25 | | - return { |
26 | | - start: query.start || '', |
27 | | - end: query.end || '', |
28 | | - }; |
29 | | - } |
30 | 16 | getAllPost: RequestHandler = async ( |
31 | 17 | req: Request<object, object, object, GetAllPostsQuery>, |
32 | | - res: Response<PostResponse>, |
| 18 | + res: Response<PostsResponseDto>, |
33 | 19 | next: NextFunction, |
34 | 20 | ) => { |
35 | 21 | try { |
36 | 22 | const { id } = req.user; |
37 | | - const { cursor, sort, isAsc } = this.validateQueryParams(req.query); |
| 23 | + const { cursor, sort, asc } = req.query; |
38 | 24 |
|
39 | | - const result = await this.postService.getAllposts(id, cursor, sort, isAsc); |
| 25 | + const result = await this.postService.getAllposts(id, cursor, sort, asc); |
40 | 26 |
|
41 | | - res.status(200).json({ |
42 | | - success: true, |
43 | | - message: 'post 전체 조회에 성공하였습니다.', |
44 | | - data: { |
45 | | - nextCursor: result.nextCursor, |
46 | | - posts: result.posts, |
47 | | - }, |
48 | | - error: null, |
49 | | - }); |
| 27 | + const response = new PostsResponseDto( |
| 28 | + true, |
| 29 | + '전체 post 조회에 성공하였습니다.', |
| 30 | + { nextCursor: result.nextCursor, posts: result.posts }, |
| 31 | + null, |
| 32 | + ); |
| 33 | + |
| 34 | + res.status(200).json(response); |
50 | 35 | } catch (error) { |
51 | 36 | logger.error('전체 조회 실패:', error); |
52 | 37 | next(error); |
53 | 38 | } |
54 | 39 | }; |
55 | 40 |
|
56 | | - getAllPostStatistics: RequestHandler = async (req: Request, res: Response, next: NextFunction) => { |
| 41 | + getAllPostStatistics: RequestHandler = async ( |
| 42 | + req: Request, |
| 43 | + res: Response<PostStatisticsResponseDto>, |
| 44 | + next: NextFunction, |
| 45 | + ) => { |
57 | 46 | try { |
58 | 47 | const { id } = req.user; |
59 | 48 |
|
60 | | - const result = await this.postService.getAllPostStatistics(id); |
| 49 | + const stats = await this.postService.getAllPostStatistics(id); |
61 | 50 | const totalPostCount = await this.postService.getTotalPostCounts(id); |
62 | 51 |
|
63 | | - res.status(200).json({ |
64 | | - success: true, |
65 | | - message: 'post 전체 통계 조회에 성공하였습니다.', |
66 | | - data: { totalPostCount, stats: result }, |
67 | | - error: null, |
68 | | - }); |
| 52 | + const response = new PostStatisticsResponseDto( |
| 53 | + true, |
| 54 | + '전체 post 통계 조회에 성공하였습니다.', |
| 55 | + { totalPostCount, stats }, |
| 56 | + null, |
| 57 | + ); |
| 58 | + |
| 59 | + res.status(200).json(response); |
69 | 60 | } catch (error) { |
70 | 61 | logger.error('전체 통계 조회 실패:', error); |
71 | 62 | next(error); |
72 | 63 | } |
73 | 64 | }; |
74 | 65 |
|
75 | | - getPost: RequestHandler = async (req: Request, res: Response, next: NextFunction) => { |
| 66 | + getPost: RequestHandler = async ( |
| 67 | + req: Request<PostParam, object, object, GetPostQuery>, |
| 68 | + res: Response<PostResponseDto>, |
| 69 | + next: NextFunction, |
| 70 | + ) => { |
76 | 71 | try { |
77 | | - const postId = parseInt(req.params.postId); |
78 | | - const { start, end } = this.validateQueryParams2(req.query); |
| 72 | + const postId = Number(req.params.postId); |
| 73 | + const { start, end } = req.query; |
| 74 | + |
79 | 75 | const post = await this.postService.getPost(postId, start, end); |
80 | | - res.status(200).json({ |
81 | | - success: true, |
82 | | - message: 'post 단건 조회에 성공하였습니다', |
83 | | - data: { post }, |
84 | | - error: null, |
85 | | - }); |
| 76 | + |
| 77 | + const response = new PostResponseDto(true, '단건 post 조회에 성공하였습니다.', { post }, null); |
| 78 | + |
| 79 | + res.status(200).json(response); |
86 | 80 | } catch (error) { |
87 | 81 | logger.error('단건 조회 실패 : ', error); |
88 | 82 | next(error); |
|
0 commit comments