relation and relationship entities created + endpoints. Still something wrong with link between Relation and Relationship

This commit is contained in:
Julie Lenaerts 2021-10-22 19:21:32 +02:00
parent 6ff80be88d
commit 05d16ec9ab
9 changed files with 539 additions and 7 deletions

View File

@ -0,0 +1,38 @@
<?php
declare(strict_types=1);
namespace Chill\PersonBundle\Controller;
use Chill\MainBundle\CRUD\Controller\ApiController;
use Chill\PersonBundle\Entity\Person;
use Chill\PersonBundle\Repository\Relationships\RelationshipRepository;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Validator\Validator\ValidatorInterface;
use Symfony\Component\Routing\Annotation\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
class RelationshipApiController extends ApiController
{
private ValidatorInterface $validator;
private RelationshipRepository $repository;
public function __construct(ValidatorInterface $validator, RelationshipRepository $repository)
{
$this->validator = $validator;
$this->repository = $repository;
}
/**
* @Route("/api/1.0/relation/relationship/by-person/{person_id}.json",
* name="chill_relation_relationship_by_person")
*
* @ParamConverter("person", options={"id" = "person_id"})
*/
public function getRelationshipsByPerson(Person $person)
{
//TODO: add permissions? (voter?)
$relationships = $this->repository->findByPerson($person);
return $this->json(\array_values($relationships), Response::HTTP_OK, [], ['groups' => [ 'read']]);
}
}

View File

@ -587,7 +587,7 @@ class ChillPersonExtension extends Extension implements PrependExtensionInterfac
],
'findAccompanyingPeriodsByPerson' => [
'path' => '/by-person/{person_id}.{_format}',
'controller_action' => 'findAccompanyingPeriodsByPerson',
'controller_action' => 'getAccompanyingPeriodsByPerson',
'methods' => [
Request::METHOD_GET => true,
Request::METHOD_HEAD => true,
@ -870,6 +870,41 @@ class ChillPersonExtension extends Extension implements PrependExtensionInterfac
],
]
],
[
'class' => \Chill\PersonBundle\Entity\Relationships\Relationship::class,
'controller' => \Chill\PersonBundle\Controller\RelationshipApiController::class,
'name' => 'relationship_by_person',
'base_path' => '/api/1.0/relations/relationship',
'base_role' => 'ROLE_USER',
'actions' => [
'_entity' => [
'methods' => [
Request::METHOD_GET => true,
Request::METHOD_HEAD => true,
Request::METHOD_POST => true,
Request::METHOD_PATCH => true
],
'roles' => [
Request::METHOD_GET => 'ROLE_USER',
Request::METHOD_HEAD => 'ROLE_USER',
Request::METHOD_POST => 'ROLE_USER',
Request::METHOD_PATCH => 'ROLE_USER'
]
],
'relationship-by-person' => [
'path' => '/by-person/{person_id}.json',
'controller_action' => 'getRelationshipsByPerson',
'methods' => [
Request::METHOD_GET => true,
Request::METHOD_HEAD => true,
],
'roles' => [
Request::METHOD_GET => 'ROLE_USER',
Request::METHOD_HEAD => 'ROLE_USER',
]
],
]
]
]
]);
}

View File

@ -0,0 +1,57 @@
<?php
namespace Chill\PersonBundle\Entity\Relationships;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity()
*/
class Relation
{
/**
* @ORM\Id
* @ORM\GeneratedValue
* @ORM\Column(type="integer")
*/
private $id;
/**
* @ORM\Column(type="json", nullable=true)
*/
private $title = [];
/**
* @ORM\Column(type="json", nullable=true)
*/
private $reverseTitle = [];
public function getId(): ?int
{
return $this->id;
}
public function getTitle(): ?array
{
return $this->title;
}
public function setTitle(?array $title): self
{
$this->title = $title;
return $this;
}
public function getReverseTitle(): ?array
{
return $this->reverseTitle;
}
public function setReverseTitle(?array $reverseTitle): self
{
$this->reverseTitle = $reverseTitle;
return $this;
}
}

View File

