<?php
namespace App\Controller;
use App\Entity\RawMaterial;
use App\Entity\RecipeTestLine;
use App\Entity\RecipeTestRawMaterial;
use App\Service\ImportarMateriasHelper;
use App\Utils\Communication;
use App\Validator\RawMaterialValidator;
use App\Validator\ValidatorHelper;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Sensio\Bundle\FrameworkExtraBundle\Request\ParamConverter;
#[Route('/api/materials')]
class RawMaterialController extends AbstractController
{
#[Route('/import', name: 'api_raw_materials_import', methods: ["GET"])]
public function import(ImportarMateriasHelper $importarMateriasHelper): JsonResponse
{
return new JsonResponse($importarMateriasHelper->importar());
}
#[Route("", name: "api_raw_materials_create", methods: ["POST"])]
public function create(EntityManagerInterface $entityManager, Request $request): JsonResponse
{
$data = json_decode($request->getContent(), true, 512, JSON_THROW_ON_ERROR);
$errors = RawMaterialValidator::createValidation($data);
$checkErrors = ValidatorHelper::checkErrorConstraint($errors, RawMaterial::class);
if (null !== $checkErrors) {
return $checkErrors;
}
$rawMaterial = new RawMaterial();
$rawMaterial->setCode($data['code']);
$rawMaterial->setName($data['name']);
$rawMaterial->setPublicName($data['name']);
$entityManager->persist($rawMaterial);
$entityManager->flush();
return new JsonResponse(Communication::CreateMessage(
entityName: RawMaterial::class,
payload: [],
entityId: $rawMaterial->getId()
), Response::HTTP_CREATED);
}
#[Route("/{code}", name: "api_raw_materials_save", methods: ["PATCH"])]
#[ParamConverter('rawMaterial', class: RawMaterial::class, options: ['code' => 'code'])]
public function save(EntityManagerInterface $entityManager, Request $request, RawMaterial $rawMaterial): JsonResponse
{
$data = json_decode($request->getContent(), true, 512, JSON_THROW_ON_ERROR);
$errors = RawMaterialValidator::saveValidation($data);
$checkErrors = ValidatorHelper::checkErrorConstraint($errors, RawMaterial::class);
if (null !== $checkErrors) {
return $checkErrors;
}
$rawMaterial->fromArray($data);
$entityManager->persist($rawMaterial);
$entityManager->flush();
return new JsonResponse([], Response::HTTP_ACCEPTED);
}
#[Route("/{code}", name: "api_raw_materials_delete", methods: ["DELETE"])]
#[ParamConverter('rawMaterial', class: RawMaterial::class, options: ['code' => 'code'])]
public function delete(EntityManagerInterface $entityManager, RawMaterial $rawMaterial): JsonResponse
{
/** @var array<RecipeTestRawMaterial> $recipes */
$recipes = $entityManager->getRepository(RecipeTestRawMaterial::class)->findBy([
'rawMaterial' => $rawMaterial
]);
if (count($recipes) > 0) {
$toRet = [];
foreach ($recipes as $recipe) {
$toRet[] = [
'recipeLine' => $recipe->getRecipeTest()->getLine(),
'recipeCode' => $recipe->getRecipeTest()->getRecipe()->getCode(),
'' => $recipe->getRecipeTest()->getRecipe()->getProject()->getName()
];
}
return new JsonResponse($toRet, Response::HTTP_IM_USED);
} else {
$entityManager->remove($rawMaterial);
$entityManager->persist();
return new JsonResponse([], Response::HTTP_ACCEPTED);
}
}
}