chore: created FrontendController

This commit is contained in:
Jan Klattenhoff 2024-01-18 13:30:39 +01:00
parent 7311f038e9
commit e6a3fd881a
8 changed files with 80 additions and 9 deletions

View file

@ -0,0 +1,19 @@
<?php
namespace App\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\Routing\Annotation\Route;
class FrontendController extends AbstractController
{
#[Route('/frontend', name: 'app_frontend')]
public function index(): JsonResponse
{
return $this->json([
'message' => 'Welcome to your new controller!',
'path' => 'src/Controller/FrontendController.php',
]);
}
}

View file

@ -77,12 +77,9 @@ class PrinterCrudController extends AbstractController
'message' => ErrorMessages::DOESNT_EXIST->value,
]);
}
if (!$this->printerService->validateJson($request->getContent())) {
return $this->json([
'message' => ErrorMessages::DATA_INCOMPLETE,
]);
}
$this->printerService->editPrinter($printer, $request->getContent());
return $this->json($printer);
}
}

View file

@ -8,6 +8,8 @@ use Symfony\Component\Serializer\SerializerInterface;
class PrinterService
{
private const REQUIRED_PROPERTIES = ['name', 'price', 'build_volume', 'max_speed'];
public function __construct(private readonly EntityManagerInterface $entityManager, private readonly SerializerInterface $serializer)
{
}
@ -30,6 +32,20 @@ class PrinterService
public function validateJson(string $jsonString): bool
{
$array = json_decode($jsonString, true);
return isset($array['name'], $array['price'], $array['max_speed'], $array['build_volume']);
return array_keys($array) == self::REQUIRED_PROPERTIES;
}
public function editPrinter(Printer $printer, string $json): Printer
{
$jsonArray = json_decode($json, true);
$printer->setName($jsonArray['name'] ?? $printer->getName());
$printer->setPrice($jsonArray['price'] ?? $printer->getPrice());
$printer->setBuildVolume($jsonArray['build_volume'] ?? $printer->getBuildVolume());
$printer->setMaxSpeed($jsonArray['max_speed'] ?? $printer->getMaxSpeed());
$this->entityManager->flush();
return $printer;
}
}