initial commit of the paytime session frontend
This commit is contained in:
@@ -0,0 +1,64 @@
|
||||
import { NextRequest, NextResponse } from 'next/server';
|
||||
import { query } from '@/lib/db';
|
||||
|
||||
interface PlayerRow {
|
||||
steamid: string;
|
||||
current_name: string;
|
||||
current_country: string | null;
|
||||
matched_name: string | null;
|
||||
total_minutes: number | null;
|
||||
}
|
||||
|
||||
const TOTAL_MINUTES_SUBQUERY = `
|
||||
(SELECT COALESCE(s.session_end_playtime_minutes, s.session_start_playtime_minutes)
|
||||
FROM playtime_display_sessions s
|
||||
WHERE s.steamid = p.steamid
|
||||
ORDER BY s.session_id DESC
|
||||
LIMIT 1) AS total_minutes
|
||||
`;
|
||||
|
||||
export async function GET(req: NextRequest) {
|
||||
const q = req.nextUrl.searchParams.get('q')?.trim() ?? '';
|
||||
const sort = req.nextUrl.searchParams.get('sort') ?? 'recent';
|
||||
|
||||
const orderBy =
|
||||
sort === 'playtime'
|
||||
? 'total_minutes IS NULL, total_minutes DESC'
|
||||
: sort === 'name'
|
||||
? 'p.current_name'
|
||||
: 'p.last_seen DESC';
|
||||
|
||||
// No search term: just list players, ordered per the chosen sort.
|
||||
if (!q) {
|
||||
const rows = await query<PlayerRow>(
|
||||
`SELECT p.steamid, p.current_name, p.current_country, NULL AS matched_name,
|
||||
${TOTAL_MINUTES_SUBQUERY}
|
||||
FROM playtime_display_players p
|
||||
ORDER BY ${orderBy}
|
||||
LIMIT 50`,
|
||||
);
|
||||
return NextResponse.json({ players: rows });
|
||||
}
|
||||
|
||||
// Search matches steamid directly, or current/previous names.
|
||||
const like = `%${q}%`;
|
||||
const rows = await query<PlayerRow>(
|
||||
`SELECT p.steamid, p.current_name, p.current_country, h.player_name AS matched_name,
|
||||
${TOTAL_MINUTES_SUBQUERY}
|
||||
FROM playtime_display_players p
|
||||
LEFT JOIN playtime_display_name_history h
|
||||
ON h.steamid = p.steamid AND h.player_name LIKE ?
|
||||
WHERE p.steamid LIKE ?
|
||||
OR p.current_name LIKE ?
|
||||
OR EXISTS (
|
||||
SELECT 1 FROM playtime_display_name_history h2
|
||||
WHERE h2.steamid = p.steamid AND h2.player_name LIKE ?
|
||||
)
|
||||
GROUP BY p.steamid
|
||||
ORDER BY ${orderBy}
|
||||
LIMIT 50`,
|
||||
[like, like, like, like],
|
||||
);
|
||||
|
||||
return NextResponse.json({ players: rows });
|
||||
}
|
||||
Reference in New Issue
Block a user