From 47a5842b7eae4aa4fea971846957cd8889f41efd Mon Sep 17 00:00:00 2001 From: filippo.bertilotti <filippobertilotti@gmail.com> Date: Thu, 23 May 2024 11:00:13 +0200 Subject: [PATCH] creazione pagina per modificare un determinato product (parte 133) --- src/App/Controllers/products.php | 46 +++++++++++++++++++++++++++++++++++++++++++++- 1 files changed, 45 insertions(+), 1 deletions(-) diff --git a/src/App/Controllers/products.php b/src/App/Controllers/products.php index a6e41d6..f06bbc6 100644 --- a/src/App/Controllers/products.php +++ b/src/App/Controllers/products.php @@ -1,12 +1,13 @@ <?php namespace App\Controllers; use App\Models\Product; +use Framework\Exceptions\PageNotFoundException; use Framework\Viewer; class Products { public function __construct(private Viewer $viewer, private Product $model) { } public function index() { - $products = $this->model->getData(); + $products = $this->model->findAll(); echo $this->viewer->render("shared/header.php"); echo $this->viewer->render("Products/index.php", [ @@ -16,13 +17,56 @@ public function show(string $id) { $product = $this->model->find($id); + if( $product == false ) { + throw new PageNotFoundException("Product not found"); + } echo $this->viewer->render("shared/header.php"); echo $this->viewer->render("Products/show.php", [ "product"=> $product ]); } + public function edit(string $id) { + $product = $this->model->find($id); + if( $product == false ) { + throw new PageNotFoundException("Product not found"); + } + echo $this->viewer->render("shared/header.php", [ + "title"=> "Edit product" + ]); + echo $this->viewer->render("Products/edit.php", [ + "product"=> $product + ]); + } + public function showPage(string $title, string $id, string $page) { echo $title, " ", $id, " ", $page; } + + public function new() { + echo $this->viewer->render("shared/header.php", [ + "title" => "New Product" + ]); + echo $this->viewer->render("Products/new.php"); + } + + public function create() { + $data = [ + "name" => $_POST["name"], + "description" => empty($_POST["description"]) ? null : $_POST["description"] + ]; + + if($this->model->insert($data)) { + header("Location: /products/{$this->model->getInsertID()}/show"); + exit; + } else { + echo $this->viewer->render("shared/header.php", [ + "title" => "New Product" + ]); + + echo $this->viewer->render("Products/new.php", + ["errors" => $this->model->getErrors() + ]); + }; + } } \ No newline at end of file -- Gitblit v1.8.0