Skip to content

Commit

Permalink
Delete Lobby API Test
Browse files Browse the repository at this point in the history
  • Loading branch information
Ashikkalis committed Jan 9, 2025
1 parent 3e51e3f commit e1781d9
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
5 changes: 5 additions & 0 deletions blokus.php
Original file line number Diff line number Diff line change
Expand Up @@ -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']);
Expand Down
22 changes: 21 additions & 1 deletion lib/lobbys.php
Original file line number Diff line number Diff line change
Expand Up @@ -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]);

Expand Down Expand Up @@ -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()]);
}
}
?>

0 comments on commit e1781d9

Please sign in to comment.