From e1781d96e68a1bd68a9d2406cb568de8f5521e4c Mon Sep 17 00:00:00 2001 From: ashikkalis Date: Thu, 9 Jan 2025 12:15:13 +0200 Subject: [PATCH] Delete Lobby API Test --- blokus.php | 5 +++++ lib/lobbys.php | 22 +++++++++++++++++++++- 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/blokus.php b/blokus.php index e2427bf..e1a8e08 100644 --- a/blokus.php +++ b/blokus.php @@ -74,10 +74,15 @@ $router->add('POST', 'lobbys/create', function($input) { //Figure out how we can test this one too createLobby((int)$input['userId'], $input['gameType'], (int)$input['maxPlayers'], $input['createdAt']); }); + $router->add('POST', 'lobbys/leave', function() { leaveLobby(); }); + $router->add('Post', 'lobbys/delete', function(){ + deleteLobby(); + }); + $router->add('POST', 'lobbys/join', function($input) { if (!isset($input['userId']) || !isset($input['lobbyId'])) { echo json_encode(['error' => 'Missing required parameters: userId or lobbyId']); diff --git a/lib/lobbys.php b/lib/lobbys.php index be40c55..a2d4cc7 100644 --- a/lib/lobbys.php +++ b/lib/lobbys.php @@ -142,7 +142,7 @@ function leaveLobby() { $lobbyId = 5; try { - $sql = "DELETE FROM games WHERE game_id = ?"; + $sql = "DELETE FROM game_players WHERE user_id = ?"; $stmt = $pdo->prepare($sql); $stmt->execute([$lobbyId]); @@ -175,4 +175,24 @@ function leaveLobby() { // echo json_encode(['error' => 'Database error: ' . $e->getMessage()]); // } // } + +function deleteLobby(){ + $pdo = getDatabaseConnection(); + $gemeId = 5; + + try{ + $sql = "DELETE FROM games WHERE game_id = ?"; + $stmt = $pdo->prepare($sql); + $stmt->execute([$gameId]); + + if ($stmt->rowCount() > 0) { + echo json_encode(['success' => true, 'message' => "Lobby with ID $gameId deleted successfully"]); + } else { + echo json_encode(['success' => false, 'message' => "No lobby found with ID $gameId"]); + } + } + catch (PDOException $e) { + echo json_encode(['error' => 'Database error: ' . $e->getMessage()]); + } +} ?> \ No newline at end of file