@ -0,0 +1,182 @@
<?php
namespace Chill\PersonBundle\Entity\Relationships;
use Chill\MainBundle\Entity\User;
use Chill\PersonBundle\Entity\Person;
use Chill\PersonBundle\Entity\Relationships\Relation;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Component\Serializer\Annotation\Groups;
/**
* @ORM\Entity()
* @ORM\Table(name="chill_person_relationships")
*/
class Relationship
{
/**
* @ORM\Id
* @ORM\GeneratedValue
* @ORM\Column(type="integer")
* @Groups({"read"})
*/
private $id;
/**
* @ORM\ManyToOne(targetEntity=Person::class)
* @ORM\JoinColumn(nullable=false)
* @Assert\NotBlank()
* @Groups({"read", "write"})
*/
private $fromPerson;
/**
* @ORM\ManyToOne(targetEntity=Person::class)
* @ORM\JoinColumn(nullable=false)
* @Assert\NotBlank()
* @Groups({"read", "write"})
*/
private $toPerson;
/**
* @ORM\ManyToOne(targetEntity=Relation::class, inversedBy="relationships")
* @ORM\JoinColumn(nullable=false)
* @Assert\NotBlank()
* @Groups({"read", "write"})
*/
private $relation;
/**
* @ORM\Column(type="boolean")
* @Assert\Type(
* type="bool",
* message="This must be of type boolean"
* )
* @Groups({"read"})
*/
private $reverse;
/**
* @ORM\ManyToOne(targetEntity=User::class)
* @ORM\JoinColumn(nullable=false)
*/
private $createdBy;
/**
* @ORM\Column(type="datetime_immutable")
*/
private $createdAt;
/**
* @ORM\ManyToOne(targetEntity=User::class)
*/
private $updatedBy;
/**
* @ORM\Column(type="datetime_immutable", nullable=true)
*/
private $updatedAt;
public function getId(): ?int
{
return $this->id;
}
public function getFromPerson(): ?Person
{
return $this->fromPerson;
}
public function setFromPerson(?Person $fromPerson): self
{
$this->fromPerson = $fromPerson;
return $this;
}
public function getToPerson(): ?Person
{
return $this->toPerson;
}
public function setToPerson(?Person $toPerson): self
{
$this->toPerson = $toPerson;
return $this;
}
public function getReverse(): ?bool
{
return $this->reverse;
}
public function setReverse(bool $reverse): self
{
$this->reverse = $reverse;
return $this;
}
public function getCreatedBy(): ?User
{
return $this->createdBy;
}
public function setCreatedBy(?User $createdBy): self
{
$this->createdBy = $createdBy;
return $this;
}
public function getCreatedAt(): ?\DateTimeImmutable
{
return $this->createdAt;
}
public function setCreatedAt(\DateTimeImmutable $createdAt): self
{
$this->createdAt = $createdAt;
return $this;
}
public function getUpdatedBy(): ?User
{
return $this->updatedBy;
}
public function setUpdatedBy(?User $updatedBy): self
{
$this->updatedBy = $updatedBy;
return $this;
}
public function getUpdatedAt(): ?\DateTimeImmutable
{
return $this->updatedAt;
}
public function setUpdatedAt(?\DateTimeImmutable $updatedAt): self
{
$this->updatedAt = $updatedAt;
return $this;
}
public function getRelation(): ?Relation
{
return $this->relation;
}
public function setRelation(?Relation $relation): self
{
$this->relation = $relation;
return $this;
}
}

View File

@ -0,0 +1,50 @@
<?php
namespace Chill\PersonBundle\Repository\Relationships;
use App\Entity\Relation;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\Persistence\ManagerRegistry;
/**
* @method Relation|null find($id, $lockMode = null, $lockVersion = null)
* @method Relation|null findOneBy(array $criteria, array $orderBy = null)
* @method Relation[] findAll()
* @method Relation[] findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
*/
class RelationRepository extends ServiceEntityRepository
{
public function __construct(ManagerRegistry $registry)
{
parent::__construct($registry, Relation::class);
}
// /**
// * @return Relation[] Returns an array of Relation objects
// */
/*
public function findByExampleField($value)
{
return $this->createQueryBuilder('r')
->andWhere('r.exampleField = :val')
->setParameter('val', $value)
->orderBy('r.id', 'ASC')
->setMaxResults(10)
->getQuery()
->getResult()
;
}
*/
/*
public function findOneBySomeField($value): ?Relation
{
return $this->createQueryBuilder('r')
->andWhere('r.exampleField = :val')
->setParameter('val', $value)
->getQuery()
->getOneOrNullResult()
;
}
*/
}

