From e6a3fd881a13c642f938943cc973a8069429d73d Mon Sep 17 00:00:00 2001 From: Jan Klattenhoff Date: Thu, 18 Jan 2024 13:30:39 +0100 Subject: [PATCH] chore: created FrontendController --- .idea/dataSources.xml | 4 ++-- .idea/php.xml | 7 +++++++ .idea/symfony_crud.iml | 7 +++++++ config/packages/security.yaml | 1 - src/Controller/FrontendController.php | 19 +++++++++++++++++ src/Controller/PrinterCrudController.php | 7 ++----- src/Service/PrinterService.php | 18 +++++++++++++++- tests/Service/PrinterServiceTest.php | 26 ++++++++++++++++++++++++ 8 files changed, 80 insertions(+), 9 deletions(-) create mode 100644 src/Controller/FrontendController.php diff --git a/.idea/dataSources.xml b/.idea/dataSources.xml index 2474fb9..64a9475 100644 --- a/.idea/dataSources.xml +++ b/.idea/dataSources.xml @@ -1,13 +1,13 @@ - + mariadb true true DDEV generated data source org.mariadb.jdbc.Driver - jdbc:mariadb://127.0.0.1:27702/db?user=db&password=db + jdbc:mariadb://127.0.0.1:32794/db?user=db&password=db $ProjectFileDir$ diff --git a/.idea/php.xml b/.idea/php.xml index d1a8cde..ab9322d 100644 --- a/.idea/php.xml +++ b/.idea/php.xml @@ -97,6 +97,13 @@ + + + + + + + diff --git a/.idea/symfony_crud.iml b/.idea/symfony_crud.iml index f68ff09..07001a2 100644 --- a/.idea/symfony_crud.iml +++ b/.idea/symfony_crud.iml @@ -37,6 +37,7 @@ + @@ -57,6 +58,7 @@ + @@ -73,6 +75,7 @@ + @@ -82,6 +85,10 @@ + + + + diff --git a/config/packages/security.yaml b/config/packages/security.yaml index a08d6d1..367af25 100644 --- a/config/packages/security.yaml +++ b/config/packages/security.yaml @@ -12,7 +12,6 @@ security: main: lazy: true provider: users_in_memory - custom_authenticator: App\Security\PrinterAuthenticator # activate different ways to authenticate # https://symfony.com/doc/current/security.html#the-firewall diff --git a/src/Controller/FrontendController.php b/src/Controller/FrontendController.php new file mode 100644 index 0000000..20b2c47 --- /dev/null +++ b/src/Controller/FrontendController.php @@ -0,0 +1,19 @@ +json([ + 'message' => 'Welcome to your new controller!', + 'path' => 'src/Controller/FrontendController.php', + ]); + } +} diff --git a/src/Controller/PrinterCrudController.php b/src/Controller/PrinterCrudController.php index aa73abe..1736e7d 100644 --- a/src/Controller/PrinterCrudController.php +++ b/src/Controller/PrinterCrudController.php @@ -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); } } diff --git a/src/Service/PrinterService.php b/src/Service/PrinterService.php index f4cde2b..94945f9 100644 --- a/src/Service/PrinterService.php +++ b/src/Service/PrinterService.php @@ -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; } } \ No newline at end of file diff --git a/tests/Service/PrinterServiceTest.php b/tests/Service/PrinterServiceTest.php index 8bfe064..6e76f39 100644 --- a/tests/Service/PrinterServiceTest.php +++ b/tests/Service/PrinterServiceTest.php @@ -80,4 +80,30 @@ class PrinterServiceTest extends TestCase $this->printerService->createPrinter($json); } + + #[Test] + public function editPrinterShouldUpdatePrinterWithJsonContent(){ + $json = '{ + "name": "Bambu A1 Mini", + }'; + + $printer = new Printer(); + $printer + ->setName('Bambu A1') + ->setPrice(10.50) + ->setBuildVolume('180x180x180') + ->setMaxSpeed(1000); + + + $expectedPrinter = new Printer(); + $expectedPrinter + ->setName('Bambu A1 Mini') + ->setPrice(10.50) + ->setBuildVolume('180x180x180') + ->setMaxSpeed(1000); + + $this->entityManager->expects(self::once())->method('flush'); + + self::assertEquals($expectedPrinter, $this->printerService->editPrinter($printer, $json)); + } }