Skip to content

Commit 8a816c5

Browse files
Practice 3: Serializer
1 parent 4233847 commit 8a816c5

File tree

4 files changed

+60
-27
lines changed

4 files changed

+60
-27
lines changed

config/serializer/dinosaur.yaml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
App\Entity\Dinosaur:
2+
attributes:
3+
id:
4+
groups: ['dinosaur', 'dinosaurs']
5+
name:
6+
groups: ['dinosaur', 'dinosaurs']
7+
gender:
8+
groups: ['dinosaur']
9+
age:
10+
groups: ['dinosaur']
11+
eyesColor:
12+
groups: ['dinosaur']
13+
species:
14+
groups: ['dinosaur']

src/Controller/API/Dinosaurs/Create.php

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,15 @@
1212
use Symfony\Component\HttpFoundation\Request;
1313
use Symfony\Component\HttpFoundation\Response;
1414
use Symfony\Component\Routing\Annotation\Route;
15+
use Symfony\Component\Serializer\SerializerInterface;
1516

1617
final class Create extends AbstractController
1718
{
19+
public function __construct(
20+
private readonly SerializerInterface $serializer
21+
) {
22+
}
23+
1824
#[Route('/api/dinosaurs', methods: 'POST')]
1925
public function __invoke(ManagerRegistry $manager, Request $request): Response
2026
{
@@ -44,18 +50,23 @@ public function __invoke(ManagerRegistry $manager, Request $request): Response
4450
$em->persist($dinosaur);
4551
$em->flush();
4652

53+
$content = $this->serializer->serialize(
54+
$dinosaur,
55+
'json',
56+
['groups' => ['dinosaur']]
57+
);
58+
59+
return new JsonResponse(
60+
$content,
61+
Response::HTTP_CREATED,
62+
json: true
63+
);
64+
} catch (\Exception $e) {
4765
return new JsonResponse([
48-
'id' => $dinosaur->getId(),
49-
'name' => $dinosaur->getName(),
50-
'gender' => $dinosaur->getGender(),
51-
'speciesId' => $dinosaur->getSpecies()->getId(),
52-
'age' => $dinosaur->getAge(),
53-
'eyesColor' => $dinosaur->getEyesColor(),
66+
'message' => 'Something went wrong',
67+
'error' => $e->getMessage(),
68+
'stack' => $e->getTraceAsString(),
5469
], Response::HTTP_CREATED);
55-
} catch (\Exception) {
56-
return new JsonResponse([
57-
'message' => 'Something went wrong'
58-
], Response::HTTP_BAD_REQUEST);
5970
}
6071
}
6172
}

src/Controller/API/Dinosaurs/GetAll.php

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,25 +10,28 @@
1010
use Symfony\Component\HttpFoundation\JsonResponse;
1111
use Symfony\Component\HttpFoundation\Response;
1212
use Symfony\Component\Routing\Annotation\Route;
13+
use Symfony\Component\Serializer\SerializerInterface;
1314

1415
final class GetAll extends AbstractController
1516
{
17+
public function __construct(
18+
private readonly SerializerInterface $serializer
19+
) {
20+
}
21+
1622
#[Route('/api/dinosaurs', methods: 'GET')]
1723
public function __invoke(ManagerRegistry $manager): Response
1824
{
1925
$dinosaurs = $manager
2026
->getRepository(Dinosaur::class)
2127
->findAll();
2228

23-
$dinosaurs = array_map(fn (Dinosaur $dinosaur) => [
24-
'id' => $dinosaur->getId(),
25-
'name' => $dinosaur->getName(),
26-
'gender' => $dinosaur->getGender(),
27-
'speciesId' => $dinosaur->getSpecies()->getId(),
28-
'age' => $dinosaur->getAge(),
29-
'eyesColor' => $dinosaur->getEyesColor(),
30-
], $dinosaurs);
29+
$content = $this->serializer->serialize(
30+
$dinosaurs,
31+
'json',
32+
['groups' => ['dinosaurs']]
33+
);
3134

32-
return new JsonResponse($dinosaurs);
35+
return new JsonResponse($content, json: true);
3336
}
3437
}

src/Controller/API/Dinosaurs/GetOne.php

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,15 @@
1010
use Symfony\Component\HttpFoundation\JsonResponse;
1111
use Symfony\Component\HttpFoundation\Response;
1212
use Symfony\Component\Routing\Annotation\Route;
13+
use Symfony\Component\Serializer\SerializerInterface;
1314

1415
final class GetOne extends AbstractController
1516
{
17+
public function __construct(
18+
private readonly SerializerInterface $serializer
19+
) {
20+
}
21+
1622
#[Route('/api/dinosaurs/{id}', methods: 'GET')]
1723
public function __invoke(ManagerRegistry $manager, string $id): Response
1824
{
@@ -26,13 +32,12 @@ public function __invoke(ManagerRegistry $manager, string $id): Response
2632
], Response::HTTP_NOT_FOUND);
2733
}
2834

29-
return new JsonResponse([
30-
'id' => $dinosaur->getId(),
31-
'name' => $dinosaur->getName(),
32-
'gender' => $dinosaur->getGender(),
33-
'speciesId' => $dinosaur->getSpecies()->getId(),
34-
'age' => $dinosaur->getAge(),
35-
'eyesColor' => $dinosaur->getEyesColor(),
36-
]);
35+
$content = $this->serializer->serialize(
36+
$dinosaur,
37+
'json',
38+
['groups' => ['dinosaur']]
39+
);
40+
41+
return new JsonResponse($content, json: true);
3742
}
3843
}

0 commit comments

Comments
 (0)