From c2428b3a8f97ff3a5c125944ac2b6aee9b081734 Mon Sep 17 00:00:00 2001 From: filippo.bertilotti <filippobertilotti@gmail.com> Date: Tue, 28 May 2024 12:00:57 +0200 Subject: [PATCH] aggiunta metodo set per la dipendeza Viewer (parte 150) --- src/App/Controllers/products.php | 71 ++++++++++++++++++++++++++--------- 1 files changed, 52 insertions(+), 19 deletions(-) diff --git a/src/App/Controllers/products.php b/src/App/Controllers/products.php index 783243b..44e61d0 100644 --- a/src/App/Controllers/products.php +++ b/src/App/Controllers/products.php @@ -3,23 +3,24 @@ use App\Models\Product; use Framework\Exceptions\PageNotFoundException; use Framework\Viewer; -class Products { - public function __construct(private Viewer $viewer, private Product $model) { } +use Framework\Controller; +class Products extends Controller { + + public function __construct(private Product $model) { } public function index() { $products = $this->model->findAll(); echo $this->viewer->render("shared/header.php"); echo $this->viewer->render("Products/index.php", [ - "products"=> $products + "products"=> $products, + "total" => $this->model->getTotal() ]); } public function show(string $id) { - $product = $this->model->find($id); - if( $product == false ) { - throw new PageNotFoundException("Product not found"); - } + $product = $this->getProduct($id); + echo $this->viewer->render("shared/header.php"); echo $this->viewer->render("Products/show.php", [ "product"=> $product @@ -27,10 +28,8 @@ } public function edit(string $id) { - $product = $this->model->find($id); - if( $product == false ) { - throw new PageNotFoundException("Product not found"); - } + $product = $this->getProduct($id); + echo $this->viewer->render("shared/header.php", [ "title"=> "Edit product" ]); @@ -52,8 +51,8 @@ public function create() { $data = [ - "name" => $_POST["name"], - "description" => empty($_POST["description"]) ? null : $_POST["description"] + "name" => $this->request->post["name"], + "description" => empty($this->request->post["description"]) ? null : $this->request->post["description"] ]; if($this->model->insert($data)) { @@ -73,14 +72,11 @@ public function update(string $id) { - $product = $this->model->find($id); - if( $product == false ) { - throw new PageNotFoundException("Product not found"); - } + $product = $this->getProduct($id); - $product["name"] = $_POST["name"]; - $product["description"] = empty($_POST["description"]) ? null : $_POST["description"]; + $product["name"] = $this->request->post["name"]; + $product["description"] = empty($this->request->post["description"]) ? null : $this->request->post["description"]; if($this->model->update($id, $product)) { header("Location: /products/{$id}/show"); @@ -96,4 +92,41 @@ ]); }; } + + public function getProduct(string $id): array { + $product = $this->model->find($id); + if( $product == false ) { + throw new PageNotFoundException("Product not found"); + } + return $product; + } + + public function delete(string $id) { + $product = $this->getProduct($id); + + if($_SERVER["REQUEST_METHOD"] === "POST") { + $this->model->delete($id); + header("Location: /products/index"); + exit; + } + + echo $this->viewer->render("shared/header.php", [ + "title" => "Delete Product" + ]); + + echo $this->viewer->render("Products/delete.php", [ + "product" => $product + ]); + } + + public function destroy(string $id) { + $product = $this->getProduct($id); + + + $this->model->delete($id); + header("Location: /products/index"); + exit; + } + + } \ No newline at end of file -- Gitblit v1.8.0