View File

@ -0,0 +1,38 @@
<?php
namespace Chill\PersonBundle\Repository\Relationships;
use Chill\PersonBundle\Entity\Relationships\Relationship;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\Persistence\ManagerRegistry;
/**
* @method Relationship|null find($id, $lockMode = null, $lockVersion = null)
* @method Relationship|null findOneBy(array $criteria, array $orderBy = null)
* @method Relationship[] findAll()
* @method Relationship[] findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
*/
class RelationshipRepository extends ServiceEntityRepository
{
public function __construct(ManagerRegistry $registry)
{
parent::__construct($registry, Relationship::class);
}
// /**
// * @return Relationship[] Returns an array of Relationship objects linked to certain person.
// */
public function findByPerson($personId)
{
// return all relationships of which person is part? or only where person is the fromPerson?
return $this->createQueryBuilder('r')
->andWhere('r.fromPerson = :val')
->orWhere('r.toPerson = :val')
->setParameter('val', $personId)
->getQuery()
->getResult()
;
}
}

View File

@ -274,6 +274,41 @@ components:
enum:
- "social_work_goal"
RelationById:
type: object
properties:
id:
type: integer
type:
type: string
enum:
- "relation"
required:
- id
- type
Relationship:
type: object
properties:
type:
type: string
enum:
- "relationship"
id:
type: integer
readOnly: true
fromPerson:
anyOf:
- $ref: "#/components/schemas/PersonById"
toPerson:
anyOf:
- $ref: "#/components/schemas/PersonById"
relation:
anyOf:
- $ref: "#/components/schemas/RelationById"
reverse:
type: boolean
paths:
/1.0/person/person/{id}.json:
get:
@ -1589,3 +1624,50 @@ paths:
description: "OK"
400:
description: "Bad Request"
/1.0/relations/relationship/by-person/{id}.json:
get:
tags:
- relationships
parameters:
- name: id
in: path
required: true
description: The person's id
schema:
type: integer
format: integer
minimum: 1
responses:
401:
description: "Unauthorized"
404:
description: "Not found"
200:
description: "OK"
400:
description: "Bad Request"
/1.0/relations/relationship.json:
post:
tags:
- relationships
summary: Create a new relationship
requestBody:
description: "A relationship"
required: true
content:
application/json:
schema:
$ref: "#/components/schemas/Relationship"
responses:
200:
description: "OK"
content:
application/json:
schema:
$ref: "#/components/schemas/Relationship"
403:
description: "Unauthorized"
422:
description: "Invalid data: the data is a valid json, could be deserialized, but does not pass validation"

View File

@ -41,12 +41,10 @@ services:
tags: ['controller.service_arguments']
Chill\PersonBundle\Controller\AccompanyingCourseApiController:
autowire: true
autoconfigure: true
# arguments:
# $eventDispatcher: '@Symfony\Contracts\EventDispatcher\EventDispatcherInterface'
# $validator: '@Symfony\Component\Validator\Validator\ValidatorInterface'
# $registry: '@Symfony\Component\Workflow\Registry'
arguments:
$eventDispatcher: '@Symfony\Contracts\EventDispatcher\EventDispatcherInterface'
$validator: '@Symfony\Component\Validator\Validator\ValidatorInterface'
$registry: '@Symfony\Component\Workflow\Registry'
tags: ['controller.service_arguments']
Chill\PersonBundle\Controller\PersonApiController:
@ -69,3 +67,7 @@ services:
Chill\PersonBundle\Controller\HouseholdApiController:
autowire: true
tags: ['controller.service_arguments']
Chill\PersonBundle\Controller\RelationshipApiController:
autowire: true
tags: ['controller.service_arguments']

