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 <?php
require_once 'config.php'; require_once 'config.php';
$method = $_SERVER['REQUEST_METHOD']; $method = $_SERVER['REQUEST_METHOD'];
$action = $_GET['action'] ?? ''; $action = $_GET['action'] ?? '';
$id = $_GET['id'] ?? null; $id = $_GET['id'] ?? null;
@@ -13,29 +12,24 @@ switch ($method) {
getAllMatches(); getAllMatches();
} }
break; break;
case 'POST': case 'POST':
requireAdmin(); requireAdmin();
addMatch(); addMatch();
break; break;
case 'PUT': case 'PUT':
requireAdmin(); requireAdmin();
updateMatch($id); updateMatch($id);
break; break;
case 'DELETE': case 'DELETE':
requireAdmin(); requireAdmin();
deleteMatch($id); deleteMatch($id);
break; break;
default: default:
jsonResponse(['error' => 'Méthode non autorisée'], 405); jsonResponse(['error' => 'Méthode non autorisée'], 405);
} }
function getAllMatches() { function getAllMatches() {
$db = getDB(); $db = getDB();
$stmt = $db->query(" $stmt = $db->query("
SELECT m.*, SELECT m.*,
p1.id as p1_id, p1.name as p1_name, p1.photo_url as p1_photo, p1.ranking as p1_ranking, 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 ELSE 5
END END
"); ");
$matches = []; $matches = [];
while ($row = $stmt->fetch()) { while ($row = $stmt->fetch()) {
$matches[] = [ $matches[] = [
@@ -88,13 +81,11 @@ function getAllMatches() {
'score' => $row['score'] 'score' => $row['score']
]; ];
} }
jsonResponse(['success' => true, 'matches' => $matches]); jsonResponse(['success' => true, 'matches' => $matches]);
} }
function getMatch($id) { function getMatch($id) {
$db = getDB(); $db = getDB();
$stmt = $db->prepare(" $stmt = $db->prepare("
SELECT m.*, SELECT m.*,
p1.id as p1_id, p1.name as p1_name, p1.photo_url as p1_photo, p1.ranking as p1_ranking, 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]); $stmt->execute([$id]);
$row = $stmt->fetch(); $row = $stmt->fetch();
if (!$row) { if (!$row) {
jsonResponse(['error' => 'Match non trouvé'], 404); jsonResponse(['error' => 'Match non trouvé'], 404);
} }
$match = [ $match = [
'id' => $row['id'], 'id' => $row['id'],
'round' => $row['round'], 'round' => $row['round'],
@@ -143,34 +132,27 @@ function getMatch($id) {
] : null, ] : null,
'score' => $row['score'] 'score' => $row['score']
]; ];
jsonResponse(['success' => true, 'match' => $match]); jsonResponse(['success' => true, 'match' => $match]);
} }
function addMatch() { function addMatch() {
$data = getJsonInput(); $data = getJsonInput();
$required = ['round', 'player1_id', 'player2_id', 'match_date']; $required = ['round', 'player1_id', 'player2_id', 'match_date'];
foreach ($required as $field) { foreach ($required as $field) {
if (!isset($data[$field])) { if (!isset($data[$field])) {
jsonResponse(['error' => "Le champ $field est requis"], 400); jsonResponse(['error' => "Le champ $field est requis"], 400);
} }
} }
$db = getDB(); $db = getDB();
// Vérifier que les joueurs existent
$stmt = $db->prepare("SELECT id FROM players WHERE id IN (?, ?)"); $stmt = $db->prepare("SELECT id FROM players WHERE id IN (?, ?)");
$stmt->execute([$data['player1_id'], $data['player2_id']]); $stmt->execute([$data['player1_id'], $data['player2_id']]);
if ($stmt->rowCount() !== 2) { if ($stmt->rowCount() !== 2) {
jsonResponse(['error' => 'Un ou les deux joueurs n\'existent pas'], 404); jsonResponse(['error' => 'Un ou les deux joueurs n\'existent pas'], 404);
} }
$stmt = $db->prepare(" $stmt = $db->prepare("
INSERT INTO matches (round, player1_id, player2_id, match_date, court, status) INSERT INTO matches (round, player1_id, player2_id, match_date, court, status)
VALUES (?, ?, ?, ?, ?, 'upcoming') VALUES (?, ?, ?, ?, ?, 'upcoming')
"); ");
$stmt->execute([ $stmt->execute([
$data['round'], $data['round'],
$data['player1_id'], $data['player1_id'],
@@ -178,55 +160,43 @@ function addMatch() {
$data['match_date'], $data['match_date'],
$data['court'] ?? null $data['court'] ?? null
]); ]);
jsonResponse(['success' => true, 'message' => 'Match ajouté avec succès', 'match_id' => $db->lastInsertId()]); jsonResponse(['success' => true, 'message' => 'Match ajouté avec succès', 'match_id' => $db->lastInsertId()]);
} }
function updateMatch($id) { function updateMatch($id) {
$data = getJsonInput(); $data = getJsonInput();
$db = getDB(); $db = getDB();
$stmt = $db->prepare("SELECT id, status FROM matches WHERE id = ?"); $stmt = $db->prepare("SELECT id, status FROM matches WHERE id = ?");
$stmt->execute([$id]); $stmt->execute([$id]);
$match = $stmt->fetch(); $match = $stmt->fetch();
if (!$match) { if (!$match) {
jsonResponse(['error' => 'Match non trouvé'], 404); jsonResponse(['error' => 'Match non trouvé'], 404);
} }
$fields = []; $fields = [];
$values = []; $values = [];
$allowedFields = ['round', 'player1_id', 'player2_id', 'match_date', 'court', 'status', 'winner_id', 'score']; $allowedFields = ['round', 'player1_id', 'player2_id', 'match_date', 'court', 'status', 'winner_id', 'score'];
foreach ($allowedFields as $field) { foreach ($allowedFields as $field) {
if (isset($data[$field])) { if (isset($data[$field])) {
$fields[] = "$field = ?"; $fields[] = "$field = ?";
$values[] = $data[$field]; $values[] = $data[$field];
} }
} }
if (empty($fields)) { if (empty($fields)) {
jsonResponse(['error' => 'Aucune donnée à mettre à jour'], 400); jsonResponse(['error' => 'Aucune donnée à mettre à jour'], 400);
} }
$values[] = $id; $values[] = $id;
$stmt = $db->prepare("UPDATE matches SET " . implode(', ', $fields) . " WHERE id = ?"); $stmt = $db->prepare("UPDATE matches SET " . implode(', ', $fields) . " WHERE id = ?");
$stmt->execute($values); $stmt->execute($values);
jsonResponse(['success' => true, 'message' => 'Match mis à jour avec succès']); jsonResponse(['success' => true, 'message' => 'Match mis à jour avec succès']);
} }
function deleteMatch($id) { function deleteMatch($id) {
$db = getDB(); $db = getDB();
$stmt = $db->prepare("DELETE FROM matches WHERE id = ?"); $stmt = $db->prepare("DELETE FROM matches WHERE id = ?");
$stmt->execute([$id]); $stmt->execute([$id]);
if ($stmt->rowCount() === 0) { if ($stmt->rowCount() === 0) {
jsonResponse(['error' => 'Match non trouvé'], 404); jsonResponse(['error' => 'Match non trouvé'], 404);
} }
jsonResponse(['success' => true, 'message' => 'Match supprimé avec succès']); jsonResponse(['success' => true, 'message' => 'Match supprimé avec succès']);
} }
?> ?>