web:framework:nextjs:api

Différences

Ci-dessous, les différences entre deux révisions de la page.

Lien vers cette vue comparative

Prochaine révision
Révision précédente
web:framework:nextjs:api [2026/04/09 02:51] – créée jcheronweb: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;title:types/data.ts> <sxh ts;title:types/data.ts>
Ligne 34: Ligne 35:
 }; };
 </sxh> </sxh>
 +==== Joueurs ====
 +
 +<sxh ts>
 +export const players: Player[] = [
 +  { id: 1, name: "Thomas Dupont", ranking: 15 },
 +  { id: 2, name: "Lucas Martin", ranking: 22 },
 +  { id: 3, name: "Enzo Garcia", ranking: 8 },
 +  { id: 4, name: "Hugo Bernard", ranking: 12 },
 +  { id: 5, name: "Nathan Petit", ranking: 30 },
 +  { id: 6, name: "Leo Robert", ranking: 18 },
 +  { id: 7, name: "Maxime Roux", ranking: 5 },
 +  { id: 8, name: "Antoine Morel", ranking: 9 },
 +  { id: 9, name: "Julien Fournier", ranking: 25 },
 +  { id: 10, name: "Paul Girard", ranking: 17 }
 +];
 +</sxh>
 +==== Equipes ====
 +
 +<sxh ts>
 +export const teams: Team[] = [
 +  { id: 1, name: "Team Smash", playerIds: [1, 2] },
 +  { id: 2, name: "Les Lobbers", playerIds: [3, 4] },
 +  { id: 3, name: "Padel Masters", playerIds: [5, 6] },
 +  { id: 4, name: "Top Spin", playerIds: [7, 8] },
 +  { id: 5, name: "Les Défenseurs", playerIds: [9, 10] }
 +];
 +</sxh>
 +
 +==== Tournois ====
 +<sxh ts>
 +export const tournaments: Tournament[] = [
 +  { id: 1, name: "Open de Paris", location: "Paris", date: "2026-05-10" },
 +  { id: 2, name: "Padel Cup Lyon", location: "Lyon", date: "2026-06-15" },
 +  { id: 3, name: "Marseille Padel Tour", location: "Marseille", date: "2026-07-20" }
 +];
 +</sxh>
 +
 +==== Matchs ====
 +<sxh ts>
 +export const matches: Match[] = [
 +  // Tournoi 1
 +  {
 +    id: 1,
 +    tournamentId: 1,
 +    teamA: 1,
 +    teamB: 2,
 +    score: "6-4 3-6 10-7",
 +    winnerTeamId: 1
 +  },
 +  {
 +    id: 2,
 +    tournamentId: 1,
 +    teamA: 3,
 +    teamB: 4,
 +    score: "6-2 6-3",
 +    winnerTeamId: 4
 +  },
 +  {
 +    id: 3,
 +    tournamentId: 1,
 +    teamA: 1,
 +    teamB: 4,
 +    score: "7-6 6-4",
 +    winnerTeamId: 1
 +  },
 +
 +  // Tournoi 2
 +  {
 +    id: 4,
 +    tournamentId: 2,
 +    teamA: 2,
 +    teamB: 3,
 +    score: "6-2 6-2",
 +    winnerTeamId: 2
 +  },
 +  {
 +    id: 5,
 +    tournamentId: 2,
 +    teamA: 4,
 +    teamB: 5,
 +    score: "6-7 6-3 10-6",
 +    winnerTeamId: 4
 +  },
 +  {
 +    id: 6,
 +    tournamentId: 2,
 +    teamA: 2,
 +    teamB: 4,
 +    score: null,
 +    winnerTeamId: null
 +  },
 +
 +  // Tournoi 3
 +  {
 +    id: 7,
 +    tournamentId: 3,
 +    teamA: 1,
 +    teamB: 5,
 +    score: null,
 +    winnerTeamId: null
 +  },
 +  {
 +    id: 8,
 +    tournamentId: 3,
 +    teamA: 3,
 +    teamB: 4,
 +    score: null,
 +    winnerTeamId: null
 +  }
 +];
 +</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>
 +
 +
 +
  • web/framework/nextjs/api.1775695911.txt.gz
  • Dernière modification : il y a 6 jours
  • de jcheron