Skip to content

Commit

Permalink
Delete Lobby API Test3
Browse files Browse the repository at this point in the history
  • Loading branch information
Ashikkalis committed Jan 9, 2025
1 parent 6817ab7 commit 5db7949
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 15 deletions.
10 changes: 8 additions & 2 deletions blokus.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,14 @@
leaveLobby();
});

$router->add('Post', 'lobbys/delete', function(){
deleteLobby();
$router->add('Post', 'lobbys/delete', function($input){
if (!isset($input['gameId'])) {
echo json_encode(['error' => 'Missing required parameters: gameId']);
return;
}
$gameId = (int)$input['gameId'];

deleteLobby($gameId);
});

$router->add('POST', 'lobbys/join', function($input) {
Expand Down
27 changes: 14 additions & 13 deletions lib/lobbys.php
Original file line number Diff line number Diff line change
Expand Up @@ -176,23 +176,24 @@ function leaveLobby() {
// }
// }

function deleteLobby(){
function deleteLobby($gameId){
$pdo = getDatabaseConnection();
$gameId = 5;

try{
$sql = "DELETE FROM games WHERE game_id = ?";
$stmt = $pdo->prepare($sql);
$stmt->execute([$gameId]);
$deleteGameSql = "CALL Blokus_db.DeleteGame(:game_id)";
$deleteGameStmt = $pdo->prepare($deleteGameSql);

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"]);
try {
$deleteGameStmt->execute(['game_id' => $gameId]);
echo json_encode(['success' => true, 'message' => "You have successfully delete the lobby with ID $gameId."]);
} catch (PDOException $e) {
error_log("Stored procedure error: " . $e->getMessage());
echo json_encode(['success' => false, 'message' => 'Failed to delete the lobby. Please contact support.']);
}
}
catch (PDOException $e) {
echo json_encode(['error' => 'Database error: ' . $e->getMessage()]);
}
catch (PDOException $e) {
// Log any unexpected database errors
error_log("Database error: " . $e->getMessage());
echo json_encode(['success' => false, 'message' => 'Database error: ' . $e->getMessage()]);
}
}
?>

0 comments on commit 5db7949

Please sign in to comment.