diff --git a/src/app/controllers/ProductController.php b/src/app/controllers/ProductController.php index f390a1e..398ada1 100644 --- a/src/app/controllers/ProductController.php +++ b/src/app/controllers/ProductController.php @@ -14,4 +14,29 @@ public function viewProducts() { $products = $this->productModel->getAll(); require_once __DIR__ . "/../views/products.php"; } + + public function viewSingleProduct() { + if($_SERVER['REQUEST_METHOD'] !== 'GET') { + header('Location: /products'); + exit; + } + + $id = isset($_GET['id']) ? $_GET['id'] : null; + + if(!$id) { + header('Location: /products'); + exit; + } + + $product = $this->productModel->getById($id); + + if(!$product) { + header('Location: /products'); + exit; + } + + $title = $product->name; + + require_once __DIR__ . "/../views/product.php"; + } } diff --git a/src/app/models/Product.php b/src/app/models/Product.php index 7ecd3c9..fefc743 100644 --- a/src/app/models/Product.php +++ b/src/app/models/Product.php @@ -11,7 +11,7 @@ public function __construct(PDO $dbConnection) { public function create($id, $name, $description, $price, $supplier_id) { $query = "INSERT INTO product (id, name, description, prince, supplier_id) - VALUES (:id, :name, :description, :price, :supplier_id)"; + VALUES (:id, :name, :description, :price, :supplier_id);"; $stmt = $this->conn->prepare($query); $stmt->bindValue(":id", $id); @@ -24,11 +24,26 @@ public function create($id, $name, $description, $price, $supplier_id) { } public function getAll() { - $query = "SELECT * FROM product"; + $query = "SELECT * FROM product;"; $stmt = $this->conn->prepare($query); $stmt->execute(); return $stmt->fetchAll(PDO::FETCH_OBJ); } + + public function getById($id) { + $query = "SELECT product.*, c.name AS category_name, + s.name AS supplier_name, s.email AS supplier_email, + s.phone AS supplier_phone + FROM product + INNER JOIN product_to_category ptc ON product.id = ptc.product_id + INNER JOIN product_category c ON ptc.category_id = c.id + INNER JOIN supplier s ON product.supplier_id = s.id + WHERE product.id = :id;"; + $stmt = $this->conn->prepare($query); + + $stmt->execute(['id' => $id]); + return $stmt->fetch(PDO::FETCH_OBJ); + } } diff --git a/src/app/views/product.php b/src/app/views/product.php new file mode 100644 index 0000000..3acca86 --- /dev/null +++ b/src/app/views/product.php @@ -0,0 +1,22 @@ + + +
Price: " . $product->price . "€
"; + echo "Description: " . $product->description . "
"; + echo "Category: " . $product->category_name . "
"; + + if($product->supplier_name + && $product->supplier_phone + && $product->supplier_email) { + + echo "Supplier: " + . $product->supplier_name + . " - " . $product->supplier_email + . " - " . $product->supplier_phone + . "
"; + } + ?> +" . $product->name . " - " . $product->price . "€
- - +" . $product->name . " - " . $product->price . "€