src/Product/Controller/ProductController.php line 33

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace App\Product\Controller;
  3. use App\Framework\Controller\APIController;
  4. use App\Framework\Entity\PaginationResult;
  5. use App\Framework\Serializer\APISerializerGroup;
  6. use App\Product\Entity\Product;
  7. use App\Product\Entity\GetProductsFiltersInput;
  8. use App\Product\Service\ProductCategoryService;
  9. use App\Product\Service\ProductService;
  10. use Doctrine\ORM\EntityNotFoundException;
  11. use Doctrine\ORM\ORMException;
  12. use Psr\Log\LoggerInterface;
  13. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Security;
  14. use Symfony\Component\Routing\Annotation\Route;
  15. #[Route(path'/api/products')]
  16. class ProductController extends APIController
  17. {
  18.     public function __construct(
  19.         private ProductService $productService,
  20.         private ProductCategoryService $categoryService,
  21.         private LoggerInterface $logger,
  22.     ) { }
  23.     /**
  24.      * @return PaginationResult<Product>
  25.      */
  26.     #[Route(path''methods: ['GET'])]
  27.     #[APISerializerGroup('common:list''product:list''productcategory:default')]
  28.     public function getProducts(): PaginationResult
  29.     {
  30.         $paginationInput $this->getRequestPaginationInput();
  31.         $isPublicSale $this->getRequestFilterBool(key'isPublicSale'requiredfalse, default: null);
  32.         $categoryId $this->getRequestFilterInt(key'categoryId'requiredfalse, default: null);
  33.         $filtersInput = new GetProductsFiltersInput(
  34.             isPublicSale$isPublicSale,
  35.             categoryId$categoryId,
  36.         );
  37.         return $this->productService->getProducts(filtersInput$filtersInputpaginationInput$paginationInput);
  38.     }
  39.     /**
  40.      * @throws EntityNotFoundException
  41.      */
  42.     #[Route(path'/{id}'requirements: ['id' => '\d+'], methods: ['GET'])]
  43.     #[APISerializerGroup('product:list''productcategory:default')]
  44.     public function getProductById(int $id): Product
  45.     {
  46.         return $this->productService->getProductById($id);
  47.     }
  48.     /**
  49.      *
  50.      * @throws EntityNotFoundException
  51.      * @throws ORMException
  52.      */
  53.     #[Route(path''methods: ['POST'])]
  54.     #[Security("is_granted('ROLE_ADMIN') or is_granted('ROLE_HUERTA')")]
  55.     #[APISerializerGroup('product:list''productcategory:default')]
  56.     public function createProduct(): Product
  57.     {
  58.         // Get request body
  59.         $data $this->getRequestBody();
  60.         if (array_key_exists('category'$data) && $data['category']) {
  61.             $data['category'] = $this->categoryService->getProductCategoryById($data['category']);
  62.         }
  63.         // Create the product
  64.         return $this->productService->createProductFromFields($data);
  65.     }
  66.     /**
  67.      *
  68.      * @throws EntityNotFoundException
  69.      * @throws ORMException
  70.      */
  71.     #[Route(path'/{id}'requirements: ['id' => '\d+'], methods: ['PATCH'])]
  72.     #[Security("is_granted('ROLE_ADMIN') or is_granted('ROLE_HUERTA')")]
  73.     #[APISerializerGroup('product:list''productcategory:default')]
  74.     public function updateProduct(int $id): Product {
  75.         // Fetch the order entity
  76.         $product $this->productService->getProductById($id);
  77.         if (!$product) {
  78.             throw EntityNotFoundException::fromClassNameAndIdentifier(Product::class, [$id]);
  79.         }
  80.         // Get request body
  81.         $data $this->getRequestBody();
  82.         if (array_key_exists('category'$data)) {
  83.             $data['category'] = $this->categoryService->getProductCategoryById($data['category']);
  84.         }
  85.         // And patch the order
  86.         $this->productService->patchFields($product$data);
  87.         return $product;
  88.     }
  89.     /**
  90.      *
  91.      * @throws EntityNotFoundException
  92.      * @throws ORMException
  93.      */
  94.     #[Route(path'/{id}/thumbnail'requirements: ['id' => '\d+'], methods: ['POST'])]
  95.     #[Security("is_granted('ROLE_ADMIN')")]
  96.     #[APISerializerGroup('product:list''productcategory:default')]
  97.     public function updateProductThumbnail(Product $product): Product
  98.     {
  99.         // Upload thumbnail
  100.         $thumbnail $this->getRequestFileData('thumbnail'false);
  101.         if ($thumbnail) {
  102.             $this->productService->updateThumbnail($product$thumbnail);
  103.         }
  104.         return $product;
  105.     }
  106. }