-
Notifications
You must be signed in to change notification settings - Fork 0
[25.06.03 / TASK-191]Feature - 전체 조회수/좋아요/게시글 디테일 API 대응개발 #33
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 6 commits
0334814
229396e
a56ce92
b25705c
6a63e00
3a8a67e
1e73b84
eb05ca8
30481cf
19d9e7c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,28 +1,19 @@ | ||
import { NextFunction, Request, RequestHandler, Response } from 'express'; | ||
import logger from '@/configs/logger.config'; | ||
import { NotiService } from "@/services/noti.service"; | ||
import { NotiPostsResponseDto } from "@/types/dto/responses/notiResponse.type"; | ||
import { NotiService } from '@/services/noti.service'; | ||
import { NotiPostsResponseDto } from '@/types/dto/responses/notiResponse.type'; | ||
|
||
export class NotiController { | ||
constructor(private notiService: NotiService) { } | ||
constructor(private notiService: NotiService) {} | ||
|
||
getAllNotiPosts: RequestHandler = async ( | ||
req: Request, | ||
res: Response<NotiPostsResponseDto>, | ||
next: NextFunction, | ||
) => { | ||
getAllNotiPosts: RequestHandler = async (req: Request, res: Response<NotiPostsResponseDto>, next: NextFunction) => { | ||
try { | ||
const result = await this.notiService.getAllNotiPosts(); | ||
const response = new NotiPostsResponseDto( | ||
true, | ||
'전체 noti post 조회에 성공하였습니다.', | ||
{ posts: result }, | ||
null, | ||
); | ||
const response = new NotiPostsResponseDto(true, '전체 noti post 조회에 성공하였습니다.', { posts: result }, null); | ||
res.status(200).json(response); | ||
} catch (error) { | ||
logger.error('전체 조회 실패:', error); | ||
next(error); | ||
} | ||
}; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import { NextFunction, Request, RequestHandler, Response } from 'express'; | ||
import logger from '@/configs/logger.config'; | ||
import { BadRequestError } from '@/exception'; | ||
import { GetTotalStatsQuery, TotalStatsResponseDto } from '@/types'; | ||
import { TotalStatsService } from '@/services/totalStats.service'; | ||
|
||
export class TotalStatsController { | ||
constructor(private totalStatsService: TotalStatsService) {} | ||
|
||
getTotalStats: RequestHandler = async ( | ||
req: Request<object, object, object, GetTotalStatsQuery>, | ||
res: Response<TotalStatsResponseDto>, | ||
next: NextFunction, | ||
) => { | ||
try { | ||
const { id } = req.user; | ||
const { period, type } = req.query; | ||
|
||
// 미들웨어에서 GetTotalStatsQueryDto 에 의해 걸리는데 런타임과 IDE 에서 구분을 못함, 이를 위해 추가 | ||
if (!type) throw new BadRequestError('type 파라미터가 필요합니다.'); | ||
|
||
const stats = await this.totalStatsService.getTotalStats(id, period, type); | ||
const message = this.totalStatsService.getSuccessMessage(type); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 요 부분 서비스단에서 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 와 같은 맥락입니다! optional 빼면 여기 Type 체크를 제네릭부터 다시 시작하거나 강제 casting 을 해야 해서요! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 넵 그 부분 이해했습니다! export class TotalStatsService {
async getTotalStats(
userId: number,
period: TotalStatsPeriod = 7, // 기존에도 있었던 부분
type: TotalStatsType = 'view',
): Promise<TotalStatsItem[]> {}
} 이렇게요! 그럼 상위에서도 TS에러가 뜨지 않아서요. (확인해보니 리더보드도 이런 식으로 우회되었네요.) 현우님께서 편하신 방향으로 🙏 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 오케이! 이왕 default 값 넣어버린거, 서비스 arguments 에 모두 default 값 넣는 걸로 update 할게요! 좋은 지적 너무 감사해요! |
||
|
||
const response = new TotalStatsResponseDto(true, message, stats, null); | ||
|
||
res.status(200).json(response); | ||
} catch (error) { | ||
logger.error('전체 통계 조회 실패:', error); | ||
next(error); | ||
} | ||
}; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
이 부분이 정확히 어떤 것을 의미하는지 조금 이해하기 어렵네요..
정확히 무슨 이유로 추가해주신걸까요?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
기준님도 아시다시피 type, interface (typscript) 문법들은 run-time 에서는 없어지잖아요~
그렇기때문에 벨리데이션은 무조건 DTO 로 해야합니다.
validateRequestDto(GetTotalStatsQueryDto, 'query')
하지만 컨트롤러의 Request 객체는 class type 선언을 쓰기엔 복잡하죠(제네릭부터 선언해야할 것이 엄청 많음). 그래서 interface 로 사용하죠! (그래서 내용이 같은거, express 가 ts 랑 사실 그렇게 궁합이 맞냐라는 질문에 역시 세모인 이유)
그래서 IDE와 린팅은 이를 인터페이스 타입이라 인지하고 옵셔널 값으로 인지해버려요. (더욱이 require 로 해버리면 제네릭을 써야하는 상황) 그래서 이 라인이 필요합니다!