diff --git a/src/Bundle/ChillPersonBundle/Controller/RelationshipApiController.php b/src/Bundle/ChillPersonBundle/Controller/RelationshipApiController.php new file mode 100644 index 000000000..5801b481e --- /dev/null +++ b/src/Bundle/ChillPersonBundle/Controller/RelationshipApiController.php @@ -0,0 +1,38 @@ +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']]); + } +} \ No newline at end of file diff --git a/src/Bundle/ChillPersonBundle/DependencyInjection/ChillPersonExtension.php b/src/Bundle/ChillPersonBundle/DependencyInjection/ChillPersonExtension.php index 1dd1a7979..51b3525fd 100644 --- a/src/Bundle/ChillPersonBundle/DependencyInjection/ChillPersonExtension.php +++ b/src/Bundle/ChillPersonBundle/DependencyInjection/ChillPersonExtension.php @@ -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', + ] + ], + ] + ] ] ]); } diff --git a/src/Bundle/ChillPersonBundle/Entity/Relationships/Relation.php b/src/Bundle/ChillPersonBundle/Entity/Relationships/Relation.php new file mode 100644 index 000000000..1f08f2a9f --- /dev/null +++ b/src/Bundle/ChillPersonBundle/Entity/Relationships/Relation.php @@ -0,0 +1,57 @@ +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; + } +} diff --git a/src/Bundle/ChillPersonBundle/Entity/Relationships/Relationship.php b/src/Bundle/ChillPersonBundle/Entity/Relationships/Relationship.php new file mode 100644 index 000000000..32dc3eac1 --- /dev/null +++ b/src/Bundle/ChillPersonBundle/Entity/Relationships/Relationship.php @@ -0,0 +1,182 @@ +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; + } +} diff --git a/src/Bundle/ChillPersonBundle/Repository/Relationships/RelationRepository.php b/src/Bundle/ChillPersonBundle/Repository/Relationships/RelationRepository.php new file mode 100644 index 000000000..8b5fbb743 --- /dev/null +++ b/src/Bundle/ChillPersonBundle/Repository/Relationships/RelationRepository.php @@ -0,0 +1,50 @@ +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() + ; + } + */ +} diff --git a/src/Bundle/ChillPersonBundle/Repository/Relationships/RelationshipRepository.php b/src/Bundle/ChillPersonBundle/Repository/Relationships/RelationshipRepository.php new file mode 100644 index 000000000..ad251b4ff --- /dev/null +++ b/src/Bundle/ChillPersonBundle/Repository/Relationships/RelationshipRepository.php @@ -0,0 +1,38 @@ +createQueryBuilder('r') + ->andWhere('r.fromPerson = :val') + ->orWhere('r.toPerson = :val') + ->setParameter('val', $personId) + ->getQuery() + ->getResult() + ; + } + +} diff --git a/src/Bundle/ChillPersonBundle/chill.api.specs.yaml b/src/Bundle/ChillPersonBundle/chill.api.specs.yaml index 5d3f07259..ab55d052a 100644 --- a/src/Bundle/ChillPersonBundle/chill.api.specs.yaml +++ b/src/Bundle/ChillPersonBundle/chill.api.specs.yaml @@ -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" diff --git a/src/Bundle/ChillPersonBundle/config/services/controller.yaml b/src/Bundle/ChillPersonBundle/config/services/controller.yaml index c61362583..cb7ebd76c 100644 --- a/src/Bundle/ChillPersonBundle/config/services/controller.yaml +++ b/src/Bundle/ChillPersonBundle/config/services/controller.yaml @@ -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'] diff --git a/src/Bundle/ChillPersonBundle/migrations/Version20211022132755.php b/src/Bundle/ChillPersonBundle/migrations/Version20211022132755.php new file mode 100644 index 000000000..f988e7ab5 --- /dev/null +++ b/src/Bundle/ChillPersonBundle/migrations/Version20211022132755.php @@ -0,0 +1,48 @@ +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'); + } +}