From c4ab6f5f1b0d81df25c42f3d8a5fae6118942f66 Mon Sep 17 00:00:00 2001 From: Kenneth SOARES Date: Fri, 17 Apr 2026 18:18:54 +0200 Subject: [PATCH] Add a product page --- src/app/controllers/ProductController.php | 25 +++++++++++++++++++++++ src/app/models/product.php | 19 +++++++++++++++-- src/app/views/product.php | 22 ++++++++++++++++++++ src/app/views/products.php | 10 +-------- src/routes/web.php | 3 ++- 5 files changed, 67 insertions(+), 12 deletions(-) create mode 100644 src/app/views/product.php 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 @@ + + +
+ ". $product->name . ""; + echo "

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 + . "

"; + } + ?> +
+ diff --git a/src/app/views/products.php b/src/app/views/products.php index 85ac03a..89b040a 100644 --- a/src/app/views/products.php +++ b/src/app/views/products.php @@ -7,15 +7,7 @@ foreach ($products as $product) { echo "
-

" . $product->name . " - " . $product->price . "€

- - +

" . $product->name . " - " . $product->price . "€

"; } ?> diff --git a/src/routes/web.php b/src/routes/web.php index 1c4a30f..172170e 100644 --- a/src/routes/web.php +++ b/src/routes/web.php @@ -7,7 +7,8 @@ '/sign-out' => ['AuthController', 'signOut'], '/account' => ['AuthController', 'viewAccount'], '/account-data' => ['AuthController', 'getAccountData'], - '/products' => ['ProductController', 'viewProducts'] + '/products' => ['ProductController', 'viewProducts'], + '/product' => ['ProductController', 'viewSingleProduct'], ], 'POST' => [ '/sign-in' => ['AuthController', 'signIn'],