@@ -9,7 +9,6 @@ export class LeaderboardRepository {
99 async getUserLeaderboard ( sort : UserLeaderboardSortType , dateRange : number , limit : number ) {
1010 try {
1111 const cteQuery = this . buildLeaderboardCteQuery ( ) ;
12- const sortCol = sort === 'postCount' ? 'post_diff' : sort === 'likeCount' ? 'like_diff' : 'view_diff' ;
1312
1413 const query = `
1514 ${ cteQuery }
@@ -28,7 +27,7 @@ export class LeaderboardRepository {
2827 LEFT JOIN start_stats ss ON ss.post_id = p.id
2928 WHERE u.email IS NOT NULL
3029 GROUP BY u.id, u.email
31- ORDER BY ${ sortCol } DESC
30+ ORDER BY ${ this . SORT_COL_MAPPING [ sort ] } DESC
3231 LIMIT $2;
3332 ` ;
3433 const result = await this . pool . query ( query , [ dateRange , limit ] ) ;
@@ -43,7 +42,6 @@ export class LeaderboardRepository {
4342 async getPostLeaderboard ( sort : PostLeaderboardSortType , dateRange : number , limit : number ) {
4443 try {
4544 const cteQuery = this . buildLeaderboardCteQuery ( ) ;
46- const sortCol = sort === 'viewCount' ? 'view_diff' : 'like_diff' ;
4745
4846 const query = `
4947 ${ cteQuery }
@@ -60,7 +58,7 @@ export class LeaderboardRepository {
6058 LEFT JOIN today_stats ts ON ts.post_id = p.id
6159 LEFT JOIN start_stats ss ON ss.post_id = p.id
6260 WHERE p.is_active = true
63- ORDER BY ${ sortCol } DESC
61+ ORDER BY ${ this . SORT_COL_MAPPING [ sort ] } DESC
6462 LIMIT $2;
6563 ` ;
6664 const result = await this . pool . query ( query , [ dateRange , limit ] ) ;
@@ -96,4 +94,10 @@ export class LeaderboardRepository {
9694 )
9795 ` ;
9896 }
97+
98+ private readonly SORT_COL_MAPPING = {
99+ viewCount : 'view_diff' ,
100+ likeCount : 'like_diff' ,
101+ postCount : 'post_diff' ,
102+ } as const ;
99103}
0 commit comments