/** * @param $productId * @throws InvalidUserOperationException * @throws \Exception * @Authorize */ public function sellProduct($productId) { $userId = $this->getCurrentUserId(); $user = $this->_eshopData->getUsersRepository()->findById($userId); if ($user == null) { throw new \Exception("Invalid user id"); } $product = $this->_eshopData->getProductsRepository()->findById($productId); $isQuantityDescreased = $this->_eshopData->getProductsRepository()->sellProduct($product->getId()); if (!$isQuantityDescreased) { throw new InvalidUserOperationException("You dont have enough quantity left of this product."); } if ($isQuantityDescreased) { $isProductRemovedFromUser = $this->_eshopData->getProductsRepository()->removeProductFromUser($userId, $productId); if (!$isProductRemovedFromUser) { throw new \Exception("Error during selling your product."); } } if ($isProductRemovedFromUser) { $updateUserCash = $this->_eshopData->getUsersRepository()->sellItems($userId, $product->getPrice()); if (!$updateUserCash) { throw new \Exception("Error during selling your product."); } } if ($updateUserCash) { RouteService::redirect('account', 'profile', true); } }
public function adminPanel() { $user = $this->_eshopData->getUsersRepository()->findById($this->getCurrentUserId()); $user = $this->_eshopData->getUsersRepository()->findByUsername($user->getUsername()); $categories = $this->_eshopData->getCategoriesRepository()->all(); $allProducts = $this->_eshopData->getProductsRepository()->getAll(); $allPromotions = $this->_eshopData->getCategoriesRepository()->getPromotionsForCategories(); $allProductsPromotions = $this->_eshopData->getProductsRepository()->getAllProductsPromotion(); $promotionOfCustomProducts = $this->_eshopData->getProductsRepository()->allCertainProductsInPromotion(); $viewModel = new AdminPanelViewModel(); $viewModel->allProductsPromotion = $allProductsPromotions; $viewModel->categoryPromotions = $allPromotions; $viewModel->productsOnPromotion = $promotionOfCustomProducts; $viewModel->categories = $categories; $viewModel->allProducts = $allProducts; $viewModel->adminData = $user; $viewModel->render(); }