| 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 |
|---|
| ==== Types ==== | ==== Types ==== |
| |
| types/data.ts | data.ts |
| |
| <sxh ts;title:types/data.ts> | <sxh ts;title:types/data.ts> |
| ]; | ]; |
| </sxh> | </sxh> |
| | |
| | ===== Helpers ===== |
| | utils/helpers.ts |
| | <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); |
| | }; |
| | </sxh> |
| | |
| | ===== Routes ===== |
| | |
| | ==== Joueurs ==== |
| | === GET /api/players === |
| | <sxh ts> |
| | import { players } from "@/data"; |
| | |
| | export async function GET() { |
| | return Response.json(players); |
| | } |
| | </sxh> |
| | |
| | === GET /api/players/[id] === |
| | <sxh ts> |
| | import { players } from "@/data"; |
| | |
| | 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("Player not found", { status: 404 }); |
| | } |
| | return Response.json(player); |
| | } |
| | </sxh> |
| | |
| | ==== Équipes ==== |
| | === GET /api/teams === |
| | <sxh ts> |
| | import { teams } from "@/data"; |
| | |
| | export async function GET() { |
| | return Response.json(teams); |
| | } |
| | </sxh> |
| | |
| | === GET /api/teams/[id] === |
| | <sxh ts> |
| | import { teams, players } from "@/data"; |
| | |
| | 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("Team not found", { status: 404 }); |
| | } |
| | const teamPlayers = players.filter(p => |
| | team.playerIds.includes(p.id) |
| | ); |
| | return Response.json({ |
| | ...team, |
| | players: teamPlayers |
| | }); |
| | } |
| | </sxh> |
| | |
| | ==== Tournois ==== |
| | === GET /api/tournaments === |
| | <sxh ts> |
| | import { tournaments } from "@/data"; |
| | |
| | export async function GET() { |
| | return Response.json(tournaments); |
| | } |
| | </sxh> |
| | |
| | === GET /api/tournaments/[id] === |
| | <sxh ts> |
| | import { tournaments, matches } from "@/data"; |
| | |
| | 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("Tournament not found", { status: 404 }); |
| | } |
| | const tournamentMatches = matches.filter( |
| | m => m.tournamentId === id |
| | ); |
| | return Response.json({ |
| | ...tournament, |
| | matches: tournamentMatches |
| | }); |
| | } |
| | </sxh> |
| | |
| | ==== Matchs ==== |
| | === GET /api/matches === |
| | Paramètres possibles : |
| | |
| | tournamentId |
| | teamId |
| | |
| | <sxh ts> |
| | import { matches } from "@/data"; |
| | |
| | export async function GET(req: Request) { |
| | const { searchParams } = new URL(req.url); |
| | const tournamentId = searchParams.get("tournamentId"); |
| | const teamId = searchParams.get("teamId"); |
| | 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); |
| | } |
| | </sxh> |
| | |
| | ==== Classement ==== |
| | === GET /api/rankings === |
| | <sxh ts> |
| | import { matches, teams } from "@/data"; |
| | |
| | export async function GET() { |
| | const scores: Record<number, number> = {}; |
| | 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); |
| | } |
| | </sxh> |
| | |
| |
| |