From e9caa7b4b86fcb2e5b015bd6b10de8d42fba44a7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julien=20Fastr=C3=A9?= Date: Wed, 2 Jun 2021 21:44:49 +0200 Subject: [PATCH] add groups on household serialization --- .../Controller/HouseholdMemberController.php | 15 +++--- .../Entity/Household/Household.php | 3 +- .../Entity/Household/HouseholdMember.php | 11 +++++ .../Normalizer/HouseholdNormalizerTest.php | 48 +++++++++++++++++++ 4 files changed, 67 insertions(+), 10 deletions(-) create mode 100644 src/Bundle/ChillPersonBundle/Tests/Serializer/Normalizer/HouseholdNormalizerTest.php diff --git a/src/Bundle/ChillPersonBundle/Controller/HouseholdMemberController.php b/src/Bundle/ChillPersonBundle/Controller/HouseholdMemberController.php index c6fd4dd59..eafdd2ae9 100644 --- a/src/Bundle/ChillPersonBundle/Controller/HouseholdMemberController.php +++ b/src/Bundle/ChillPersonBundle/Controller/HouseholdMemberController.php @@ -9,6 +9,7 @@ use Symfony\Component\Serializer\Exception; use Symfony\Component\Routing\Annotation\Route; use Chill\MainBundle\CRUD\Controller\ApiController; use Chill\PersonBundle\Household\MembersEditor; +use Symfony\Component\Serializer\Normalizer\AbstractNormalizer; class HouseholdMemberController extends ApiController { @@ -27,24 +28,20 @@ class HouseholdMemberController extends ApiController } catch (Exception\InvalidArgumentException | Exception\UnexpectedValueException $e) { throw new BadRequestException("Deserialization error: {$e->getMessage()}", 45896, $e); } - dump($editor); + // TODO ACL // // TODO validation // $em = $this->getDoctrine()->getManager(); - // to ensure closing membership before creating one, we must manually open a transaction - $em->beginTransaction(); - foreach ($editor->getPersistable() as $el) { $em->persist($el); } - $em->flush(); - $em->commit(); - - - return $this->json($editor->getHousehold(), Response::HTTP_OK, ["groups" => ["read"]]); + + return $this->json($editor->getHousehold(), Response::HTTP_OK, [], [ + "groups" => ["read"], + ]); } } diff --git a/src/Bundle/ChillPersonBundle/Entity/Household/Household.php b/src/Bundle/ChillPersonBundle/Entity/Household/Household.php index bbec0612a..f95b177f9 100644 --- a/src/Bundle/ChillPersonBundle/Entity/Household/Household.php +++ b/src/Bundle/ChillPersonBundle/Entity/Household/Household.php @@ -26,7 +26,7 @@ class Household * @ORM\Column(type="integer") * @Serializer\Groups({"read"}) */ - private ?int $id; + private ?int $id = null; /** * Addresses @@ -44,6 +44,7 @@ class Household * targetEntity=HouseholdMember::class, * mappedBy="household" * ) + * @Serializer\Groups({"read"}) */ private Collection $members; diff --git a/src/Bundle/ChillPersonBundle/Entity/Household/HouseholdMember.php b/src/Bundle/ChillPersonBundle/Entity/Household/HouseholdMember.php index 52bd969fc..cd58af565 100644 --- a/src/Bundle/ChillPersonBundle/Entity/Household/HouseholdMember.php +++ b/src/Bundle/ChillPersonBundle/Entity/Household/HouseholdMember.php @@ -6,6 +6,7 @@ use Doctrine\ORM\Mapping as ORM; use Chill\PersonBundle\Entity\Person; use Chill\PersonBundle\Entity\Household\Household; use Chill\PersonBundle\Entity\Household\Position; +use Symfony\Component\Serializer\Annotation as Serializer; /** @@ -20,26 +21,31 @@ class HouseholdMember * @ORM\Id * @ORM\GeneratedValue * @ORM\Column(type="integer") + * @Serializer\Groups({"read"}) */ private $id; /** * @ORM\ManyToOne(targetEntity=Position::class) + * @Serializer\Groups({"read"}) */ private ?Position $position = null; /** * @ORM\Column(type="date_immutable", nullable=true, options={"default": null}) + * @Serializer\Groups({"read"}) */ private ?\DateTimeImmutable $startDate = null; /** * @ORM\Column(type="date_immutable", nullable= true, options={"default": null}) + * @Serializer\Groups({"read"}) */ private ?\DateTimeImmutable $endDate = null; /** * @ORM\Column(type="string", length=255, nullable=true) + * @Serializer\Groups({"read"}) */ private ?string $comment = NULL; @@ -50,6 +56,7 @@ class HouseholdMember /** * @ORM\Column(type="boolean", options={"default": false}) + * @Serializer\Groups({"read"}) */ private bool $holder = false; @@ -59,6 +66,7 @@ class HouseholdMember * @ORM\ManyToOne( * targetEntity="\Chill\PersonBundle\Entity\Person" * ) + * @Serializer\Groups({"read"}) */ private ?Person $person = null; @@ -131,6 +139,9 @@ class HouseholdMember return $this; } + /** + * @Serializer\Groups({"read"}) + */ public function getShareHousehold(): ?bool { return $this->sharedHousehold; diff --git a/src/Bundle/ChillPersonBundle/Tests/Serializer/Normalizer/HouseholdNormalizerTest.php b/src/Bundle/ChillPersonBundle/Tests/Serializer/Normalizer/HouseholdNormalizerTest.php new file mode 100644 index 000000000..9dd348c82 --- /dev/null +++ b/src/Bundle/ChillPersonBundle/Tests/Serializer/Normalizer/HouseholdNormalizerTest.php @@ -0,0 +1,48 @@ +normalizer= self::$container->get(NormalizerInterface::class); + } + + public function testNormalizationRecursive() + { + $person = new Person(); + $member = new HouseholdMember(); + $household = new Household(); + $position = (new Position()) + ->setShareHousehold(true) + ->setAllowHolder(true) + ; + + $member->setPerson($person) + ->setStartDate(new \DateTimeImmutable('1 year ago')) + ->setEndDate(new \DateTimeImmutable('1 month ago')); + + $household->addMember($member); + + $normalized = $this->normalizer->normalize($household, + 'json', [ 'groups' => [ 'read' ]]); + + $this->assertIsArray($normalized); + $this->assertArrayHasKey('type', $normalized); + $this->assertEquals('household', $normalized['type']); + } + +}