endofday: 17.01.2024

This commit is contained in:
Jan Klattenhoff 2024-01-17 17:17:12 +01:00
commit 9286839d99
18 changed files with 1835 additions and 32 deletions

View file

@ -2,18 +2,33 @@
namespace App\Controller;
use App\Entity\Printer;
use App\Repository\PrinterRepository;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\Routing\Annotation\Route;
class PrinterCrudController extends AbstractController
{
#[Route('/printer/crud', name: 'app_printer_crud')]
public function index(): JsonResponse
public function __construct(private readonly PrinterRepository $printerRepository)
{
return $this->json([
'message' => 'Welcome to your new controller!',
'path' => 'src/Controller/PrinterCrudController.php',
]);
}
#[Route('/printer', name: 'printer')]
public function printer(): JsonResponse
{
return $this->json($this->printerRepository->findAll());
}
#[Route('/printer/{id}', name: 'app_printer', methods: ["GET"])]
public function index(?Printer $printer): JsonResponse
{
if (!$printer) {
return $this->json([
'message' => 'printer does not exist',
]);
}
return $this->json($printer);
}
}

View file

@ -4,9 +4,11 @@ namespace App\Entity;
use App\Repository\PrinterRepository;
use Doctrine\ORM\Mapping as ORM;
use JsonSerializable;
use PHPUnit\Util\Json;
#[ORM\Entity(repositoryClass: PrinterRepository::class)]
class Printer
class Printer implements JsonSerializable
{
#[ORM\Id]
#[ORM\GeneratedValue]
@ -77,4 +79,15 @@ class Printer
return $this;
}
public function jsonSerialize(): array
{
return [
'id' => $this->id,
'name' => $this->name,
'price' => $this->price,
'build_volume' => $this->buildVolume,
'max_speed' => $this->maxSpeed
];
}
}