src/Controller/RawMaterialController.php line 23

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\RawMaterial;
  4. use App\Entity\RecipeTestLine;
  5. use App\Entity\RecipeTestRawMaterial;
  6. use App\Service\ImportarMateriasHelper;
  7. use App\Utils\Communication;
  8. use App\Validator\RawMaterialValidator;
  9. use App\Validator\ValidatorHelper;
  10. use Doctrine\ORM\EntityManagerInterface;
  11. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  12. use Symfony\Component\HttpFoundation\JsonResponse;
  13. use Symfony\Component\HttpFoundation\Request;
  14. use Symfony\Component\HttpFoundation\Response;
  15. use Symfony\Component\Routing\Annotation\Route;
  16. use Sensio\Bundle\FrameworkExtraBundle\Request\ParamConverter;
  17. #[Route('/api/materials')]
  18. class RawMaterialController extends AbstractController
  19. {
  20.     #[Route('/import'name'api_raw_materials_import'methods: ["GET"])]
  21.     public function import(ImportarMateriasHelper $importarMateriasHelper): JsonResponse
  22.     {
  23.         return new JsonResponse($importarMateriasHelper->importar());
  24.     }
  25.     #[Route(""name"api_raw_materials_create"methods: ["POST"])]
  26.     public function create(EntityManagerInterface $entityManagerRequest $request): JsonResponse
  27.     {
  28.         $data json_decode($request->getContent(), true512JSON_THROW_ON_ERROR);
  29.         $errors RawMaterialValidator::createValidation($data);
  30.         $checkErrors ValidatorHelper::checkErrorConstraint($errorsRawMaterial::class);
  31.         if (null !== $checkErrors) {
  32.             return $checkErrors;
  33.         }
  34.         $rawMaterial = new RawMaterial();
  35.         $rawMaterial->setCode($data['code']);
  36.         $rawMaterial->setName($data['name']);
  37.         $rawMaterial->setPublicName($data['name']);
  38.         $entityManager->persist($rawMaterial);
  39.         $entityManager->flush();
  40.         return new JsonResponse(Communication::CreateMessage(
  41.             entityNameRawMaterial::class,
  42.             payload: [],
  43.             entityId$rawMaterial->getId()
  44.         ), Response::HTTP_CREATED);
  45.     }
  46.     #[Route("/{code}"name"api_raw_materials_save"methods: ["PATCH"])]
  47.     #[ParamConverter('rawMaterial', class: RawMaterial::class, options: ['code' => 'code'])]
  48.     public function save(EntityManagerInterface $entityManagerRequest $requestRawMaterial $rawMaterial): JsonResponse
  49.     {
  50.         $data json_decode($request->getContent(), true512JSON_THROW_ON_ERROR);
  51.         $errors RawMaterialValidator::saveValidation($data);
  52.         $checkErrors ValidatorHelper::checkErrorConstraint($errorsRawMaterial::class);
  53.         if (null !== $checkErrors) {
  54.             return $checkErrors;
  55.         }
  56.         $rawMaterial->fromArray($data);
  57.         $entityManager->persist($rawMaterial);
  58.         $entityManager->flush();
  59.         return new JsonResponse([], Response::HTTP_ACCEPTED);
  60.     }
  61.     #[Route("/{code}"name"api_raw_materials_delete"methods: ["DELETE"])]
  62.     #[ParamConverter('rawMaterial', class: RawMaterial::class, options: ['code' => 'code'])]
  63.     public function delete(EntityManagerInterface $entityManagerRawMaterial $rawMaterial): JsonResponse
  64.     {
  65.         /** @var array<RecipeTestRawMaterial> $recipes */
  66.         $recipes $entityManager->getRepository(RecipeTestRawMaterial::class)->findBy([
  67.             'rawMaterial' => $rawMaterial
  68.         ]);
  69.         if (count($recipes) > 0) {
  70.             $toRet = [];
  71.             foreach ($recipes as $recipe) {
  72.                 $toRet[] = [
  73.                     'recipeLine' => $recipe->getRecipeTest()->getLine(),
  74.                     'recipeCode' => $recipe->getRecipeTest()->getRecipe()->getCode(),
  75.                     '' => $recipe->getRecipeTest()->getRecipe()->getProject()->getName()
  76.                 ];
  77.             }
  78.             return new JsonResponse($toRetResponse::HTTP_IM_USED);
  79.         } else {
  80.             $entityManager->remove($rawMaterial);
  81.             $entityManager->persist();
  82.             return new JsonResponse([], Response::HTTP_ACCEPTED);
  83.         }
  84.     }
  85. }