corso https://vola.udemy.com/course/php-mvc-from-scratch/learn/lecture/40931984#overview
filippo.bertilotti
2024-05-28 c2428b3a8f97ff3a5c125944ac2b6aee9b081734
src/App/Controllers/products.php
@@ -1,27 +1,132 @@
<?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) { }
use Framework\Controller;
class Products extends Controller {
    public function __construct(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", [
            "products"=> $products
            "products"=> $products,
            "total" => $this->model->getTotal()
        ]);
    }
    public function show(string $id) {
        $product = $this->getProduct($id);
        echo $this->viewer->render("shared/header.php");
        echo $this->viewer->render("Products/show.php", [
            "id"=> $id
            "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
        ]);
    }
    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" => $this->request->post["name"],
            "description" => empty($this->request->post["description"]) ? null : $this->request->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(),
                "product" => $data
            ]);
        };
    }
    public function update(string $id) {
        $product = $this->getProduct($id);
        $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");
            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);
        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;
    }
}