View File

@ -0,0 +1,48 @@
<?php
declare(strict_types=1);
namespace Chill\Migrations\Person;
use Doctrine\DBAL\Schema\Schema;
use Doctrine\Migrations\AbstractMigration;
/**
* Entities Relation and Relationship created.
*/
final class Version20211022132755 extends AbstractMigration
{
public function getDescription(): string
{
return '';
}
public function up(Schema $schema): void
{
$this->addSql('CREATE SEQUENCE Relation_id_seq INCREMENT BY 1 MINVALUE 1 START 1');
$this->addSql('CREATE SEQUENCE chill_person_relationships_id_seq INCREMENT BY 1 MINVALUE 1 START 1');
$this->addSql('CREATE TABLE Relation (id INT NOT NULL, title JSON DEFAULT NULL, reverseTitle JSON DEFAULT NULL, PRIMARY KEY(id))');
$this->addSql('CREATE TABLE chill_person_relationships (id INT NOT NULL, relation_id INT NOT NULL, reverse BOOLEAN NOT NULL, createdAt TIMESTAMP(0) WITHOUT TIME ZONE NOT NULL, updatedAt TIMESTAMP(0) WITHOUT TIME ZONE DEFAULT NULL, fromPerson_id INT NOT NULL, toPerson_id INT NOT NULL, createdBy_id INT NOT NULL, updatedBy_id INT DEFAULT NULL, PRIMARY KEY(id))');
$this->addSql('CREATE INDEX IDX_23D47C51CBA59C1E ON chill_person_relationships (fromPerson_id)');
$this->addSql('CREATE INDEX IDX_23D47C514013E22A ON chill_person_relationships (toPerson_id)');
$this->addSql('CREATE INDEX IDX_23D47C513174800F ON chill_person_relationships (createdBy_id)');
$this->addSql('CREATE INDEX IDX_23D47C5165FF1AEC ON chill_person_relationships (updatedBy_id)');
$this->addSql('CREATE INDEX IDX_23D47C513256915B ON chill_person_relationships (relation_id)');
$this->addSql('COMMENT ON COLUMN chill_person_relationships.createdAt IS \'(DC2Type:datetime_immutable)\'');
$this->addSql('COMMENT ON COLUMN chill_person_relationships.updatedAt IS \'(DC2Type:datetime_immutable)\'');
$this->addSql('ALTER TABLE chill_person_relationships ADD CONSTRAINT FK_23D47C51CBA59C1E FOREIGN KEY (fromPerson_id) REFERENCES chill_person_person (id) NOT DEFERRABLE INITIALLY IMMEDIATE');
$this->addSql('ALTER TABLE chill_person_relationships ADD CONSTRAINT FK_23D47C514013E22A FOREIGN KEY (toPerson_id) REFERENCES chill_person_person (id) NOT DEFERRABLE INITIALLY IMMEDIATE');
$this->addSql('ALTER TABLE chill_person_relationships ADD CONSTRAINT FK_23D47C513174800F FOREIGN KEY (createdBy_id) REFERENCES users (id) NOT DEFERRABLE INITIALLY IMMEDIATE');
$this->addSql('ALTER TABLE chill_person_relationships ADD CONSTRAINT FK_23D47C5165FF1AEC FOREIGN KEY (updatedBy_id) REFERENCES users (id) NOT DEFERRABLE INITIALLY IMMEDIATE');
$this->addSql('ALTER TABLE chill_person_relationships ADD CONSTRAINT FK_23D47C513256915B FOREIGN KEY (relation_id) REFERENCES Relation (id) NOT DEFERRABLE INITIALLY IMMEDIATE');
}
public function down(Schema $schema): void
{
$this->addSql('ALTER TABLE chill_person_relationships DROP CONSTRAINT FK_23D47C513256915B');
$this->addSql('DROP SEQUENCE Relation_id_seq CASCADE');
$this->addSql('DROP SEQUENCE chill_person_relationships_id_seq CASCADE');
$this->addSql('DROP TABLE Relation');
$this->addSql('DROP TABLE chill_person_relationships');
}
}