Show sourcecode
The following files exists in this folder. Click to view.
webbserverprogrammering/submissions/projekt-matkort-handler/classes/
CardBalance.php
FoodLog.php
Restaurant.php
User.php
Restaurant.php
186 lines UTF-8 Windows (CRLF)
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
<?php
class Restaurant {
private $conn;
function __construct($conn) {
$this->conn = $conn;
}
function getAll() {
$sql = "SELECT restaurant_name FROM restaurants";
try {
$stmt = $this->conn->query($sql);
$stmt->fetchAll(PDO::FETCH_ASSOC);
} catch (PDOException $e) {
throw new Exception("Kunde inte hämta restauranger:".$e->getMessage());
}
}
function getByID($restaurant_id) {
try {
$stmt = $this->conn->prepare("SELECT * FROM restaurants WHERE id = :id");
$stmt->execute(["id" => $restaurant_id]);
$stmt->fetch(PDO::FETCH_ASSOC);
} catch (PDOException $e) {
throw new Exception("Kunde inte hämta restaurang:" . $e->getMessage());
}
}
public function searchByName($term) {
$sql = "SELECT id, restaurant_name FROM restaurants WHERE restaurant_name LIKE :term ORDER BY restaurant_name ASC";
try {
$stmt = $this->conn->prepare($sql);
$likeTerm = $term . '%';
$stmt->bindParam(':term', $likeTerm);
$stmt->execute();
return $stmt->fetchAll(PDO::FETCH_ASSOC);
} catch (PDOException $e) {
throw new Exception("Kunde inte söka restauranger: " . $e->getMessage());
}
}
function create($restaurant_name, $location, $lat, $lng) {
$sql = "INSERT INTO restaurants(restaurant_name, location, lat, lng)
VALUES (?, ?, ?, ?)
ON DUPLICATE KEY UPDATE
location = VALUES(location),
lat = VALUES(lat),
lng = VALUES(lng);
";
try {
$stmt = $this->conn->prepare($sql);
$stmt->execute([$restaurant_name, $location, $lat, $lng]);
} catch (PDOException $e) {
throw new Exception('Kunde inte skapa restaurang: '.$e->getMessage());
}
}
function getAsJson() {
$sql = "SELECT id, restaurant_name AS name, location AS address, price, lat, lng FROM restaurants";
try {
$stmt = $this->conn->query($sql);
$result = $stmt->fetchAll(PDO::FETCH_ASSOC);
foreach ($result as &$row) {
$row['id'] = (int)$row['id'];
$row['lat'] = (float)$row['lat'];
$row['lng'] = (float)$row['lng'];
}
return json_encode($result);
} catch (PDOException $e) {
throw new Exception('Kunde inte hämta restauranger i JSON: '.$e->getMessage());
}
}
function getAllWithStats() {
try {
$sql = "
SELECT
r.id,
r.restaurant_name,
r.location,
r.lat,
r.lng,
COUNT(fl.id) as total_logs,
AVG(fl.healthy_rating) as avg_health,
AVG(fl.happy_rating) as avg_happy
FROM restaurants r
LEFT JOIN food_logs fl ON r.id = fl.restaurant_id
GROUP BY r.id
ORDER BY r.restaurant_name ASC
";
$stmt = $this->conn->query($sql);
return $stmt->fetchAll(PDO::FETCH_ASSOC);
} catch (PDOException $e) {
throw new Exception("Kunde inte hämta statistik för restauranger: " . $e->getMessage());
}
}
function deleteRestaurant($id) {
try {
$stmt = $this->conn->prepare("DELETE FROM restaurants WHERE id = ?");
return $stmt->execute([$id]);
} catch (PDOException $e) {
throw new Exception("Kunde inte ta bort restaurangen: " . $e->getMessage());
}
}
function updateRestaurant($id, $restaurant_name, $location, $lat, $lng) {
try {
$stmt = $this->conn->prepare("UPDATE restaurants SET restaurant_name = ?, location = ?, lat = ?, lng = ? WHERE id = ?");
return $stmt->execute([$restaurant_name, $location, $lat, $lng, $id]);
} catch (PDOException $e) {
throw new Exception('Kunde inte uppdatera restaurang: ' . $e->getMessage());
}
}
function getStatsAndFavorite($restaurant_id, $user_id = null) {
try {
$statsSql = "SELECT AVG(healthy_rating) as avg_health, AVG(happy_rating) as avg_happy FROM food_logs WHERE restaurant_id = ?";
$stmt1 = $this->conn->prepare($statsSql);
$stmt1->execute([$restaurant_id]);
$stats = $stmt1->fetch(PDO::FETCH_ASSOC);
$is_favorite = false;
if ($user_id) {
$favSql = "SELECT 1 FROM favourite_restaurants WHERE user_id = ? AND restaurant_id = ?";
$stmt2 = $this->conn->prepare($favSql);
$stmt2->execute([$user_id, $restaurant_id]);
if ($stmt2->fetchColumn()) {
$is_favorite = true;
}
}
return [
'avg_health' => $stats['avg_health'] ? round($stats['avg_health'], 1) : null,
'avg_happy' => $stats['avg_happy'] ? round($stats['avg_happy'], 1) : null,
'is_favorite'=> $is_favorite
];
} catch (PDOException $e) {
return ['error' => $e->getMessage()];
}
}
function toggleFavorite($user_id, $restaurant_id) {
try {
$checkSql = "SELECT id FROM favourite_restaurants WHERE user_id = ? AND restaurant_id = ?";
$checkStmt = $this->conn->prepare($checkSql);
$checkStmt->execute([$user_id, $restaurant_id]);
$record = $checkStmt->fetch(PDO::FETCH_ASSOC);
if ($record) {
// Exists, delete
$delSql = "DELETE FROM favourite_restaurants WHERE id = ?";
$delStmt = $this->conn->prepare($delSql);
$delStmt->execute([$record['id']]);
return ['status' => 'removed'];
} else {
// Dosent exist, insert
$insSql = "INSERT INTO favourite_restaurants (user_id, restaurant_id) VALUES (?, ?)";
$insStmt = $this->conn->prepare($insSql);
$insStmt->execute([$user_id, $restaurant_id]);
return ['status' => 'added'];
}
} catch (PDOException $e) {
return ['error' => $e->getMessage()];
}
}
function getUserFavorites($user_id) {
try {
$sql = "SELECT r.id, r.restaurant_name AS name, r.location AS address, r.price, r.lat, r.lng
FROM restaurants r
JOIN favourite_restaurants fr ON r.id = fr.restaurant_id
WHERE fr.user_id = ?";
$stmt = $this->conn->prepare($sql);
$stmt->execute([$user_id]);
return $stmt->fetchAll(PDO::FETCH_ASSOC);
} catch (PDOException $e) {
return [];
}
}
}