Skip to content

Commit 8fb5b6e

Browse files
Merge pull request #3 from freelancerwebro/feature/update_readme
Implement band validation
2 parents 68f79e4 + 7b7b8c1 commit 8fb5b6e

File tree

6 files changed

+96
-7
lines changed

6 files changed

+96
-7
lines changed

config/services.yaml

+4-1
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,7 @@ services:
2828

2929
App\Validator\ExcelFileValidator:
3030
arguments:
31-
$validator: '@validator'
31+
$validator: '@validator'
32+
33+
App\Serializer\BandNormalizer:
34+
tags: [ 'serializer.normalizer' ]

src/Controller/BandController.php

+8-4
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ public function create(
100100
'json'
101101
);
102102

103-
$errors = $validator->validate($band);
103+
$errors = $validator->validate($band, null, ['create']);
104104
if (count($errors) > 0) {
105105
return $this->json($errors, 422);
106106
}
@@ -135,15 +135,19 @@ public function create(
135135
*/
136136
#[Route('/band/{id}', name: 'app_band_update', methods: ['PUT'])]
137137
public function update(
138+
int $id,
138139
Request $request,
139140
SerializerInterface $serializer,
140141
ValidatorInterface $validator,
141-
Band $band,
142142
): JsonResponse {
143+
$band = $this->bandRepository->find($id);
144+
if (!$band) {
145+
return $this->json(['error' => 'Band not found'], 404);
146+
}
147+
143148
$band = $serializer->deserialize(
144149
$request->getContent(),
145150
Band::class,
146-
147151
'json',
148152
[
149153
AbstractNormalizer::OBJECT_TO_POPULATE => $band,
@@ -155,7 +159,7 @@ public function update(
155159
return $this->json($errors, 422);
156160
}
157161

158-
$this->bandRepository->save($band);
162+
$this->bandRepository->flush();
159163

160164
return $this->json($band);
161165
}

src/Entity/Band.php

+11
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use App\Repository\BandRepository;
66
use Doctrine\DBAL\Types\Types;
77
use Doctrine\ORM\Mapping as ORM;
8+
use Symfony\Component\Validator\Constraints as Assert;
89

910
#[ORM\Entity(repositoryClass: BandRepository::class)]
1011
class Band
@@ -15,24 +16,34 @@ class Band
1516
private ?int $id = null;
1617

1718
#[ORM\Column(length: 30)]
19+
#[Assert\NotBlank(message: 'Name is required.', groups: ['create'])]
1820
private ?string $name = null;
1921

2022
#[ORM\Column(length: 30)]
23+
#[Assert\NotBlank(message: 'Origin is required.', groups: ['create'])]
2124
private ?string $origin = null;
2225

2326
#[ORM\Column(length: 30)]
27+
#[Assert\NotBlank(message: 'City is required.', groups: ['create'])]
2428
private ?string $city = null;
2529

2630
#[ORM\Column(type: Types::SMALLINT)]
31+
#[Assert\NotBlank(message: 'Start year is required.', groups: ['create'])]
32+
#[Assert\Type(type: 'integer', message: 'Start year must be an integer.')]
33+
#[Assert\Range(notInRangeMessage: 'Start year must be between 1900 and 2100.', min: 1900, max: 2100)]
2734
private ?int $startYear = null;
2835

2936
#[ORM\Column(type: Types::SMALLINT, nullable: true)]
37+
#[Assert\Type(type: 'integer', message: 'Separation year must be an integer.')]
38+
#[Assert\Range(notInRangeMessage: 'Separation year must be between 1900 and 2100.', min: 1900, max: 2100)]
3039
private ?int $separationYear = null;
3140

3241
#[ORM\Column(length: 255, nullable: true)]
3342
private ?string $founders = null;
3443

3544
#[ORM\Column(type: Types::SMALLINT, nullable: true)]
45+
#[Assert\Type(type: 'integer', message: 'Members must be an integer.')]
46+
#[Assert\Range(notInRangeMessage: 'Members must be between 1 and 50.', min: 1, max: 50)]
3647
private ?int $members = null;
3748

3849
#[ORM\Column(length: 30, nullable: true)]

src/Repository/BandRepository.php

+5
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,11 @@ public function save(Band $band)
2222
$this->getEntityManager()->flush();
2323
}
2424

25+
public function flush()
26+
{
27+
$this->getEntityManager()->flush();
28+
}
29+
2530
public function delete(Band $band)
2631
{
2732
$this->getEntityManager()->remove($band);

src/Serializer/BandNormalizer.php

+66
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace App\Serializer;
6+
7+
use App\Entity\Band;
8+
use Symfony\Component\Serializer\Normalizer\AbstractNormalizer;
9+
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
10+
11+
class BandNormalizer implements DenormalizerInterface
12+
{
13+
public function denormalize(mixed $data, string $type, ?string $format = null, array $context = []): mixed
14+
{
15+
if (Band::class !== $type) {
16+
return null;
17+
}
18+
$existingBand = $context[AbstractNormalizer::OBJECT_TO_POPULATE] ?? null;
19+
20+
if ($existingBand instanceof Band) {
21+
$band = $existingBand;
22+
} else {
23+
$band = new Band();
24+
}
25+
26+
if (isset($data['name'])) {
27+
$band->setName($data['name']);
28+
}
29+
if (isset($data['origin'])) {
30+
$band->setOrigin($data['origin']);
31+
}
32+
if (isset($data['city'])) {
33+
$band->setCity($data['city']);
34+
}
35+
if (isset($data['startYear'])) {
36+
$band->setStartYear((int) $data['startYear']);
37+
}
38+
if (isset($data['separationYear'])) {
39+
$band->setSeparationYear((int) $data['separationYear']);
40+
}
41+
if (isset($data['founders'])) {
42+
$band->setFounders($data['founders']);
43+
}
44+
if (isset($data['members'])) {
45+
$band->setMembers((int) $data['members']);
46+
}
47+
if (isset($data['musicalCurrent'])) {
48+
$band->setMusicalCurrent($data['musicalCurrent']);
49+
}
50+
if (isset($data['presentation'])) {
51+
$band->setPresentation($data['presentation']);
52+
}
53+
54+
return $band;
55+
}
56+
57+
public function supportsDenormalization(mixed $data, string $type, ?string $format = null, array $context = []): bool
58+
{
59+
return Band::class === $type;
60+
}
61+
62+
public function getSupportedTypes(?string $format): array
63+
{
64+
return [Band::class => true];
65+
}
66+
}

tests/Functional/Controller/BandControllerTest.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -154,8 +154,8 @@ public function test_update_nonexistent_band()
154154
);
155155

156156
$response = $this->client->getResponse();
157-
$this->assertEquals(Response::HTTP_BAD_REQUEST, $response->getStatusCode());
158-
$this->assertStringContainsString('object not found', $response->getContent());
157+
$this->assertEquals(Response::HTTP_NOT_FOUND, $response->getStatusCode());
158+
$this->assertStringContainsString('Band not found', $response->getContent());
159159
}
160160

161161
public function test_get_nonexistent_url()

0 commit comments

Comments
 (0)