From 69007cb918189a63334848fd9c72cf16f2157e96 Mon Sep 17 00:00:00 2001 From: filippo.bertilotti <filippobertilotti@gmail.com> Date: Fri, 24 May 2024 10:53:49 +0200 Subject: [PATCH] inizializzazione metodo per eliminare un prodotto dal database --- src/App/Controllers/products.php | 74 ++++++++++++++++++++++++++++++++++-- 1 files changed, 69 insertions(+), 5 deletions(-) diff --git a/src/App/Controllers/products.php b/src/App/Controllers/products.php index da5873c..b279418 100644 --- a/src/App/Controllers/products.php +++ b/src/App/Controllers/products.php @@ -16,12 +16,21 @@ } 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 + ]); + } + + public function edit(string $id) { + $product = $this->getProduct($id); + + echo $this->viewer->render("shared/header.php", [ + "title"=> "Edit product" + ]); + echo $this->viewer->render("Products/edit.php", [ "product"=> $product ]); } @@ -43,6 +52,61 @@ "description" => empty($_POST["description"]) ? null : $_POST["description"] ]; - var_dump($this->model->insert($data)); + 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(), + "product" => $data + ]); + }; + } + + public function update(string $id) { + + $product = $this->getProduct($id); + + + $product["name"] = $_POST["name"]; + $product["description"] = empty($_POST["description"]) ? null : $_POST["description"]; + + if($this->model->update($id, $product)) { + header("Location: /products/{$id}/show"); + exit; + } else { + echo $this->viewer->render("shared/header.php", [ + "title" => "Edit Product" + ]); + + echo $this->viewer->render("Products/edit.php", [ + "errors" => $this->model->getErrors(), + "product"=> $product + ]); + }; + } + + 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); + + echo $this->viewer->render("shared/header.php", [ + "title" => "Delete Product" + ]); + + echo $this->viewer->render("Products/delete.php", [ + "product" => $product + ]); } } \ No newline at end of file -- Gitblit v1.8.0