Actualiser api/matches.php

This commit is contained in:
2026-07-21 16:18:21 +02:00
parent 7dd4e683c5
commit 5f708b7993
-30
View File
@@ -1,6 +1,5 @@
<?php
require_once 'config.php';
$method = $_SERVER['REQUEST_METHOD'];
$action = $_GET['action'] ?? '';
$id = $_GET['id'] ?? null;
@@ -13,29 +12,24 @@ switch ($method) {
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,
@@ -56,7 +50,6 @@ function getAllMatches() {
ELSE 5
END
");
$matches = [];
while ($row = $stmt->fetch()) {
$matches[] = [
@@ -88,13 +81,11 @@ function getAllMatches() {
'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,
@@ -110,11 +101,9 @@ function getMatch($id) {
");
$stmt->execute([$id]);
$row = $stmt->fetch();
if (!$row) {
jsonResponse(['error' => 'Match non trouvé'], 404);
}
$match = [
'id' => $row['id'],
'round' => $row['round'],
@@ -143,34 +132,27 @@ function getMatch($id) {
] : 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'],
@@ -178,55 +160,43 @@ function addMatch() {
$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']);
}
?>