102 lines
2.9 KiB
PHP
102 lines
2.9 KiB
PHP
|
|
<?php
|
||
|
|
|
||
|
|
declare(strict_types=1);
|
||
|
|
|
||
|
|
namespace App\Controller;
|
||
|
|
|
||
|
|
use App\Entity\Todo;
|
||
|
|
use Doctrine\ORM\EntityManagerInterface;
|
||
|
|
use Symfony\Component\HttpFoundation\JsonResponse;
|
||
|
|
use Symfony\Component\HttpFoundation\Request;
|
||
|
|
use Symfony\Component\HttpFoundation\Response;
|
||
|
|
use Symfony\Component\Routing\Attribute\Route;
|
||
|
|
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Auto-generated CRUD controller for the Todo bridge resource.
|
||
|
|
* Edit freely — re-running make:bridge:resource won't overwrite this file.
|
||
|
|
*/
|
||
|
|
#[Route('/api/todos')]
|
||
|
|
final class TodoController
|
||
|
|
{
|
||
|
|
public function __construct(
|
||
|
|
private readonly EntityManagerInterface $em,
|
||
|
|
private readonly NormalizerInterface $normalizer,
|
||
|
|
) {
|
||
|
|
}
|
||
|
|
|
||
|
|
#[Route('', name: 'todo_list', methods: ['GET'])]
|
||
|
|
public function list(): JsonResponse
|
||
|
|
{
|
||
|
|
$items = $this->em->getRepository(Todo::class)->findAll();
|
||
|
|
|
||
|
|
return new JsonResponse($this->normalizer->normalize($items, 'json'));
|
||
|
|
}
|
||
|
|
|
||
|
|
#[Route('', name: 'todo_create', methods: ['POST'])]
|
||
|
|
public function create(Request $request): JsonResponse
|
||
|
|
{
|
||
|
|
$data = json_decode((string) $request->getContent(), true) ?? [];
|
||
|
|
$entity = new Todo();
|
||
|
|
|
||
|
|
if (isset($data['title'])) {
|
||
|
|
$entity->setTitle((string) $data['title']);
|
||
|
|
}
|
||
|
|
|
||
|
|
if (isset($data['done'])) {
|
||
|
|
$entity->setDone((bool) $data['done']);
|
||
|
|
}
|
||
|
|
|
||
|
|
$this->em->persist($entity);
|
||
|
|
$this->em->flush();
|
||
|
|
|
||
|
|
return new JsonResponse(
|
||
|
|
$this->normalizer->normalize($entity, 'json'),
|
||
|
|
Response::HTTP_CREATED,
|
||
|
|
);
|
||
|
|
}
|
||
|
|
|
||
|
|
#[Route('/{id}', name: 'todo_update', methods: ['PATCH'])]
|
||
|
|
public function update(string $id, Request $request): JsonResponse
|
||
|
|
{
|
||
|
|
$entity = $this->em->getRepository(Todo::class)->find($id);
|
||
|
|
|
||
|
|
if (null === $entity) {
|
||
|
|
return new JsonResponse(
|
||
|
|
['title' => 'Not Found', 'status' => 404],
|
||
|
|
Response::HTTP_NOT_FOUND,
|
||
|
|
['Content-Type' => 'application/problem+json'],
|
||
|
|
);
|
||
|
|
}
|
||
|
|
|
||
|
|
$data = json_decode((string) $request->getContent(), true) ?? [];
|
||
|
|
|
||
|
|
if (isset($data['title'])) {
|
||
|
|
$entity->setTitle((string) $data['title']);
|
||
|
|
}
|
||
|
|
|
||
|
|
if (isset($data['done'])) {
|
||
|
|
$entity->setDone((bool) $data['done']);
|
||
|
|
}
|
||
|
|
|
||
|
|
$this->em->flush();
|
||
|
|
|
||
|
|
return new JsonResponse($this->normalizer->normalize($entity, 'json'));
|
||
|
|
}
|
||
|
|
|
||
|
|
#[Route('/{id}', name: 'todo_delete', methods: ['DELETE'])]
|
||
|
|
public function delete(string $id): JsonResponse
|
||
|
|
{
|
||
|
|
$entity = $this->em->getRepository(Todo::class)->find($id);
|
||
|
|
|
||
|
|
if (null === $entity) {
|
||
|
|
return new JsonResponse(null, Response::HTTP_NO_CONTENT);
|
||
|
|
}
|
||
|
|
|
||
|
|
$this->em->remove($entity);
|
||
|
|
$this->em->flush();
|
||
|
|
|
||
|
|
return new JsonResponse(null, Response::HTTP_NO_CONTENT);
|
||
|
|
}
|
||
|
|
}
|