import { session } from '$lib/stores/session.svelte'; import { apiRequest, buildPath, type RequestOptions } from './client'; import type { GameDTO } from './schema-helpers'; /** GameController is `[Authorize]`; reuse the session JWT unless one is passed in. */ function authed(options: RequestOptions): RequestOptions { return { ...options, token: options.token ?? session.token }; } /** GET /api/Game — every game, in database order (sort by `order` for display). */ export function listGames(options: RequestOptions = {}): Promise { return apiRequest('/api/Game', authed(options)); } /** * POST /api/Game — `AddOrUpdate`: an `id` of 0 inserts, anything else updates. * The update replaces every column, so send a full DTO rather than a patch. * Returns the saved game with its assigned id. */ export function saveGame(game: GameDTO, options: RequestOptions = {}): Promise { return apiRequest('/api/Game', { ...authed(options), method: 'POST', body: game }); } /** * DELETE /api/Game/{id} — 204 on success. The service swallows `DbUpdateException`, * so a game still referenced by tournaments comes back as a 404 rather than a 409. */ export async function deleteGame(id: number, options: RequestOptions = {}): Promise { await apiRequest(buildPath('/api/Game/{id}', { id }), { ...authed(options), method: 'DELETE' }); } /** * GET /api/Game/smash/{name} — searches start.gg's videogame catalogue by name. * The `id` of each match is a **start.gg** videogame id, i.e. a candidate value for * `GameDTO.smashId`, not a LaDOSE game id. */ export function searchSmashGames( name: string, options: RequestOptions = {} ): Promise { return apiRequest(buildPath('/api/Game/smash/{name}', { name }), authed(options)); }