Différences
Ci-dessous, les différences entre deux révisions de la page.
| Les deux révisions précédentes Révision précédente Prochaine révision | Révision précédente | ||
| web:framework:nextjs:api [2026/04/09 02:57] – jcheron | web:framework:nextjs:api [2026/04/09 03:10] (Version actuelle) – jcheron | ||
|---|---|---|---|
| Ligne 3: | Ligne 3: | ||
| ==== Types ==== | ==== Types ==== | ||
| - | types/data.ts | + | data.ts |
| <sxh ts; | <sxh ts; | ||
| Ligne 146: | Ligne 146: | ||
| ]; | ]; | ||
| </ | </ | ||
| + | |||
| + | ===== Helpers ===== | ||
| + | utils/ | ||
| + | <sxh ts> | ||
| + | export const getTeamById = (id: number) => | ||
| + | teams.find(t => t.id === id); | ||
| + | |||
| + | export const getPlayerById = (id: number) => | ||
| + | players.find(p => p.id === id); | ||
| + | |||
| + | export const getPlayersOfTeam = (teamId: number) => { | ||
| + | const team = getTeamById(teamId); | ||
| + | if (!team) return []; | ||
| + | |||
| + | return team.playerIds.map(getPlayerById); | ||
| + | }; | ||
| + | </ | ||
| + | |||
| + | ===== Routes ===== | ||
| + | |||
| + | ==== Joueurs ==== | ||
| + | === GET / | ||
| + | <sxh ts> | ||
| + | import { players } from " | ||
| + | |||
| + | export async function GET() { | ||
| + | return Response.json(players); | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | === GET / | ||
| + | <sxh ts> | ||
| + | import { players } from " | ||
| + | |||
| + | export async function GET( | ||
| + | req: Request, | ||
| + | { params }: { params: { id: string } } | ||
| + | ) { | ||
| + | const id = parseInt(params.id); | ||
| + | const player = players.find(p => p.id === id); | ||
| + | if (!player) { | ||
| + | return new Response(" | ||
| + | } | ||
| + | return Response.json(player); | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | ==== Équipes ==== | ||
| + | === GET /api/teams === | ||
| + | <sxh ts> | ||
| + | import { teams } from " | ||
| + | |||
| + | export async function GET() { | ||
| + | return Response.json(teams); | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | === GET / | ||
| + | <sxh ts> | ||
| + | import { teams, players } from " | ||
| + | |||
| + | export async function GET( | ||
| + | req: Request, | ||
| + | { params }: { params: { id: string } } | ||
| + | ) { | ||
| + | const id = parseInt(params.id); | ||
| + | const team = teams.find(t => t.id === id); | ||
| + | if (!team) { | ||
| + | return new Response(" | ||
| + | } | ||
| + | const teamPlayers = players.filter(p => | ||
| + | team.playerIds.includes(p.id) | ||
| + | ); | ||
| + | return Response.json({ | ||
| + | ...team, | ||
| + | players: teamPlayers | ||
| + | }); | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | ==== Tournois ==== | ||
| + | === GET / | ||
| + | <sxh ts> | ||
| + | import { tournaments } from " | ||
| + | |||
| + | export async function GET() { | ||
| + | return Response.json(tournaments); | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | === GET / | ||
| + | <sxh ts> | ||
| + | import { tournaments, | ||
| + | |||
| + | export async function GET( | ||
| + | req: Request, | ||
| + | { params }: { params: { id: string } } | ||
| + | ) { | ||
| + | const id = parseInt(params.id); | ||
| + | const tournament = tournaments.find(t => t.id === id); | ||
| + | if (!tournament) { | ||
| + | return new Response(" | ||
| + | } | ||
| + | const tournamentMatches = matches.filter( | ||
| + | m => m.tournamentId === id | ||
| + | ); | ||
| + | return Response.json({ | ||
| + | ...tournament, | ||
| + | matches: tournamentMatches | ||
| + | }); | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | ==== Matchs ==== | ||
| + | === GET / | ||
| + | Paramètres possibles : | ||
| + | |||
| + | tournamentId | ||
| + | teamId | ||
| + | |||
| + | <sxh ts> | ||
| + | import { matches } from " | ||
| + | |||
| + | export async function GET(req: Request) { | ||
| + | const { searchParams } = new URL(req.url); | ||
| + | const tournamentId = searchParams.get(" | ||
| + | const teamId = searchParams.get(" | ||
| + | let result = matches; | ||
| + | if (tournamentId) { | ||
| + | result = result.filter( | ||
| + | m => m.tournamentId === parseInt(tournamentId) | ||
| + | ); | ||
| + | } | ||
| + | if (teamId) { | ||
| + | const id = parseInt(teamId); | ||
| + | result = result.filter( | ||
| + | m => m.teamA === id || m.teamB === id | ||
| + | ); | ||
| + | } | ||
| + | return Response.json(result); | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | ==== Classement ==== | ||
| + | === GET / | ||
| + | <sxh ts> | ||
| + | import { matches, teams } from " | ||
| + | |||
| + | export async function GET() { | ||
| + | const scores: Record< | ||
| + | teams.forEach(team => { | ||
| + | scores[team.id] = 0; | ||
| + | }); | ||
| + | matches.forEach(match => { | ||
| + | if (match.winnerTeamId) { | ||
| + | scores[match.winnerTeamId] += 3; | ||
| + | } | ||
| + | }); | ||
| + | const ranking = teams | ||
| + | .map(team => ({ | ||
| + | team, | ||
| + | points: scores[team.id] | ||
| + | })) | ||
| + | .sort((a, b) => b.points - a.points); | ||
| + | return Response.json(ranking); | ||
| + | } | ||
| + | </ | ||
| + | |||