<?php
namespace App\Controller;
use App\Entity\GroupProducts;
use App\Entity\MaterialProducts;
use App\Entity\Product;
use App\Entity\ShopCart;
use App\Entity\User;
use App\Form\RegistrationFormBuyerType;
use App\Repository\ProductRepository;
use App\Services\IndividualProductService;
use App\Services\MasterService;
use App\Services\ProductService;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Config\Definition\Exception\Exception;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
class ShopController extends AbstractController
{
#[Route(['/', '/shop'], name: 'app_shop')]
public function index(ProductRepository $productRepository, EntityManagerInterface $entityManager, Request $request): Response
{
$groupsProducts = $entityManager->getRepository(GroupProducts::class)->findAll();
$materialsProducts = $entityManager->getRepository(MaterialProducts::class)->findAll();
/** @var User $user */
$user = $this->getUser();
$formBuyer = $this->createForm(RegistrationFormBuyerType::class, $user, [
'action' => $this->generateUrl('app_register'),
'method' => 'POST',
]);
$filter = (new \ShopService())->filterProductsConstructor($groupsProducts, $materialsProducts, $request);
$maxPrice = $productRepository->findMaxPrice();
$products = $productRepository->getProducts($filter);
return $this->render('shop/index.html.twig', [
'products' => $products,
'groupsProducts' => $groupsProducts,
'registrationFormBuyer' => $formBuyer->createView(),
'materialsProducts'=> $materialsProducts,
'maxPrice' => $maxPrice
]);
}
#[Route('/get-product/{product_id}', name: 'get_product')]
public function getProduct(ProductService $productService, EntityManagerInterface $entityManager, $product_id): Response
{
$product = $entityManager->getRepository(Product::class)->find($product_id);
if (!$product) {
throw new exception('Продукт не найден');
}
return $this->render('shop/oneProductCard.html.twig', [
'product' => $product
]);
}
#[Route('/masters', name: 'app_masters')]
public function getMasters(EntityManagerInterface $entityManager) : Response
{
$masters = (new MasterService())->getMasters($entityManager);
return $this->render('shop/masters.html.twig', [
'masters' => $masters
]);
}
#[Route('/addProductToCart')]
public function addProductToCart(Request $request, EntityManagerInterface $entityManager): JsonResponse
{
$user = $this->getUser();
$result = (new \ShopService())->addProductToUserCart($request, $user, $entityManager);
return $result;
}
#[Route('/cart', name:'viewCart')]
public function getCart(EntityManagerInterface $entityManager, Request $request): Response
{
$user = $this->getUser();
$products = (new \ShopService())->viewCart($entityManager, $request, $user);
return $this->render('shop/cart.html.twig', [
'cartPositions' => $products]);
}
#[Route('/deleteProductFromCart/{cartPositionId}')]
public function deleteProductFromCart(EntityManagerInterface $entityManager, ShopCart $cartPositionId): JsonResponse
{
$shopService = new \ShopService();
$shopService->deleteFromCart($entityManager, $cartPositionId);
return new JsonResponse(['success' => true]);
}
#[Route('/cart/buyProduct', name: 'buy_product')]
public function buyProductPage(EntityManagerInterface $entityManager, Request $request)
{
$user = $this->getUser();
$cartItems = (new \ShopService())->loadContentForCheckoutPage($entityManager, $request, $user);
return $this->render('shop/checkout.html.twig', [
'cartPositions' => $cartItems,
'user' => $user
]);
}
#[Route('/cart/confirmPurchase')]
public function confirmPurchase(Request $request, EntityManagerInterface $entityManager): JsonResponse
{
(new \ShopService())->confirmPurchaseSaveData($request, $entityManager);
return new JsonResponse(['success' => 'Заказ успешно сформирован']);
}
#[Route('/addIndividualProduct', name: 'add_individual_product')]
public function addIndividualProduct(Request $request, EntityManagerInterface $entityManager)
{
$user = $this->getUser();
$iProductService = new IndividualProductService();
$iProductService->addIndividualProduct($request, $entityManager, $user);
return $this->render('shop/individual_product_add.html.twig');
}
}