Skip to content

Commit 7245acc

Browse files
committed
hotfix: 날짜 조회 SQL 쿼리의 치명적인 이슈 핸들링
1 parent 4a73e7f commit 7245acc

File tree

4 files changed

+36
-25
lines changed

4 files changed

+36
-25
lines changed

src/controllers/post.controller.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import {
1313
export class PostController {
1414
constructor(private postService: PostService) {}
1515

16-
getAllPost: RequestHandler = async (
16+
getAllPosts: RequestHandler = async (
1717
req: Request<object, object, object, GetAllPostsQuery>,
1818
res: Response<PostsResponseDto>,
1919
next: NextFunction,
@@ -38,15 +38,15 @@ export class PostController {
3838
}
3939
};
4040

41-
getAllPostStatistics: RequestHandler = async (
41+
getAllPostsStatistics: RequestHandler = async (
4242
req: Request,
4343
res: Response<PostStatisticsResponseDto>,
4444
next: NextFunction,
4545
) => {
4646
try {
4747
const { id } = req.user;
4848

49-
const stats = await this.postService.getAllPostStatistics(id);
49+
const stats = await this.postService.getAllPostsStatistics(id);
5050
const totalPostCount = await this.postService.getTotalPostCounts(id);
5151

5252
const response = new PostStatisticsResponseDto(

src/repositories/post.repository.ts

Lines changed: 28 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import logger from '@/configs/logger.config';
33
import { DBError } from '@/exception';
44

55
export class PostRepository {
6-
constructor(private pool: Pool) {}
6+
constructor(private pool: Pool) { }
77

88
async findPostsByUserId(userId: number, cursor?: string, sort?: string, isAsc?: boolean, limit: number = 15) {
99
try {
@@ -162,28 +162,39 @@ export class PostRepository {
162162

163163
async findPostByPostId(postId: number, start?: string, end?: string) {
164164
try {
165-
let query = `
166-
SELECT
167-
(pds.date AT TIME ZONE 'Asia/Seoul') AT TIME ZONE 'UTC' AS date,
168-
pds.daily_view_count,
169-
pds.daily_like_count
170-
FROM posts_postdailystatistics pds
171-
WHERE pds.post_id = $1
172-
ORDER BY pds.date ASC
165+
// 기본 쿼리 부분
166+
const baseQuery = `
167+
SELECT
168+
(pds.date AT TIME ZONE 'Asia/Seoul') AT TIME ZONE 'UTC' AS date,
169+
pds.daily_view_count,
170+
pds.daily_like_count
171+
FROM posts_postdailystatistics pds
172+
WHERE pds.post_id = $1
173173
`;
174174

175-
const values: (number | string)[] = [postId];
175+
// 날짜 필터링 조건 구성
176+
const dateFilterQuery = (start && end)
177+
? `
178+
AND (pds.date AT TIME ZONE 'Asia/Seoul' AT TIME ZONE 'UTC')::date >= ($2 AT TIME ZONE 'Asia/Seoul' AT TIME ZONE 'UTC')::date
179+
AND (pds.date AT TIME ZONE 'Asia/Seoul' AT TIME ZONE 'UTC')::date <= ($3 AT TIME ZONE 'Asia/Seoul' AT TIME ZONE 'UTC')::date
180+
`
181+
: '';
176182

177-
if (start && end) {
178-
query += ` AND (pds.date AT TIME ZONE 'Asia/Seoul' AT TIME ZONE 'UTC')::date >= ($2 AT TIME ZONE 'Asia/Seoul' AT TIME ZONE 'UTC')::date
179-
AND (pds.date AT TIME ZONE 'Asia/Seoul' AT TIME ZONE 'UTC')::date <= ($3 AT TIME ZONE 'Asia/Seoul' AT TIME ZONE 'UTC')::date`;
180-
values.push(start, end);
181-
}
183+
// 정렬 조건 추가
184+
const orderByQuery = `ORDER BY pds.date ASC`;
182185

183-
const result = await this.pool.query(query, values);
186+
// 최종 쿼리 조합
187+
const fullQuery = [baseQuery, dateFilterQuery, orderByQuery].join(' ');
188+
189+
// 파라미터 배열 구성
190+
const queryParams: Array<number | string> = [postId];
191+
if (start && end) queryParams.push(start, end);
192+
193+
// 쿼리 실행
194+
const result = await this.pool.query(fullQuery, queryParams);
184195
return result.rows;
185196
} catch (error) {
186-
logger.error('Post Repo findPostByPostId error : ', error);
197+
logger.error('Post Repo findPostByPostId error:', error);
187198
throw new DBError('단건 post 조회 중 문제가 발생했습니다.');
188199
}
189200
}

src/routes/post.router.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ router.get(
4949
'/posts',
5050
authMiddleware.verify,
5151
validateRequestDto(GetAllPostsQueryDto, 'query'),
52-
postController.getAllPost,
52+
postController.getAllPosts,
5353
);
5454

5555
/**
@@ -69,7 +69,7 @@ router.get(
6969
* '500':
7070
* description: 서버 오류 / 데이터 베이스 조회 오류
7171
*/
72-
router.get('/posts-stats', authMiddleware.verify, postController.getAllPostStatistics);
72+
router.get('/posts-stats', authMiddleware.verify, postController.getAllPostsStatistics);
7373

7474
/**
7575
* @swagger

src/services/post.service.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,12 @@ export class PostService {
2626
nextCursor: result.nextCursor,
2727
};
2828
} catch (error) {
29-
logger.error('PostService getAllpost error : ', error);
29+
logger.error('PostService getAllposts error : ', error);
3030
throw error;
3131
}
3232
}
3333

34-
async getAllPostStatistics(userId: number) {
34+
async getAllPostsStatistics(userId: number) {
3535
try {
3636
const postsStatistics = await this.postRepo.getYesterdayAndTodayViewLikeStats(userId);
3737

@@ -45,7 +45,7 @@ export class PostService {
4545

4646
return transformedStatistics;
4747
} catch (error) {
48-
logger.error('PostService getAllPostStatistics error : ', error);
48+
logger.error('PostService getAllPostsStatistics error : ', error);
4949
throw error;
5050
}
5151
}

0 commit comments

Comments
 (0)