From ac9362bcabaa1f0302528d3ce003d290c38cb4bf Mon Sep 17 00:00:00 2001 From: Flatlogic Bot Date: Mon, 15 Dec 2025 09:49:58 +0000 Subject: [PATCH] 4 --- api/favorites.php | 56 ++++++++ db/migrations/003_create_users_table.sql | 8 ++ .../004_create_favorite_restaurants_table.sql | 10 ++ favorites.php | 59 +++++++++ includes/footer.php | 5 + includes/header.php | 45 +++++++ index.php | 119 ++++++----------- login.php | 84 ++++++++++++ logout.php | 6 + menu.php | 121 +++++++++++------- register.php | 99 ++++++++++++++ 11 files changed, 487 insertions(+), 125 deletions(-) create mode 100644 api/favorites.php create mode 100644 db/migrations/003_create_users_table.sql create mode 100644 db/migrations/004_create_favorite_restaurants_table.sql create mode 100644 favorites.php create mode 100644 includes/footer.php create mode 100644 includes/header.php create mode 100644 login.php create mode 100644 logout.php create mode 100644 register.php diff --git a/api/favorites.php b/api/favorites.php new file mode 100644 index 0000000..69bc20e --- /dev/null +++ b/api/favorites.php @@ -0,0 +1,56 @@ + false, 'loggedIn' => false, 'isFavorite' => false, 'message' => '']; + +if (!isset($_SESSION['user_id'])) { + $response['message'] = 'You must be logged in to favorite a restaurant.'; + echo json_encode($response); + exit; +} + +$response['loggedIn'] = true; +$user_id = $_SESSION['user_id']; + +$data = json_decode(file_get_contents('php://input'), true); +$restaurant_id = $data['restaurant_id'] ?? null; + +if (!$restaurant_id || !is_numeric($restaurant_id)) { + $response['message'] = 'Invalid restaurant ID.'; + echo json_encode($response); + exit; +} + +$pdo = db(); + +// Check if it's already a favorite +$stmt = $pdo->prepare("SELECT id FROM favorite_restaurants WHERE user_id = ? AND restaurant_id = ?"); +$stmt->execute([$user_id, $restaurant_id]); +$existing_favorite = $stmt->fetch(); + +if ($existing_favorite) { + // Remove from favorites + $stmt = $pdo->prepare("DELETE FROM favorite_restaurants WHERE id = ?"); + if ($stmt->execute([$existing_favorite['id']])) { + $response['success'] = true; + $response['isFavorite'] = false; + $response['message'] = 'Restaurant removed from favorites.'; + } else { + $response['message'] = 'Failed to remove from favorites.'; + } +} else { + // Add to favorites + $stmt = $pdo->prepare("INSERT INTO favorite_restaurants (user_id, restaurant_id) VALUES (?, ?)"); + if ($stmt->execute([$user_id, $restaurant_id])) { + $response['success'] = true; + $response['isFavorite'] = true; + $response['message'] = 'Restaurant added to favorites.'; + } else { + $response['message'] = 'Failed to add to favorites.'; + } +} + +echo json_encode($response); diff --git a/db/migrations/003_create_users_table.sql b/db/migrations/003_create_users_table.sql new file mode 100644 index 0000000..4ea44ae --- /dev/null +++ b/db/migrations/003_create_users_table.sql @@ -0,0 +1,8 @@ +-- 003_create_users_table.sql +CREATE TABLE IF NOT EXISTS `users` ( + `id` INT AUTO_INCREMENT PRIMARY KEY, + `name` VARCHAR(255) NOT NULL, + `email` VARCHAR(255) NOT NULL UNIQUE, + `password_hash` VARCHAR(255) NOT NULL, + `created_at` TIMESTAMP DEFAULT CURRENT_TIMESTAMP +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4; diff --git a/db/migrations/004_create_favorite_restaurants_table.sql b/db/migrations/004_create_favorite_restaurants_table.sql new file mode 100644 index 0000000..526c3bc --- /dev/null +++ b/db/migrations/004_create_favorite_restaurants_table.sql @@ -0,0 +1,10 @@ +-- 004_create_favorite_restaurants_table.sql +CREATE TABLE IF NOT EXISTS `favorite_restaurants` ( + `id` INT AUTO_INCREMENT PRIMARY KEY, + `user_id` INT NOT NULL, + `restaurant_id` INT NOT NULL, + `created_at` TIMESTAMP DEFAULT CURRENT_TIMESTAMP, + FOREIGN KEY (`user_id`) REFERENCES `users`(`id`) ON DELETE CASCADE, + FOREIGN KEY (`restaurant_id`) REFERENCES `restaurants`(`id`) ON DELETE CASCADE, + UNIQUE KEY `user_restaurant_unique` (`user_id`, `restaurant_id`) +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4; diff --git a/favorites.php b/favorites.php new file mode 100644 index 0000000..1ac44cb --- /dev/null +++ b/favorites.php @@ -0,0 +1,59 @@ +prepare(" + SELECT r.id, r.name, r.cuisine, r.address + FROM restaurants r + JOIN favorite_restaurants fr ON r.id = fr.restaurant_id + WHERE fr.user_id = ? + ORDER BY r.name ASC + "); + $stmt->execute([$user_id]); + $favorite_restaurants = $stmt->fetchAll(PDO::FETCH_ASSOC); +} catch (PDOException $e) { + error_log("Database error fetching favorites: " . $e->getMessage()); + // Optionally, show a friendly error to the user +} + +?> + +
+

