Skip to content

Commit 5b55ef4

Browse files
committed
Removed forced limits from api calls
1 parent c013d17 commit 5b55ef4

File tree

5 files changed

+56
-153
lines changed

5 files changed

+56
-153
lines changed

src/api/routers/commands/stats.router.ts

Lines changed: 3 additions & 113 deletions
Original file line numberDiff line numberDiff line change
@@ -75,12 +75,11 @@ statsRouter.openapi(
7575
async (c) => {
7676
const { queue_id } = c.req.valid('param')
7777
const { limit } = c.req.valid('query')
78-
const finalLimit = limit || 100
7978

8079
try {
8180
const leaderboard = await COMMAND_HANDLERS.STATS.GET_LEADERBOARD(
8281
queue_id,
83-
finalLimit,
82+
limit,
8483
)
8584

8685
return c.json(
@@ -178,12 +177,11 @@ statsRouter.openapi(
178177
async (c) => {
179178
const { queue_id } = c.req.valid('param')
180179
const { limit } = c.req.valid('query')
181-
const finalLimit = limit || 50
182180

183181
try {
184182
const matches = await COMMAND_HANDLERS.STATS.GET_OVERALL_HISTORY(
185183
queue_id,
186-
finalLimit,
184+
limit,
187185
)
188186

189187
return c.json(
@@ -284,120 +282,12 @@ statsRouter.openapi(
284282
async (c) => {
285283
const { user_id, queue_id } = c.req.valid('param')
286284
const { limit } = c.req.valid('query')
287-
const finalLimit = limit || 50
288285

289286
try {
290287
const matches = await COMMAND_HANDLERS.STATS.GET_MATCH_HISTORY(
291288
user_id,
292289
queue_id,
293-
finalLimit,
294-
)
295-
296-
return c.json(
297-
{
298-
matches,
299-
},
300-
200,
301-
)
302-
} catch (error) {
303-
console.error('Error fetching match history:', error)
304-
return c.json(
305-
{
306-
error: 'Internal server error',
307-
},
308-
500,
309-
)
310-
}
311-
},
312-
)
313-
314-
statsRouter.openapi(
315-
createRoute({
316-
method: 'get',
317-
path: '/history/{user_id}/{queue_id}',
318-
description: 'Get match history for a player in a specific queue.',
319-
request: {
320-
params: z.object({
321-
user_id: z.string().openapi({
322-
param: {
323-
name: 'user_id',
324-
in: 'path',
325-
},
326-
example: '123456789012345678',
327-
}),
328-
queue_id: z
329-
.string()
330-
.regex(/^\d+$/)
331-
.transform(Number)
332-
.openapi({
333-
param: {
334-
name: 'queue_id',
335-
in: 'path',
336-
},
337-
example: '1',
338-
}),
339-
}),
340-
query: z.object({
341-
limit: z
342-
.string()
343-
.regex(/^\d+$/)
344-
.transform(Number)
345-
.optional()
346-
.openapi({
347-
param: {
348-
name: 'limit',
349-
in: 'query',
350-
},
351-
example: '10',
352-
}),
353-
}),
354-
},
355-
responses: {
356-
200: {
357-
content: {
358-
'application/json': {
359-
schema: z.object({
360-
matches: z.array(
361-
z.object({
362-
match_id: z.number(),
363-
won: z.boolean(),
364-
elo_change: z.number().nullable(),
365-
team: z.number().nullable(),
366-
deck: z.string().nullable(),
367-
stake: z.string().nullable(),
368-
best_of_3: z.boolean(),
369-
best_of_5: z.boolean(),
370-
created_at: z.string(),
371-
winning_team: z.number().nullable(),
372-
}),
373-
),
374-
}),
375-
},
376-
},
377-
description: 'Match history retrieved successfully.',
378-
},
379-
500: {
380-
content: {
381-
'application/json': {
382-
schema: z.object({
383-
error: z.string(),
384-
}),
385-
},
386-
},
387-
description: 'Internal server error.',
388-
},
389-
},
390-
}),
391-
async (c) => {
392-
const { user_id, queue_id } = c.req.valid('param')
393-
const { limit } = c.req.valid('query')
394-
const finalLimit = limit || 50
395-
396-
try {
397-
const matches = await COMMAND_HANDLERS.STATS.GET_MATCH_HISTORY(
398-
user_id,
399-
queue_id,
400-
finalLimit,
290+
limit,
401291
)
402292

403293
return c.json(

src/command-handlers/stats/getLeaderboard.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,12 @@ export type LeaderboardEntry = {
1616
* Gets leaderboard data for a specific queue.
1717
*
1818
* @param {number} queueId - The queue ID to fetch leaderboard for.
19-
* @param {number} limit - Maximum number of entries to return.
19+
* @param {number} limit - Optional maximum number of entries to return. If not provided, returns all entries.
2020
* @return {Promise<LeaderboardEntry[]>} A promise that resolves to the leaderboard data.
2121
*/
2222
export async function getLeaderboard(
2323
queueId: number,
24-
limit: number = 100,
24+
limit?: number,
2525
): Promise<LeaderboardEntry[]> {
2626
try {
2727
return await getQueueLeaderboard(queueId, limit)

src/command-handlers/stats/getMatchHistory.ts

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,18 +18,17 @@ export type MatchHistoryEntry = {
1818
*
1919
* @param {string} userId - The Discord user ID of the player.
2020
* @param {number} queueId - The queue ID to fetch match history for.
21-
* @param {number} limit - Maximum number of matches to return (default: 50).
21+
* @param {number} limit - Optional maximum number of matches to return. If not provided, returns all matches.
2222
* @return {Promise<MatchHistoryEntry[]>} A promise that resolves to an array of match history entries.
2323
*/
2424
export async function getMatchHistory(
2525
userId: string,
2626
queueId: number,
27-
limit: number = 50,
27+
limit?: number,
2828
): Promise<MatchHistoryEntry[]> {
2929
try {
3030
// Get match history for the player
31-
const historyRes = await pool.query(
32-
`
31+
let query = `
3332
SELECT
3433
m.id as match_id,
3534
m.winning_team,
@@ -44,10 +43,15 @@ export async function getMatchHistory(
4443
JOIN matches m ON m.id = mu.match_id
4544
WHERE mu.user_id = $1 AND m.queue_id = $2 AND m.winning_team IS NOT NULL
4645
ORDER BY m.created_at DESC
47-
LIMIT $3
48-
`,
49-
[userId, queueId, limit],
50-
)
46+
`
47+
48+
const params: any[] = [userId, queueId]
49+
if (limit) {
50+
query += ` LIMIT $${params.length + 1}`
51+
params.push(limit)
52+
}
53+
54+
const historyRes = await pool.query(query, params)
5155

5256
return historyRes.rows.map((row) => ({
5357
match_id: row.match_id,

src/command-handlers/stats/getOverallHistory.ts

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,17 +19,16 @@ export type OverallHistoryEntry = {
1919
* Gets overall match history for a queue.
2020
*
2121
* @param {number} queueId - The queue ID to fetch match history for.
22-
* @param {number} limit - Maximum number of matches to return (default: 50).
22+
* @param {number} limit - Optional maximum number of matches to return. If not provided, returns all matches.
2323
* @return {Promise<OverallHistoryEntry[]>} A promise that resolves to an array of match history entries.
2424
*/
2525
export async function getOverallHistory(
2626
queueId: number,
27-
limit: number = 50,
27+
limit?: number,
2828
): Promise<OverallHistoryEntry[]> {
2929
try {
3030
// Get overall match history for the queue
31-
const historyRes = await pool.query(
32-
`
31+
let query = `
3332
SELECT
3433
m.id as match_id,
3534
m.winning_team,
@@ -41,10 +40,15 @@ export async function getOverallHistory(
4140
FROM matches m
4241
WHERE m.queue_id = $1 AND m.winning_team IS NOT NULL
4342
ORDER BY m.created_at DESC
44-
LIMIT $2
45-
`,
46-
[queueId, limit],
47-
)
43+
`
44+
45+
const params: any[] = [queueId]
46+
if (limit) {
47+
query += ` LIMIT $${params.length + 1}`
48+
params.push(limit)
49+
}
50+
51+
const historyRes = await pool.query(query, params)
4852

4953
// For each match, get the players
5054
const matches = await Promise.all(

src/utils/queryDB.ts

Lines changed: 27 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2093,7 +2093,7 @@ export async function getAllOpenRooms(): Promise<UserRoom[]> {
20932093
// Get leaderboard data for a queue
20942094
export async function getQueueLeaderboard(
20952095
queueId: number,
2096-
limit: number = 100,
2096+
limit?: number,
20972097
): Promise<
20982098
Array<{
20992099
rank: number
@@ -2107,27 +2107,32 @@ export async function getQueueLeaderboard(
21072107
peak_streak: number
21082108
}>
21092109
> {
2110-
const res = await pool.query(
2111-
`
2112-
SELECT
2113-
qu.user_id,
2114-
u.display_name,
2115-
qu.elo,
2116-
qu.peak_elo,
2117-
qu.win_streak,
2118-
qu.peak_win_streak,
2119-
COUNT(CASE WHEN m.winning_team = mu.team THEN 1 END)::integer as wins,
2120-
COUNT(CASE WHEN m.winning_team IS NOT NULL AND m.winning_team != mu.team THEN 1 END)::integer as losses
2121-
FROM queue_users qu
2122-
LEFT JOIN users u ON u.user_id = qu.user_id
2123-
LEFT JOIN match_users mu ON mu.user_id = qu.user_id
2124-
LEFT JOIN matches m ON m.id = mu.match_id AND m.queue_id = $1
2125-
WHERE qu.queue_id = $1
2126-
GROUP BY qu.user_id, u.display_name, qu.elo, qu.peak_elo, qu.win_streak, qu.peak_win_streak
2127-
ORDER BY qu.elo DESC
2128-
LIMIT $2`,
2129-
[queueId, limit],
2130-
)
2110+
let query = `
2111+
SELECT
2112+
qu.user_id,
2113+
u.display_name,
2114+
qu.elo,
2115+
qu.peak_elo,
2116+
qu.win_streak,
2117+
qu.peak_win_streak,
2118+
COUNT(CASE WHEN m.winning_team = mu.team THEN 1 END)::integer as wins,
2119+
COUNT(CASE WHEN m.winning_team IS NOT NULL AND m.winning_team != mu.team THEN 1 END)::integer as losses
2120+
FROM queue_users qu
2121+
LEFT JOIN users u ON u.user_id = qu.user_id
2122+
LEFT JOIN match_users mu ON mu.user_id = qu.user_id
2123+
LEFT JOIN matches m ON m.id = mu.match_id AND m.queue_id = $1
2124+
WHERE qu.queue_id = $1
2125+
GROUP BY qu.user_id, u.display_name, qu.elo, qu.peak_elo, qu.win_streak, qu.peak_win_streak
2126+
ORDER BY qu.elo DESC
2127+
`
2128+
2129+
const params: any[] = [queueId]
2130+
if (limit) {
2131+
query += ` LIMIT $${params.length + 1}`
2132+
params.push(limit)
2133+
}
2134+
2135+
const res = await pool.query(query, params)
21312136

21322137
return res.rows.map((row, index) => ({
21332138
rank: index + 1,

0 commit comments

Comments
 (0)