src/Controller/ShopController.php line 80

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\GroupProducts;
  4. use App\Entity\MaterialProducts;
  5. use App\Entity\Product;
  6. use App\Entity\ShopCart;
  7. use App\Entity\User;
  8. use App\Form\RegistrationFormBuyerType;
  9. use App\Repository\ProductRepository;
  10. use App\Services\IndividualProductService;
  11. use App\Services\MasterService;
  12. use App\Services\ProductService;
  13. use Doctrine\ORM\EntityManagerInterface;
  14. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  15. use Symfony\Component\Config\Definition\Exception\Exception;
  16. use Symfony\Component\HttpFoundation\JsonResponse;
  17. use Symfony\Component\HttpFoundation\Request;
  18. use Symfony\Component\HttpFoundation\Response;
  19. use Symfony\Component\Routing\Annotation\Route;
  20. class ShopController extends AbstractController
  21. {
  22.     #[Route(['/''/shop'], name'app_shop')]
  23.     public function index(ProductRepository $productRepositoryEntityManagerInterface $entityManagerRequest $request): Response
  24.     {
  25.         $groupsProducts $entityManager->getRepository(GroupProducts::class)->findAll();
  26.         $materialsProducts $entityManager->getRepository(MaterialProducts::class)->findAll();
  27.         /** @var User $user */
  28.         $user $this->getUser();
  29.         $formBuyer $this->createForm(RegistrationFormBuyerType::class, $user, [
  30.             'action' => $this->generateUrl('app_register'),
  31.             'method' => 'POST',
  32.         ]);
  33.         $filter = (new \ShopService())->filterProductsConstructor($groupsProducts$materialsProducts$request);
  34.         $maxPrice $productRepository->findMaxPrice();
  35.         $products $productRepository->getProducts($filter);
  36.         return $this->render('shop/index.html.twig', [
  37.             'products' => $products,
  38.             'groupsProducts' => $groupsProducts,
  39.             'registrationFormBuyer' => $formBuyer->createView(),
  40.             'materialsProducts'=> $materialsProducts,
  41.             'maxPrice' => $maxPrice
  42.         ]);
  43.     }
  44.     #[Route('/get-product/{product_id}'name'get_product')]
  45.     public function getProduct(ProductService $productServiceEntityManagerInterface $entityManager$product_id): Response
  46.     {
  47.         $product $entityManager->getRepository(Product::class)->find($product_id);
  48.         if (!$product) {
  49.             throw new exception('Продукт не найден');
  50.         }
  51.         return $this->render('shop/oneProductCard.html.twig', [
  52.             'product' => $product
  53.         ]);
  54.     }
  55.     #[Route('/masters'name'app_masters')]
  56.     public function getMasters(EntityManagerInterface $entityManager) : Response
  57.     {
  58.        $masters = (new MasterService())->getMasters($entityManager);
  59.         return $this->render('shop/masters.html.twig', [
  60.             'masters' => $masters
  61.         ]);
  62.     }
  63.     #[Route('/addProductToCart')]
  64.     public function addProductToCart(Request $requestEntityManagerInterface $entityManager): JsonResponse
  65.     {
  66.         $user $this->getUser();
  67.         $result = (new \ShopService())->addProductToUserCart($request$user$entityManager);
  68.         return $result;
  69.     }
  70.     #[Route('/cart'name:'viewCart')]
  71.     public function getCart(EntityManagerInterface $entityManagerRequest $request): Response
  72.     {
  73.         $user $this->getUser();
  74.         $products = (new \ShopService())->viewCart($entityManager$request$user);
  75.         return $this->render('shop/cart.html.twig', [
  76.             'cartPositions' => $products]);
  77.     }
  78.     #[Route('/deleteProductFromCart/{cartPositionId}')]
  79.     public function deleteProductFromCart(EntityManagerInterface $entityManagerShopCart $cartPositionId): JsonResponse
  80.     {
  81.         $shopService = new \ShopService();
  82.         $shopService->deleteFromCart($entityManager$cartPositionId);
  83.         return new JsonResponse(['success' => true]);
  84.     }
  85.     #[Route('/cart/buyProduct'name'buy_product')]
  86.     public function buyProductPage(EntityManagerInterface $entityManagerRequest $request)
  87.     {
  88.         $user $this->getUser();
  89.         $cartItems = (new \ShopService())->loadContentForCheckoutPage($entityManager$request$user);
  90.         return $this->render('shop/checkout.html.twig', [
  91.             'cartPositions' => $cartItems,
  92.             'user' => $user
  93.         ]);
  94.     }
  95.     #[Route('/cart/confirmPurchase')]
  96.     public function confirmPurchase(Request $requestEntityManagerInterface $entityManager): JsonResponse
  97.     {
  98.         (new \ShopService())->confirmPurchaseSaveData($request$entityManager);
  99.         return new JsonResponse(['success' => 'Заказ успешно сформирован']);
  100.     }
  101.     #[Route('/addIndividualProduct'name'add_individual_product')]
  102.     public function addIndividualProduct(Request $requestEntityManagerInterface $entityManager)
  103.     {
  104.         $user $this->getUser();
  105.         $iProductService = new IndividualProductService();
  106.         $iProductService->addIndividualProduct($request$entityManager$user);
  107.         return $this->render('shop/individual_product_add.html.twig');
  108.     }
  109. }