Différences
Ci-dessous, les différences entre deux révisions de la page.
| Prochaine révision | Révision précédente | ||
| web:framework:nextjs:api [2026/04/09 02:51] – créée jcheron | web:framework:nextjs:api [2026/04/09 03:10] (Version actuelle) – jcheron | ||
|---|---|---|---|
| Ligne 1: | Ligne 1: | ||
| ====== Mock API Padel ====== | ====== Mock API Padel ====== | ||
| - | ===== Types ===== | + | ===== Données===== |
| + | ==== Types ==== | ||
| - | types/data.ts | + | data.ts |
| <sxh ts; | <sxh ts; | ||
| Ligne 34: | Ligne 35: | ||
| }; | }; | ||
| </ | </ | ||
| + | ==== Joueurs ==== | ||
| + | |||
| + | <sxh ts> | ||
| + | export const players: Player[] = [ | ||
| + | { id: 1, name: " | ||
| + | { id: 2, name: "Lucas Martin", | ||
| + | { id: 3, name: "Enzo Garcia", | ||
| + | { id: 4, name: "Hugo Bernard", | ||
| + | { id: 5, name: " | ||
| + | { id: 6, name: "Leo Robert", | ||
| + | { id: 7, name: " | ||
| + | { id: 8, name: " | ||
| + | { id: 9, name: " | ||
| + | { id: 10, name: "Paul Girard", | ||
| + | ]; | ||
| + | </ | ||
| + | ==== Equipes ==== | ||
| + | |||
| + | <sxh ts> | ||
| + | export const teams: Team[] = [ | ||
| + | { id: 1, name: "Team Smash", | ||
| + | { id: 2, name: "Les Lobbers", | ||
| + | { id: 3, name: "Padel Masters", | ||
| + | { id: 4, name: "Top Spin", playerIds: [7, 8] }, | ||
| + | { id: 5, name: "Les Défenseurs", | ||
| + | ]; | ||
| + | </ | ||
| + | |||
| + | ==== Tournois ==== | ||
| + | <sxh ts> | ||
| + | export const tournaments: | ||
| + | { id: 1, name: "Open de Paris", | ||
| + | { id: 2, name: "Padel Cup Lyon", location: " | ||
| + | { id: 3, name: " | ||
| + | ]; | ||
| + | </ | ||
| + | |||
| + | ==== Matchs ==== | ||
| + | <sxh ts> | ||
| + | export const matches: Match[] = [ | ||
| + | // Tournoi 1 | ||
| + | { | ||
| + | id: 1, | ||
| + | tournamentId: | ||
| + | teamA: 1, | ||
| + | teamB: 2, | ||
| + | score: "6-4 3-6 10-7", | ||
| + | winnerTeamId: | ||
| + | }, | ||
| + | { | ||
| + | id: 2, | ||
| + | tournamentId: | ||
| + | teamA: 3, | ||
| + | teamB: 4, | ||
| + | score: "6-2 6-3", | ||
| + | winnerTeamId: | ||
| + | }, | ||
| + | { | ||
| + | id: 3, | ||
| + | tournamentId: | ||
| + | teamA: 1, | ||
| + | teamB: 4, | ||
| + | score: "7-6 6-4", | ||
| + | winnerTeamId: | ||
| + | }, | ||
| + | |||
| + | // Tournoi 2 | ||
| + | { | ||
| + | id: 4, | ||
| + | tournamentId: | ||
| + | teamA: 2, | ||
| + | teamB: 3, | ||
| + | score: "6-2 6-2", | ||
| + | winnerTeamId: | ||
| + | }, | ||
| + | { | ||
| + | id: 5, | ||
| + | tournamentId: | ||
| + | teamA: 4, | ||
| + | teamB: 5, | ||
| + | score: "6-7 6-3 10-6", | ||
| + | winnerTeamId: | ||
| + | }, | ||
| + | { | ||
| + | id: 6, | ||
| + | tournamentId: | ||
| + | teamA: 2, | ||
| + | teamB: 4, | ||
| + | score: null, | ||
| + | winnerTeamId: | ||
| + | }, | ||
| + | |||
| + | // Tournoi 3 | ||
| + | { | ||
| + | id: 7, | ||
| + | tournamentId: | ||
| + | teamA: 1, | ||
| + | teamB: 5, | ||
| + | score: null, | ||
| + | winnerTeamId: | ||
| + | }, | ||
| + | { | ||
| + | id: 8, | ||
| + | tournamentId: | ||
| + | teamA: 3, | ||
| + | teamB: 4, | ||
| + | score: null, | ||
| + | winnerTeamId: | ||
| + | } | ||
| + | ]; | ||
| + | </ | ||
| + | |||
| + | ===== 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); | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | |||
| + | |||