This commit is contained in:
Jan Klattenhoff 2024-03-13 06:43:01 +01:00
parent 2a1ce070a2
commit fe4e4fa940
11 changed files with 100 additions and 6 deletions

View file

@ -9,6 +9,7 @@ use App\Service\PrinterService;
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;
class PrinterCrudController extends AbstractController
@ -29,10 +30,11 @@ class PrinterCrudController extends AbstractController
#[Route('/printer/{id}', name: 'get_printer', methods: ['GET'])]
public function getPrinter(?Printer $printer): JsonResponse
{
//TODO: ungleich null und in funktion auslagern
if (!$printer) {
return $this->json([
'message' => ErrorMessages::DOESNT_EXIST->value,
]);
], Response::HTTP_NOT_FOUND);
}
return $this->json($printer);
@ -44,7 +46,7 @@ class PrinterCrudController extends AbstractController
if (!$printer) {
return $this->json([
'message' => ErrorMessages::DOESNT_EXIST->value,
]);
], Response::HTTP_NOT_FOUND);
}
$this->printerService->deletePrinter($printer);
@ -61,7 +63,7 @@ class PrinterCrudController extends AbstractController
if (!$this->printerService->validateJson($jsonContent)) {
return $this->json([
'message' => ErrorMessages::DATA_INCOMPLETE->value,
]);
], Response::HTTP_NOT_FOUND);
}
$printer = $this->printerService->createPrinter($jsonContent);
@ -75,7 +77,7 @@ class PrinterCrudController extends AbstractController
if (!$printer) {
return $this->json([
'message' => ErrorMessages::DOESNT_EXIST->value,
]);
], Response::HTTP_NOT_FOUND);
}
$this->printerService->editPrinter($printer, $request->getContent());

View file

@ -15,6 +15,7 @@ class Printer implements JsonSerializable
#[ORM\Column]
private ?int $id = null;
//TODO doctrine required
#[ORM\Column(length: 255)]
private ?string $name = null;

View file

@ -22,6 +22,7 @@ class PrinterService
public function createPrinter(string $jsonString): Printer
{
//TODO use serializer for validation
$printer = $this->serializer->deserialize($jsonString, Printer::class, 'json');
$this->entityManager->persist($printer);
@ -33,11 +34,13 @@ class PrinterService
public function validateJson(string $jsonString): bool
{
$array = json_decode($jsonString, true);
//TODO: find another way
return array_keys($array) == self::REQUIRED_PROPERTIES;
}
public function editPrinter(Printer $printer, string $json): Printer
{
//TODO use deserialize
$jsonArray = json_decode($json, true);
$printer->setName($jsonArray['name'] ?? $printer->getName());