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 changeStock($productId, $newQuantity) { $this->validatePermissions(); $categoryModel = new ProductModel(); $viewModel = new InformationViewModel(); try { $categoryModel->changeStock($productId, $newQuantity); $viewModel->success = "Quantity of product {$productId} updated successfully"; } catch (\Exception $e) { $viewModel->error = $e->getMessage(); } return new View($viewModel); }