Files
mon-petit-pari/api/matches.php
T
2026-07-03 12:26:01 +02:00

232 lines
6.9 KiB
PHP

<?php
require_once 'config.php';
$method = $_SERVER['REQUEST_METHOD'];
$action = $_GET['action'] ?? '';
$id = $_GET['id'] ?? null;
switch ($method) {
case 'GET':
if ($id) {
getMatch($id);
} else {
getAllMatches();
}
break;
case 'POST':
requireAdmin();
addMatch();
break;
case 'PUT':
requireAdmin();
updateMatch($id);
break;
case 'DELETE':
requireAdmin();
deleteMatch($id);
break;
default:
jsonResponse(['error' => 'Méthode non autorisée'], 405);
}
function getAllMatches() {
$db = getDB();
$stmt = $db->query("
SELECT m.*,
p1.id as p1_id, p1.name as p1_name, p1.photo_url as p1_photo, p1.ranking as p1_ranking,
p1.nationality as p1_nationality, p1.handedness as p1_handedness,
p2.id as p2_id, p2.name as p2_name, p2.photo_url as p2_photo, p2.ranking as p2_ranking,
p2.nationality as p2_nationality, p2.handedness as p2_handedness,
pw.name as winner_name
FROM matches m
JOIN players p1 ON m.player1_id = p1.id
JOIN players p2 ON m.player2_id = p2.id
LEFT JOIN players pw ON m.winner_id = pw.id
ORDER BY m.match_date DESC,
CASE m.round
WHEN 'Finale' THEN 1
WHEN 'Demi-finale' THEN 2
WHEN 'Quart de finale' THEN 3
WHEN '8ème de finale' THEN 4
ELSE 5
END
");
$matches = [];
while ($row = $stmt->fetch()) {
$matches[] = [
'id' => $row['id'],
'round' => $row['round'],
'player1' => [
'id' => $row['p1_id'],
'name' => $row['p1_name'],
'photo' => $row['p1_photo'],
'ranking' => $row['p1_ranking'],
'nationality' => $row['p1_nationality'],
'handedness' => $row['p1_handedness']
],
'player2' => [
'id' => $row['p2_id'],
'name' => $row['p2_name'],
'photo' => $row['p2_photo'],
'ranking' => $row['p2_ranking'],
'nationality' => $row['p2_nationality'],
'handedness' => $row['p2_handedness']
],
'date' => $row['match_date'],
'court' => $row['court'],
'status' => $row['status'],
'winner' => $row['winner_id'] ? [
'id' => $row['winner_id'],
'name' => $row['winner_name']
] : null,
'score' => $row['score']
];
}
jsonResponse(['success' => true, 'matches' => $matches]);
}
function getMatch($id) {
$db = getDB();
$stmt = $db->prepare("
SELECT m.*,
p1.id as p1_id, p1.name as p1_name, p1.photo_url as p1_photo, p1.ranking as p1_ranking,
p1.nationality as p1_nationality, p1.handedness as p1_handedness,
p2.id as p2_id, p2.name as p2_name, p2.photo_url as p2_photo, p2.ranking as p2_ranking,
p2.nationality as p2_nationality, p2.handedness as p2_handedness,
pw.name as winner_name
FROM matches m
JOIN players p1 ON m.player1_id = p1.id
JOIN players p2 ON m.player2_id = p2.id
LEFT JOIN players pw ON m.winner_id = pw.id
WHERE m.id = ?
");
$stmt->execute([$id]);
$row = $stmt->fetch();
if (!$row) {
jsonResponse(['error' => 'Match non trouvé'], 404);
}
$match = [
'id' => $row['id'],
'round' => $row['round'],
'player1' => [
'id' => $row['p1_id'],
'name' => $row['p1_name'],
'photo' => $row['p1_photo'],
'ranking' => $row['p1_ranking'],
'nationality' => $row['p1_nationality'],
'handedness' => $row['p1_handedness']
],
'player2' => [
'id' => $row['p2_id'],
'name' => $row['p2_name'],
'photo' => $row['p2_photo'],
'ranking' => $row['p2_ranking'],
'nationality' => $row['p2_nationality'],
'handedness' => $row['p2_handedness']
],
'date' => $row['match_date'],
'court' => $row['court'],
'status' => $row['status'],
'winner' => $row['winner_id'] ? [
'id' => $row['winner_id'],
'name' => $row['winner_name']
] : null,
'score' => $row['score']
];
jsonResponse(['success' => true, 'match' => $match]);
}
function addMatch() {
$data = getJsonInput();
$required = ['round', 'player1_id', 'player2_id', 'match_date'];
foreach ($required as $field) {
if (!isset($data[$field])) {
jsonResponse(['error' => "Le champ $field est requis"], 400);
}
}
$db = getDB();
// Vérifier que les joueurs existent
$stmt = $db->prepare("SELECT id FROM players WHERE id IN (?, ?)");
$stmt->execute([$data['player1_id'], $data['player2_id']]);
if ($stmt->rowCount() !== 2) {
jsonResponse(['error' => 'Un ou les deux joueurs n\'existent pas'], 404);
}
$stmt = $db->prepare("
INSERT INTO matches (round, player1_id, player2_id, match_date, court, status)
VALUES (?, ?, ?, ?, ?, 'upcoming')
");
$stmt->execute([
$data['round'],
$data['player1_id'],
$data['player2_id'],
$data['match_date'],
$data['court'] ?? null
]);
jsonResponse(['success' => true, 'message' => 'Match ajouté avec succès', 'match_id' => $db->lastInsertId()]);
}
function updateMatch($id) {
$data = getJsonInput();
$db = getDB();
$stmt = $db->prepare("SELECT id, status FROM matches WHERE id = ?");
$stmt->execute([$id]);
$match = $stmt->fetch();
if (!$match) {
jsonResponse(['error' => 'Match non trouvé'], 404);
}
$fields = [];
$values = [];
$allowedFields = ['round', 'player1_id', 'player2_id', 'match_date', 'court', 'status', 'winner_id', 'score'];
foreach ($allowedFields as $field) {
if (isset($data[$field])) {
$fields[] = "$field = ?";
$values[] = $data[$field];
}
}
if (empty($fields)) {
jsonResponse(['error' => 'Aucune donnée à mettre à jour'], 400);
}
$values[] = $id;
$stmt = $db->prepare("UPDATE matches SET " . implode(', ', $fields) . " WHERE id = ?");
$stmt->execute($values);
jsonResponse(['success' => true, 'message' => 'Match mis à jour avec succès']);
}
function deleteMatch($id) {
$db = getDB();
$stmt = $db->prepare("DELETE FROM matches WHERE id = ?");
$stmt->execute([$id]);
if ($stmt->rowCount() === 0) {
jsonResponse(['error' => 'Match non trouvé'], 404);
}
jsonResponse(['success' => true, 'message' => 'Match supprimé avec succès']);
}
?>