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 +} + +?> + +
You haven't added any favorite restaurants yet.
+ += htmlspecialchars($restaurant['cuisine']) ?>
+= htmlspecialchars($restaurant['address']) ?>
+ View Menu +