public function add() { if (!isset($_POST['id']) || !isset($_POST['quantity'])) { header("Location: /products/all"); } if (!$this->isLogged()) { header("Location: /users/login"); } $productId = $_POST['id']; $quantityWanted = $_POST['quantity']; $userId = (string) $_SESSION['id']; $product = new ProductModel(); $viewModel = new InformationViewModel(); if (!$product->exists($productId)) { $viewModel->error = "Product {$productId} does not exist"; } $quantityAvailable = $product->details($productId)->getQuantity(); if ($quantityWanted > $quantityAvailable) { $viewModel->error = "Not enough stock"; } $shoppingCart = new CartModel(); if ($shoppingCart->add($userId, $productId, $quantityWanted)) { $viewModel->success = "Product {$productId} added to cart successfully"; } else { $viewModel->error = "Cannot add product {$productId} to cart"; } return new View($viewModel); }
public function details($id) { if (!$this->isLogged()) { header("Location: /users/login"); exit; } $model = new ProductModel(); if (!$model->exists($id)) { $error = new \stdClass(); $error->error = "Product {$id} does not exist"; return new View($error); } $product = $model->details($id); return new View($product); }