Skip to content

Commit

Permalink
Practice #3: serializer
Browse files Browse the repository at this point in the history
  • Loading branch information
clementvtrd committed Nov 18, 2022
1 parent 432a511 commit aba9aba
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 33 deletions.
12 changes: 6 additions & 6 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 14 additions & 0 deletions config/serializer/dinosaur.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
App\Entity\Dinosaur:
attributes:
id:
groups: ['dinosaur', 'dinosaurs']
name:
groups: ['dinosaur', 'dinosaurs']
gender:
groups: ['dinosaur']
age:
groups: ['dinosaur']
eyesColor:
groups: ['dinosaur']
species:
groups: ['dinosaur']
28 changes: 18 additions & 10 deletions src/Controller/API/Dinosaurs/Create.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,16 @@
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Serializer\SerializerInterface;

class Create extends AbstractController
{
public function __construct(
private readonly SerializerInterface $serializer
)
{
}

#[Route('/api/dinosaurs', methods: 'POST')]
public function __invoke(ManagerRegistry $manager, Request $request): Response
{
Expand Down Expand Up @@ -46,17 +53,18 @@ public function __invoke(ManagerRegistry $manager, Request $request): Response
$em->persist($dinosaur);
$em->flush();

$content = $this->serializer->serialize(
$dinosaur,
'json',
['groups' => ['dinosaur']]
);

return new JsonResponse($content, Response::HTTP_CREATED, json: true);
} catch (Exception $e) {
return new JsonResponse([
'id' => $dinosaur->getId(),
'name' => $dinosaur->getName(),
'gender' => $dinosaur->getGender(),
'speciesId' => $dinosaur->getSpecies()->getId(),
'age' => $dinosaur->getAge(),
'eyesColor' => $dinosaur->getEyesColor(),
], Response::HTTP_CREATED);
} catch (Exception) {
return new JsonResponse([
'message' => 'Something went wrong'
'message' => 'Something went wrong',
'error' => $e->getMessage(),
'stack' => $e->getTraceAsString(),
], Response::HTTP_BAD_REQUEST);
}
}
Expand Down
22 changes: 13 additions & 9 deletions src/Controller/API/Dinosaurs/GetAll.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,16 @@
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Serializer\SerializerInterface;

class GetAll extends AbstractController
{
public function __construct(
private readonly SerializerInterface $serializer
)
{
}

#[Route('/api/dinosaurs', methods: 'GET')]
public function __invoke(ManagerRegistry $manager): Response
{
Expand All @@ -21,15 +28,12 @@ public function __invoke(ManagerRegistry $manager): Response
->findAll()
;

$dinosaurs = array_map(fn (Dinosaur $dinosaur) => [
'id' => $dinosaur->getId(),
'name' => $dinosaur->getName(),
'gender' => $dinosaur->getGender(),
'speciesId' => $dinosaur->getSpecies()->getId(),
'age' => $dinosaur->getAge(),
'eyesColor' => $dinosaur->getEyesColor(),
], $dinosaurs);
$content = $this->serializer->serialize(
$dinosaurs,
'json',
['groups' => ['dinosaurs']]
);

return new JsonResponse($dinosaurs);
return new JsonResponse($content, json: true);
}
}
22 changes: 14 additions & 8 deletions src/Controller/API/Dinosaurs/GetOne.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,16 @@
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Serializer\SerializerInterface;

class GetOne extends AbstractController
{
public function __construct(
private readonly SerializerInterface $serializer
)
{
}

#[Route('/api/dinosaurs/{id}', methods: 'GET')]
public function __invoke(ManagerRegistry $manager, string $id): Response
{
Expand All @@ -27,13 +34,12 @@ public function __invoke(ManagerRegistry $manager, string $id): Response
], Response::HTTP_NOT_FOUND);
}

return new JsonResponse([
'id' => $dinosaur->getId(),
'name' => $dinosaur->getName(),
'gender' => $dinosaur->getGender(),
'speciesId' => $dinosaur->getSpecies()->getId(),
'age' => $dinosaur->getAge(),
'eyesColor' => $dinosaur->getEyesColor(),
]);
$content = $this->serializer->serialize(
$dinosaur,
'json',
['groups' => ['dinosaur']]
);

return new JsonResponse($content, json: true);
}
}

0 comments on commit aba9aba

Please sign in to comment.