add groups on household serialization

This commit is contained in:
Julien Fastré 2021-06-02 21:44:49 +02:00
parent 261acdfd24
commit e9caa7b4b8
4 changed files with 67 additions and 10 deletions

View File

@ -9,6 +9,7 @@ use Symfony\Component\Serializer\Exception;
use Symfony\Component\Routing\Annotation\Route; use Symfony\Component\Routing\Annotation\Route;
use Chill\MainBundle\CRUD\Controller\ApiController; use Chill\MainBundle\CRUD\Controller\ApiController;
use Chill\PersonBundle\Household\MembersEditor; use Chill\PersonBundle\Household\MembersEditor;
use Symfony\Component\Serializer\Normalizer\AbstractNormalizer;
class HouseholdMemberController extends ApiController class HouseholdMemberController extends ApiController
{ {
@ -27,24 +28,20 @@ class HouseholdMemberController extends ApiController
} catch (Exception\InvalidArgumentException | Exception\UnexpectedValueException $e) { } catch (Exception\InvalidArgumentException | Exception\UnexpectedValueException $e) {
throw new BadRequestException("Deserialization error: {$e->getMessage()}", 45896, $e); throw new BadRequestException("Deserialization error: {$e->getMessage()}", 45896, $e);
} }
dump($editor);
// TODO ACL // TODO ACL
// //
// TODO validation // TODO validation
// //
$em = $this->getDoctrine()->getManager(); $em = $this->getDoctrine()->getManager();
// to ensure closing membership before creating one, we must manually open a transaction
$em->beginTransaction();
foreach ($editor->getPersistable() as $el) { foreach ($editor->getPersistable() as $el) {
$em->persist($el); $em->persist($el);
} }
$em->flush(); $em->flush();
$em->commit();
return $this->json($editor->getHousehold(), Response::HTTP_OK, [], [
"groups" => ["read"],
return $this->json($editor->getHousehold(), Response::HTTP_OK, ["groups" => ["read"]]); ]);
} }
} }

View File

@ -26,7 +26,7 @@ class Household
* @ORM\Column(type="integer") * @ORM\Column(type="integer")
* @Serializer\Groups({"read"}) * @Serializer\Groups({"read"})
*/ */
private ?int $id; private ?int $id = null;
/** /**
* Addresses * Addresses
@ -44,6 +44,7 @@ class Household
* targetEntity=HouseholdMember::class, * targetEntity=HouseholdMember::class,
* mappedBy="household" * mappedBy="household"
* ) * )
* @Serializer\Groups({"read"})
*/ */
private Collection $members; private Collection $members;

View File

@ -6,6 +6,7 @@ use Doctrine\ORM\Mapping as ORM;
use Chill\PersonBundle\Entity\Person; use Chill\PersonBundle\Entity\Person;
use Chill\PersonBundle\Entity\Household\Household; use Chill\PersonBundle\Entity\Household\Household;
use Chill\PersonBundle\Entity\Household\Position; use Chill\PersonBundle\Entity\Household\Position;
use Symfony\Component\Serializer\Annotation as Serializer;
/** /**
@ -20,26 +21,31 @@ class HouseholdMember
* @ORM\Id * @ORM\Id
* @ORM\GeneratedValue * @ORM\GeneratedValue
* @ORM\Column(type="integer") * @ORM\Column(type="integer")
* @Serializer\Groups({"read"})
*/ */
private $id; private $id;
/** /**
* @ORM\ManyToOne(targetEntity=Position::class) * @ORM\ManyToOne(targetEntity=Position::class)
* @Serializer\Groups({"read"})
*/ */
private ?Position $position = null; private ?Position $position = null;
/** /**
* @ORM\Column(type="date_immutable", nullable=true, options={"default": null}) * @ORM\Column(type="date_immutable", nullable=true, options={"default": null})
* @Serializer\Groups({"read"})
*/ */
private ?\DateTimeImmutable $startDate = null; private ?\DateTimeImmutable $startDate = null;
/** /**
* @ORM\Column(type="date_immutable", nullable= true, options={"default": null}) * @ORM\Column(type="date_immutable", nullable= true, options={"default": null})
* @Serializer\Groups({"read"})
*/ */
private ?\DateTimeImmutable $endDate = null; private ?\DateTimeImmutable $endDate = null;
/** /**
* @ORM\Column(type="string", length=255, nullable=true) * @ORM\Column(type="string", length=255, nullable=true)
* @Serializer\Groups({"read"})
*/ */
private ?string $comment = NULL; private ?string $comment = NULL;
@ -50,6 +56,7 @@ class HouseholdMember
/** /**
* @ORM\Column(type="boolean", options={"default": false}) * @ORM\Column(type="boolean", options={"default": false})
* @Serializer\Groups({"read"})
*/ */
private bool $holder = false; private bool $holder = false;
@ -59,6 +66,7 @@ class HouseholdMember
* @ORM\ManyToOne( * @ORM\ManyToOne(
* targetEntity="\Chill\PersonBundle\Entity\Person" * targetEntity="\Chill\PersonBundle\Entity\Person"
* ) * )
* @Serializer\Groups({"read"})
*/ */
private ?Person $person = null; private ?Person $person = null;
@ -131,6 +139,9 @@ class HouseholdMember
return $this; return $this;
} }
/**
* @Serializer\Groups({"read"})
*/
public function getShareHousehold(): ?bool public function getShareHousehold(): ?bool
{ {
return $this->sharedHousehold; return $this->sharedHousehold;

View File

@ -0,0 +1,48 @@
<?php
namespace Chill\PersonBundle\Tests\Serializer\Normalizer;
use Chill\PersonBundle\Entity\Household\Position;
use Chill\PersonBundle\Entity\Household\Household;
use Chill\PersonBundle\Entity\Household\HouseholdMember;
use Chill\PersonBundle\Entity\Person;
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
use Symfony\Component\Serializer\SerializerInterface;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
class HouseholdNormalizerTest extends KernelTestCase
{
private ?NormalizerInterface $normalizer;
protected function setUp()
{
self::bootKernel();
$this->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']);
}
}