src/Product/Controller/ProductCategoryController.php line 25

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\Serializer\APISerializerGroup;
  5. use App\Product\Entity\Product;
  6. use App\Product\Entity\ProductCategory;
  7. use App\Product\Repository\ProductCategoryRepository;
  8. use App\Product\Repository\ProductRepository;
  9. use App\Product\Service\ProductCategoryService;
  10. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Security;
  11. use Symfony\Component\Routing\Annotation\Route;
  12. #[Route(path'/api/categories')]
  13. class ProductCategoryController extends APIController
  14. {
  15.     /**
  16.      * @throws ExceptionInterface
  17.      *
  18.      * @return mixed[]|ProductCategory[]|void
  19.      */
  20.     #[Route(path''methods: ['GET'])]
  21.     #[APISerializerGroup('productcategory:default')]
  22.     public function indexAction(ProductCategoryRepository $productCategoryRepository): array
  23.     {
  24.         if ($this->isGranted('ROLE_ADMIN')) {
  25.             return $productCategoryRepository->findAll();
  26.         } else {
  27.             return $productCategoryRepository->findBy([
  28.                 'display' => 1,
  29.             ]);
  30.         }
  31.     }
  32.     #[Route(path''methods: ['POST'])]
  33.     #[APISerializerGroup('productcategory:default')]
  34.     #[Security("is_granted('ROLE_ADMIN')")]
  35.     public function createAction(ProductCategoryService $categoryService): ProductCategory
  36.     {
  37.         $name $this->getRequestBodyData('name'true);
  38.         $visible $this->getRequestBodyData('visible'falsefalse);
  39.         return $categoryService->createProductCategory($name$visible);
  40.     }
  41.     #[Route(path'/{id}'methods: ['PATCH'])]
  42.     #[APISerializerGroup('productcategory:default')]
  43.     #[Security("is_granted('ROLE_ADMIN')")]
  44.     public function updateAction(ProductCategory $categoryProductCategoryService $categoryService): ProductCategory
  45.     {
  46.         $name $this->getRequestBodyData('name'true);
  47.         $visible $this->getRequestBodyData('visible'true);
  48.         return $categoryService->updateProductCategory($category$name$visible);
  49.     }
  50.     #[Route(path'/{id}'methods: ['DELETE'])]
  51.     #[APISerializerGroup('productcategory:default')]
  52.     #[Security("is_granted('ROLE_ADMIN')")]
  53.     public function deleteAction(ProductCategory $categoryProductCategoryService $categoryService): void
  54.     {
  55.         $categoryService->deleteProductCategory($category);
  56.     }
  57.     /**
  58.      * @throws ExceptionInterface
  59.      *
  60.      * @return Product[]
  61.      */
  62.     #[Route(path'/{id}'methods: ['GET'])]
  63.     #[APISerializerGroup('product:list')]
  64.     public function listAction(int $idProductRepository $productRepository): array
  65.     {
  66.         // TODO: Use service instead of repository directly
  67.         return $productRepository->findForSaleByCategoryId($id);
  68.     }
  69. }