From f1b92040ccbbd036683e2915029fc7f66a7f8af9 Mon Sep 17 00:00:00 2001 From: Wilco Louwerse Date: Thu, 14 Mar 2024 14:28:48 +0100 Subject: [PATCH 1/7] Added Database Entity with relation to Organization --- api/migrations/Version20240314120005.php | 39 +++ api/src/Entity/Database.php | 362 ++++++++++++++++++++++ api/src/Entity/Organization.php | 24 ++ api/src/Repository/DatabaseRepository.php | 50 +++ 4 files changed, 475 insertions(+) create mode 100644 api/migrations/Version20240314120005.php create mode 100644 api/src/Entity/Database.php create mode 100644 api/src/Repository/DatabaseRepository.php diff --git a/api/migrations/Version20240314120005.php b/api/migrations/Version20240314120005.php new file mode 100644 index 000000000..923adc92d --- /dev/null +++ b/api/migrations/Version20240314120005.php @@ -0,0 +1,39 @@ +addSql('CREATE SEQUENCE database_id_seq INCREMENT BY 1 MINVALUE 1 START 1'); + $this->addSql('CREATE TABLE database (id UUID NOT NULL, name VARCHAR(255) NOT NULL, description TEXT DEFAULT NULL, reference VARCHAR(255) NOT NULL, version VARCHAR(255) NOT NULL DEFAULT \'0.0.0\', uri VARCHAR(255) NOT NULL, date_created TIMESTAMP(0) WITHOUT TIME ZONE DEFAULT NULL, date_modified TIMESTAMP(0) WITHOUT TIME ZONE DEFAULT NULL, PRIMARY KEY(id))'); + $this->addSql('ALTER TABLE organization ADD database_id UUID DEFAULT NULL'); + $this->addSql('ALTER TABLE organization ADD CONSTRAINT FK_C1EE637CF0AA09DB FOREIGN KEY (database_id) REFERENCES database (id) NOT DEFERRABLE INITIALLY IMMEDIATE'); + $this->addSql('CREATE INDEX IDX_C1EE637CF0AA09DB ON organization (database_id)'); + } + + public function down(Schema $schema): void + { + // this down() migration is auto-generated, please modify it to your needs + $this->addSql('ALTER TABLE organization DROP CONSTRAINT FK_C1EE637CF0AA09DB'); + $this->addSql('DROP INDEX IDX_C1EE637CF0AA09DB'); + $this->addSql('ALTER TABLE organization DROP database_id'); + $this->addSql('DROP SEQUENCE database_id_seq CASCADE'); + $this->addSql('DROP TABLE database'); + } +} diff --git a/api/src/Entity/Database.php b/api/src/Entity/Database.php new file mode 100644 index 000000000..46b61d480 --- /dev/null +++ b/api/src/Entity/Database.php @@ -0,0 +1,362 @@ +organizations = new ArrayCollection(); + } + + public function __toString() + { + return $this->getName(); + } + + /** + * Create or update this Database from an external schema array. + * + * This function is used to update and create databases form database.json objects. + * + * @param array $schema The schema to load. + * + * @return $this This Database. + */ + public function fromSchema(array $schema): self + { + // Basic stuff + if (array_key_exists('$id', $schema)) { + $this->setReference($schema['$id']); + } + if (array_key_exists('version', $schema)) { + $this->setVersion($schema['version']); + } + + array_key_exists('title', $schema) ? $this->setName($schema['title']) : ''; + array_key_exists('description', $schema) ? $this->setDescription($schema['description']) : ''; + array_key_exists('uri', $schema) ? $this->setUri($schema['uri']) : ''; + + return $this; + } + + /** + * Convert this Gateway to a schema. + * + * @return array Schema array. + */ + public function toSchema(): array + { + // Do not return jwt, secret, password or apikey this way! + return [ + '$id' => $this->getReference(), //@todo dit zou een interne uri verwijzing moeten zijn maar hebben we nog niet + '$schema' => 'https://docs.commongateway.nl/schemas/Database.schema.json', + 'title' => $this->getName(), + 'description' => $this->getDescription(), + 'version' => $this->getVersion(), + 'name' => $this->getName(), + 'uri' => $this->getUri(), + ]; + } + + public function getId() + { + return $this->id; + } + + public function setId(string $id): self + { + $this->id = Uuid::fromString($id); + + return $this; + } + + public function getName(): ?string + { + return $this->name; + } + + public function setName(string $name): self + { + $this->name = $name; + + return $this; + } + + public function getDescription(): ?string + { + return $this->description; + } + + public function setDescription(?string $description): self + { + $this->description = $description; + + return $this; + } + + public function getReference(): ?string + { + return $this->reference; + } + + public function setReference(string $reference): self + { + $this->reference = $reference; + + return $this; + } + + public function getVersion(): ?string + { + return $this->version; + } + + public function setVersion(string $version): self + { + $this->version = $version; + + return $this; + } + + public function getUri(): ?string + { + return $this->uri; + } + + public function setUri(?string $uri): self + { + $this->uri = $uri; + + return $this; + } + + /** + * @return Collection|Organization[] + */ + public function getOrganizations(): Collection + { + return $this->organizations; + } + + public function addOrganization(Organization $organization): self + { + if (!$this->organizations->contains($organization)) { + $this->organizations[] = $organization; + $organization->setDatabase($this); + } + + return $this; + } + + public function removeOrganization(Organization $organization): self + { + if ($this->organizations->removeElement($organization)) { + // set the owning side to null (unless already changed) + if ($organization->getDatabase() === $this) { + $organization->setDatabase(null); + } + } + + return $this; + } + + public function getDateCreated(): ?DateTimeInterface + { + return $this->dateCreated; + } + + public function setDateCreated(DateTimeInterface $dateCreated): self + { + $this->dateCreated = $dateCreated; + + return $this; + } + + public function getDateModified(): ?DateTimeInterface + { + return $this->dateModified; + } + + public function setDateModified(DateTimeInterface $dateModified): self + { + $this->dateModified = $dateModified; + + return $this; + } +} diff --git a/api/src/Entity/Organization.php b/api/src/Entity/Organization.php index 7043caac4..24f85144d 100644 --- a/api/src/Entity/Organization.php +++ b/api/src/Entity/Organization.php @@ -20,6 +20,7 @@ use Symfony\Component\Serializer\Annotation\Groups; use Symfony\Component\Serializer\Annotation\MaxDepth; use Symfony\Component\Validator\Constraints as Assert; +use Symfony\Component\VarDumper\Cloner\Data; /** * This entity holds the information about an Organization. @@ -119,6 +120,17 @@ class Organization */ private string $version = '0.0.0'; + /** + * The database that this organization uses to store its objects. + * + * @Groups({"read", "write"}) + * + * @ORM\ManyToOne(targetEntity=Database::class, inversedBy="organizations") + * + * @MaxDepth(1) + */ + private ?Database $database = null; + /** * @Groups({"read", "write"}) * @@ -286,6 +298,18 @@ public function setVersion(?string $version): self return $this; } + public function getDatabase(): ?Database + { + return $this->database; + } + + public function setDatabase(?Database $database): self + { + $this->database = $database; + + return $this; + } + /** * @return Collection|Application[] */ diff --git a/api/src/Repository/DatabaseRepository.php b/api/src/Repository/DatabaseRepository.php new file mode 100644 index 000000000..a3da5d9ff --- /dev/null +++ b/api/src/Repository/DatabaseRepository.php @@ -0,0 +1,50 @@ +createQueryBuilder('d') + ->andWhere('d.exampleField = :val') + ->setParameter('val', $value) + ->orderBy('d.id', 'ASC') + ->setMaxResults(10) + ->getQuery() + ->getResult() + ; + } + */ + + /* + public function findOneBySomeField($value): ?Database + { + return $this->createQueryBuilder('d') + ->andWhere('d.exampleField = :val') + ->setParameter('val', $value) + ->getQuery() + ->getOneOrNullResult() + ; + } + */ +} From ae4e5036947f9e2f20adf9a0881153eae3d60272 Mon Sep 17 00:00:00 2001 From: Wilco Louwerse Date: Thu, 14 Mar 2024 14:35:42 +0100 Subject: [PATCH 2/7] Let's not asser url for database uri --- api/src/Entity/Database.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/api/src/Entity/Database.php b/api/src/Entity/Database.php index 46b61d480..ef708c440 100644 --- a/api/src/Entity/Database.php +++ b/api/src/Entity/Database.php @@ -139,7 +139,6 @@ class Database /** * @var string An uri used to connect to the database. * - * @Assert\Url * @Assert\NotNull * * @Groups({"read", "write"}) @@ -151,6 +150,8 @@ class Database /** * The organizations that use this Database. * + * @Groups({"read", "write"}) + * * @MaxDepth(1) * * @ORM\OneToMany(targetEntity=Organization::class, mappedBy="database") From b308e4fd709e5362f290de2fd2591271b07397b4 Mon Sep 17 00:00:00 2001 From: Wilco Louwerse Date: Fri, 15 Mar 2024 14:56:41 +0100 Subject: [PATCH 3/7] Add database to _self of ObjectEntities --- api/src/Entity/Database.php | 2 +- api/src/Entity/ObjectEntity.php | 5 +++++ 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/api/src/Entity/Database.php b/api/src/Entity/Database.php index ef708c440..a6b5cc6ce 100644 --- a/api/src/Entity/Database.php +++ b/api/src/Entity/Database.php @@ -141,7 +141,7 @@ class Database * * @Assert\NotNull * - * @Groups({"read", "write"}) + * @Groups({"read", "read_secure" , "write"}) * * @ORM\Column(type="string", length=255) */ diff --git a/api/src/Entity/ObjectEntity.php b/api/src/Entity/ObjectEntity.php index 8f9095279..416723afe 100644 --- a/api/src/Entity/ObjectEntity.php +++ b/api/src/Entity/ObjectEntity.php @@ -1127,6 +1127,11 @@ public function toArray(array $configuration = []): array 'level' => $configuration['level'], 'dateCreated' => $this->getDateCreated() ? $this->getDateCreated()->format('c') : null, 'dateModified' => $this->getDateModified() ? $this->getDateModified()->format('c') : null, + 'database' => [ + 'id' => $this->getOrganization() && $this->getOrganization()->getDatabase() ? $this->getOrganization()->getDatabase()->getId()->toString() : null, + 'name' => $this->getOrganization() && $this->getOrganization()->getDatabase() ? $this->getOrganization()->getDatabase()->getName() : null, + 'ref' => $this->getOrganization() && $this->getOrganization()->getDatabase() ? $this->getOrganization()->getDatabase()->getReference() : null + ], 'owner' => [ 'id' => $this->getOwner(), 'name' => isset($configuration['user']) ? $configuration['user']->getName() : $this->getOwner(), From 60652e6889e0f2a01f10b96a8f83e5de235ce49e Mon Sep 17 00:00:00 2001 From: Wilco Louwerse Date: Fri, 15 Mar 2024 15:01:49 +0100 Subject: [PATCH 4/7] Let's remove database->uri read for now --- api/src/Entity/Database.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/api/src/Entity/Database.php b/api/src/Entity/Database.php index a6b5cc6ce..e172dfe3b 100644 --- a/api/src/Entity/Database.php +++ b/api/src/Entity/Database.php @@ -141,7 +141,7 @@ class Database * * @Assert\NotNull * - * @Groups({"read", "read_secure" , "write"}) + * @Groups({"write"}) * * @ORM\Column(type="string", length=255) */ From 1f54e71d1024dee48c29946277d634eeea8f39b0 Mon Sep 17 00:00:00 2001 From: Wilco Louwerse Date: Fri, 15 Mar 2024 16:06:01 +0100 Subject: [PATCH 5/7] Added missing types --- api/src/Entity/Database.php | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/api/src/Entity/Database.php b/api/src/Entity/Database.php index e172dfe3b..8f356d336 100644 --- a/api/src/Entity/Database.php +++ b/api/src/Entity/Database.php @@ -73,7 +73,7 @@ class Database * * @ORM\CustomIdGenerator(class="Ramsey\Uuid\Doctrine\UuidGenerator") */ - private $id; + private UuidInterface $id; /** * @var string The name of this Database. @@ -159,7 +159,7 @@ class Database private Collection $organizations; /** - * @var Datetime The moment this resource was created + * @var DateTimeInterface|null The moment this resource was created * * @Groups({"read"}) * @@ -167,10 +167,10 @@ class Database * * @ORM\Column(type="datetime", nullable=true) */ - private $dateCreated; + private ?DateTimeInterface $dateCreated; /** - * @var Datetime The moment this resource was last Modified + * @var DateTimeInterface|null The moment this resource was last Modified * * @Groups({"read"}) * @@ -178,7 +178,7 @@ class Database * * @ORM\Column(type="datetime", nullable=true) */ - private $dateModified; + private ?DateTimeInterface $dateModified; public function __construct() { From a08c33899dd2146fcc687ad524c28d1e798fe8bd Mon Sep 17 00:00:00 2001 From: Wilco Louwerse Date: Mon, 18 Mar 2024 09:52:00 +0100 Subject: [PATCH 6/7] Added missing string return type --- api/src/Entity/Database.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/api/src/Entity/Database.php b/api/src/Entity/Database.php index 8f356d336..8abb9f007 100644 --- a/api/src/Entity/Database.php +++ b/api/src/Entity/Database.php @@ -185,7 +185,7 @@ public function __construct() $this->organizations = new ArrayCollection(); } - public function __toString() + public function __toString(): string { return $this->getName(); } From 0c6f4fba65f1238d4c5e0fbf8aee99c2031bc41e Mon Sep 17 00:00:00 2001 From: Wilco Louwerse Date: Mon, 18 Mar 2024 09:55:23 +0100 Subject: [PATCH 7/7] Added docblock for toString function --- api/src/Entity/Database.php | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/api/src/Entity/Database.php b/api/src/Entity/Database.php index 8abb9f007..1a2bbf60a 100644 --- a/api/src/Entity/Database.php +++ b/api/src/Entity/Database.php @@ -180,14 +180,19 @@ class Database */ private ?DateTimeInterface $dateModified; - public function __construct() + /** + * Return the name of this Database when cast to a string. + * + * @return string + */ + public function __toString(): string { - $this->organizations = new ArrayCollection(); + return $this->getName(); } - public function __toString(): string + public function __construct() { - return $this->getName(); + $this->organizations = new ArrayCollection(); } /**