From 5f708b7993ab709dd87d1a244afcd38323cdadf5 Mon Sep 17 00:00:00 2001 From: Cedric Date: Tue, 21 Jul 2026 16:18:21 +0200 Subject: [PATCH] Actualiser api/matches.php --- api/matches.php | 30 ------------------------------ 1 file changed, 30 deletions(-) diff --git a/api/matches.php b/api/matches.php index 44e9233..cb1f3dc 100644 --- a/api/matches.php +++ b/api/matches.php @@ -1,6 +1,5 @@ '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']); } ?> \ No newline at end of file