From 11c7207a1c63ada7a63a6d9b108ccdb49ea6f27a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ma=C3=ABl=20Gangloff?= Date: Wed, 17 Jul 2024 19:29:43 +0200 Subject: [PATCH] feat: add createdAt and updatedAt on Domain entity --- migrations/Version20240717172250.php | 37 ++++++++++++++++++++++++++ src/Entity/Domain.php | 39 ++++++++++++++++++++++++++++ src/Service/RDAPService.php | 2 +- 3 files changed, 77 insertions(+), 1 deletion(-) create mode 100644 migrations/Version20240717172250.php diff --git a/migrations/Version20240717172250.php b/migrations/Version20240717172250.php new file mode 100644 index 0000000..ccb56b9 --- /dev/null +++ b/migrations/Version20240717172250.php @@ -0,0 +1,37 @@ +addSql('ALTER TABLE domain ADD COLUMN created_at DATE NOT NULL'); + $this->addSql('ALTER TABLE domain ADD COLUMN updated_at DATE NOT NULL'); + } + + public function down(Schema $schema): void + { + // this down() migration is auto-generated, please modify it to your needs + $this->addSql('CREATE TEMPORARY TABLE __temp__domain AS SELECT ldh_name, handle, status FROM domain'); + $this->addSql('DROP TABLE domain'); + $this->addSql('CREATE TABLE domain (ldh_name VARCHAR(255) NOT NULL, handle VARCHAR(255) NOT NULL, status CLOB NOT NULL --(DC2Type:simple_array) + , PRIMARY KEY(ldh_name))'); + $this->addSql('INSERT INTO domain (ldh_name, handle, status) SELECT ldh_name, handle, status FROM __temp__domain'); + $this->addSql('DROP TABLE __temp__domain'); + } +} diff --git a/src/Entity/Domain.php b/src/Entity/Domain.php index 864125c..0f73e42 100644 --- a/src/Entity/Domain.php +++ b/src/Entity/Domain.php @@ -8,6 +8,7 @@ use ApiPlatform\Metadata\GetCollection; use ApiPlatform\Metadata\Post; use App\Controller\DomainRefreshController; use App\Repository\DomainRepository; +use DateTimeImmutable; use Doctrine\Common\Collections\ArrayCollection; use Doctrine\Common\Collections\Collection; use Doctrine\DBAL\Types\Types; @@ -98,6 +99,12 @@ class Domain #[Groups(['domain:item'])] private Collection $nameservers; + #[ORM\Column(type: Types::DATE_IMMUTABLE)] + private ?DateTimeImmutable $createdAt = null; + + #[ORM\Column(type: Types::DATE_IMMUTABLE)] + private ?DateTimeImmutable $updatedAt = null; + public function __construct() { $this->events = new ArrayCollection(); @@ -252,4 +259,36 @@ class Domain return $this; } + + public function getUpdatedAt(): ?DateTimeImmutable + { + return $this->updatedAt; + } + + #[ORM\PrePersist] + #[ORM\PreUpdate] + public function updateTimestamps(): void + { + $this->setUpdatedAt(new DateTimeImmutable('now')); + if ($this->getCreatedAt() === null) { + $this->setCreatedAt(new DateTimeImmutable('now')); + } + } + + private function setUpdatedAt(?DateTimeImmutable $updatedAt): void + { + $this->updatedAt = $updatedAt; + + } + + public function getCreatedAt(): ?DateTimeImmutable + { + return $this->createdAt; + } + + private function setCreatedAt(?DateTimeImmutable $createdAt): void + { + $this->createdAt = $createdAt; + + } } diff --git a/src/Service/RDAPService.php b/src/Service/RDAPService.php index 142d3ab..bbcf9d9 100644 --- a/src/Service/RDAPService.php +++ b/src/Service/RDAPService.php @@ -163,7 +163,7 @@ readonly class RDAPService $domain->addNameserver($nameserver); } - + $domain->updateTimestamps(); $this->em->persist($domain); $this->em->flush();