From 501dedf9c07544a242f6f18ca093dd42c88284a4 Mon Sep 17 00:00:00 2001 From: Undergraduate Student Date: Sun, 15 Dec 2024 18:30:33 +0200 Subject: [PATCH] api beta update v2 --- app.js | 45 +++++++++++++++++++++++++++++ blokus.php | 3 +- lib/users.php | 78 ++++++++++++++++++++++++++++++++++++++++++++++++--- login.html | 36 ++++++++++++++++++++++++ stats.html | 33 ++++++++++++++++++++++ status.html | 33 ++++++++++++++++++++++ 6 files changed, 222 insertions(+), 6 deletions(-) create mode 100644 app.js create mode 100644 login.html create mode 100644 stats.html create mode 100644 status.html diff --git a/app.js b/app.js new file mode 100644 index 0000000..abdb8a4 --- /dev/null +++ b/app.js @@ -0,0 +1,45 @@ +const API_URL = "https://users.iee.ihu.gr/~iee2020202/ADISE24_DreamTeam/blokus.php/users/"; + +// Handle login +$("#loginForm").on("submit", function (event) { + event.preventDefault(); + + const userId = $("#userId").val(); + + $.ajax({ + url: API_URL + userId, + method: "GET", + success: function (response) { + if (response && response.id) { + // Save user info in sessionStorage + sessionStorage.setItem("loggedInUser", JSON.stringify(response)); + window.location.href = "stats.html"; // Redirect to stats page + } else { + $("#errorMessage").text("Invalid User ID").show(); + } + }, + error: function () { + $("#errorMessage").text("Unable to fetch user data. Please try again.").show(); + }, + }); +}); + +// Redirect to login if not logged in +if (window.location.pathname.endsWith("stats.html")) { + const loggedInUser = sessionStorage.getItem("loggedInUser"); + if (!loggedInUser) { + window.location.href = "login.html"; + } else { + const user = JSON.parse(loggedInUser); + $("#userId").text(user.id); + $("#username").text(user.username); + $("#email").text(user.email); + $("#createdAt").text(user.created_at); + } +} + +// Logout functionality +$("#logoutBtn").on("click", function () { + sessionStorage.removeItem("loggedInUser"); +}); + diff --git a/blokus.php b/blokus.php index bd36fe1..e710b46 100644 --- a/blokus.php +++ b/blokus.php @@ -59,7 +59,7 @@ function handle_users($method, $input) { // Handler for 'user' endpoint (show a specific user) function handle_user($method, $identifier, $input) { if ($method == 'GET') { - show_user($identifier); // Assuming you have a function to show a specific user + getUserProfile($identifier); // Assuming you have a function to show a specific user } else { header('HTTP/1.1 405 Method Not Allowed'); } @@ -74,4 +74,3 @@ function handle_status($method) { } } ?> - diff --git a/lib/users.php b/lib/users.php index 4f13f98..e36fc29 100755 --- a/lib/users.php +++ b/lib/users.php @@ -11,9 +11,78 @@ function getPathSegments() { return $segments; } -// Function to get the database connection +/** + * Retrieve user profile details + */ +function getUserProfile($userId) { + $pdo = getDatabaseConnection(); // Get the PDO connection here + try { + $sql = "SELECT id, username, email, created_at FROM users WHERE id = ?"; + $stmt = $pdo->prepare($sql); + $stmt->execute([$userId]); + $user = $stmt->fetch(PDO::FETCH_ASSOC); + if ($user) { + echo json_encode($user, JSON_PRETTY_PRINT); // Return the user's profile data as JSON + } else { + echo json_encode(['error' => 'User not found']); + } + } catch (PDOException $e) { + echo json_encode(['error' => 'Error in getUserProfile: ' . $e->getMessage()]); + } +} + +/** + * Update user profile details + */ +function updateUserProfile($userId, $username, $email) { + $pdo = getDatabaseConnection(); // Get the PDO connection here + try { + $sql = "UPDATE users SET username = ?, email = ? WHERE id = ?"; + $stmt = $pdo->prepare($sql); + $stmt->execute([$username, $email, $userId]); + return ['success' => $stmt->rowCount() > 0]; + } catch (PDOException $e) { + return ['error' => 'Error in updateUserProfile: ' . $e->getMessage()]; + } +} + +/** + * Delete user account + */ +function deleteUser($userId) { + $pdo = getDatabaseConnection(); // Get the PDO connection here + try { + $sql = "DELETE FROM users WHERE id = ?"; + $stmt = $pdo->prepare($sql); + $stmt->execute([$userId]); + return ['success' => $stmt->rowCount() > 0]; + } catch (PDOException $e) { + return ['error' => 'Error in deleteUser: ' . $e->getMessage()]; + } +} + +/** + * Retrieve user's game statistics + */ +function getUserGameStats($userId) { + $pdo = getDatabaseConnection(); // Get the PDO connection here + try { + $sql = "SELECT + COUNT(CASE WHEN player1_id = ? AND winner_id = player1_id THEN 1 END) AS wins, + COUNT(CASE WHEN player2_id = ? AND winner_id = player2_id THEN 1 END) AS losses, + COUNT(game_id) AS total_games + FROM games + WHERE player1_id = ? OR player2_id = ?"; + $stmt = $pdo->prepare($sql); + $stmt->execute([$userId, $userId, $userId, $userId]); + return $stmt->fetch(PDO::FETCH_ASSOC); + } catch (PDOException $e) { + return ['error' => 'Error in getUserGameStats: ' . $e->getMessage()]; + } +} + // RESTful Functions function show_users() { $pdo = getDatabaseConnection(); @@ -47,9 +116,9 @@ function show_user($piece_color) { // If no additional segment, call show_users() show_users(); } elseif (count($segments) === 2) { - // If additional segment, call show_user($piece_color) - $piece_color = $segments[1]; - show_user($piece_color); + // If additional segment, call getUserProfile($userId) + $userId = $segments[1]; + getUserProfile($userId); } else { http_response_code(404); echo json_encode(["error" => "Invalid endpoint"]); @@ -62,5 +131,6 @@ function show_user($piece_color) { http_response_code(404); echo json_encode(["error" => "Endpoint not found"]); } + ?> diff --git a/login.html b/login.html new file mode 100644 index 0000000..68fbdd4 --- /dev/null +++ b/login.html @@ -0,0 +1,36 @@ + + + + + + Login Page + + + + +
+
+
+
+
+

Login

+
+
+
+
+ + +
+ +
+ +
+
+
+
+
+ + + + + diff --git a/stats.html b/stats.html new file mode 100644 index 0000000..ffda4de --- /dev/null +++ b/stats.html @@ -0,0 +1,33 @@ + + + + + + User Stats + + + + +
+
+
+
+
+

Your Stats

+
+
+

Username:

+

Total Games Played:

+

Wins:

+

Losses:

+ Logout +
+
+
+
+
+ + + + + diff --git a/status.html b/status.html new file mode 100644 index 0000000..f42e918 --- /dev/null +++ b/status.html @@ -0,0 +1,33 @@ + + + + + + User Stats + + + + +
+
+
+
+
+

Your Stats

+
+
+

ID:

+

Username:

+

Email:

+

Created At:

+ Logout +
+
+
+
+
+ + + + +