My Favorite Restaurants

+ +
+ +
+

You haven't added any favorite restaurants yet.

+ +
+ + +
+
+
+
+

+

+ View Menu +
+
+
+ + +
+
+ + diff --git a/includes/footer.php b/includes/footer.php new file mode 100644 index 0000000..cbbb3ce --- /dev/null +++ b/includes/footer.php @@ -0,0 +1,5 @@ + + + + + diff --git a/includes/header.php b/includes/header.php new file mode 100644 index 0000000..c47df7b --- /dev/null +++ b/includes/header.php @@ -0,0 +1,45 @@ + + + + + + + Restaurant Marketplace + + + + + + +
\ No newline at end of file diff --git a/index.php b/index.php index 444ff56..6b7a9e4 100644 --- a/index.php +++ b/index.php @@ -1,4 +1,5 @@ - - - - - - Find a Restaurant - - - - - +
+
+

Find Your Next Meal

+

Browse through our collection of partner restaurants.

+
+
-
-
-

Find Your Next Meal

-

Browse through our collection of partner restaurants.

-
-
- -
+
@@ -99,45 +62,39 @@ try {

No restaurants match your search.

+ - - - + searchInput.addEventListener('input', filterRestaurants); + cuisineFilter.addEventListener('change', filterRestaurants); + }); + + diff --git a/login.php b/login.php new file mode 100644 index 0000000..0795597 --- /dev/null +++ b/login.php @@ -0,0 +1,84 @@ +prepare("SELECT id, name, password_hash FROM users WHERE email = ?"); + $stmt->execute([$email]); + $user = $stmt->fetch(); + + if ($user && password_verify($password, $user['password_hash'])) { + $_SESSION['user_id'] = $user['id']; + $_SESSION['user_name'] = $user['name']; + header("Location: index.php"); + exit; + } else { + $error_message = "Invalid email or password."; + } + } +} +?> + + + + + + Login + + + + + + +
+
+
+
+
+

Login

+
+
+ +
+ +
+
+ + +
+
+ + +
+ +
+
+ +
+
+
+
+ + + + diff --git a/logout.php b/logout.php new file mode 100644 index 0000000..f83284d --- /dev/null +++ b/logout.php @@ -0,0 +1,6 @@ +fetch(PDO::FETCH_ASSOC); } catch (PDOException $e) { error_log("DB error fetching restaurant: " . $e->getMessage()); + // Show a generic error page to the user + die("Error: Could not load restaurant information."); } if (!$restaurant) { - die("Restaurant not found."); + // Redirect or show a 404 page + header("Location: index.php?error=not_found"); + exit; +} + +$is_favorite = false; +if (isset($_SESSION['user_id'])) { + $stmt = $pdo->prepare("SELECT id FROM favorite_restaurants WHERE user_id = ? AND restaurant_id = ?"); + $stmt->execute([$_SESSION['user_id'], $restaurant_id]); + if ($stmt->fetch()) { + $is_favorite = true; + } } // Fetch menu items @@ -29,6 +45,7 @@ try { $menu_items = $stmt->fetchAll(PDO::FETCH_ASSOC); } catch (PDOException $e) { error_log("DB error fetching menu items: " . $e->getMessage()); + // It's okay to show the restaurant info even if menu fails to load } // Group menu items by category @@ -39,44 +56,27 @@ foreach ($menu_items as $item) { } ?> - - - - - - Menu for <?= htmlspecialchars($restaurant['name']) ?> - - - - - +