From 2b1d9cabff2b1b69c0e1e2b965aa666bce5b80e4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julien=20Fastr=C3=A9?= Date: Thu, 20 Oct 2022 21:35:57 +0200 Subject: [PATCH] Feature: [calendar] associate document with calendar --- .../ChillCalendarBundle/Entity/Calendar.php | 62 ++++++++ .../Entity/CalendarDoc.php | 135 ++++++++++++++++++ .../Repository/CalendarDocRepository.php | 52 +++++++ .../CalendarDocRepositoryInterface.php | 33 +++++ .../migrations/Version20221020101547.php | 47 ++++++ 5 files changed, 329 insertions(+) create mode 100644 src/Bundle/ChillCalendarBundle/Entity/CalendarDoc.php create mode 100644 src/Bundle/ChillCalendarBundle/Repository/CalendarDocRepository.php create mode 100644 src/Bundle/ChillCalendarBundle/Repository/CalendarDocRepositoryInterface.php create mode 100644 src/Bundle/ChillCalendarBundle/migrations/Version20221020101547.php diff --git a/src/Bundle/ChillCalendarBundle/Entity/Calendar.php b/src/Bundle/ChillCalendarBundle/Entity/Calendar.php index 24cd0f758..2c8992aa0 100644 --- a/src/Bundle/ChillCalendarBundle/Entity/Calendar.php +++ b/src/Bundle/ChillCalendarBundle/Entity/Calendar.php @@ -116,6 +116,17 @@ class Calendar implements TrackCreationInterface, TrackUpdateInterface */ private CommentEmbeddable $comment; + /** + * @ORM\Column(type="integer", nullable=false, options={"default": 0}) + */ + private int $dateTimeVersion = 0; + + /** + * @var Collection + * @ORM\OneToMany(targetEntity=CalendarDoc::class, mappedBy="calendar", orphanRemoval=true) + */ + private Collection $documents; + /** * @ORM\Column(type="datetime_immutable", nullable=false) * @Serializer\Groups({"calendar:read", "read", "calendar:light", "docgen:read"}) @@ -216,12 +227,25 @@ class Calendar implements TrackCreationInterface, TrackUpdateInterface public function __construct() { $this->comment = new CommentEmbeddable(); + $this->documents = new ArrayCollection(); $this->privateComment = new PrivateCommentEmbeddable(); $this->persons = new ArrayCollection(); $this->professionals = new ArrayCollection(); $this->invites = new ArrayCollection(); } + /** + * @internal use @{CalendarDoc::__construct} instead + */ + public function addDocument(CalendarDoc $calendarDoc): self + { + if ($this->documents->contains($calendarDoc)) { + $this->documents[] = $calendarDoc; + } + + return $this; + } + /** * @internal Use {@link (Calendar::addUser)} instead */ @@ -287,6 +311,19 @@ class Calendar implements TrackCreationInterface, TrackUpdateInterface return $this->comment; } + /** + * Each time the date and time is update, this version is incremented. + */ + public function getDateTimeVersion(): int + { + return $this->dateTimeVersion; + } + + public function getDocuments(): Collection + { + return $this->documents; + } + /** * @Serializer\Groups({"docgen:read"}) */ @@ -472,6 +509,18 @@ class Calendar implements TrackCreationInterface, TrackUpdateInterface ])); } + /** + * @internal use @{CalendarDoc::setCalendar} with null instead + */ + public function removeDocument(CalendarDoc $calendarDoc): self + { + if ($calendarDoc->getCalendar() !== $this) { + throw new LogicException('cannot remove document of another calendar'); + } + + return $this; + } + /** * @internal Use {@link (Calendar::removeUser)} instead */ @@ -562,6 +611,10 @@ class Calendar implements TrackCreationInterface, TrackUpdateInterface public function setEndDate(DateTimeImmutable $endDate): self { + if ($this->endDate->getTimestamp() !== $endDate->getTimestamp()) { + $this->increaseaDatetimeVersion(); + } + $this->endDate = $endDate; return $this; @@ -609,6 +662,10 @@ class Calendar implements TrackCreationInterface, TrackUpdateInterface public function setStartDate(DateTimeImmutable $startDate): self { + if ($this->startDate->getTimestamp() !== $startDate->getTimestamp()) { + $this->increaseaDatetimeVersion(); + } + $this->startDate = $startDate; return $this; @@ -631,4 +688,9 @@ class Calendar implements TrackCreationInterface, TrackUpdateInterface return $this; } + + private function increaseaDatetimeVersion(): void + { + ++$this->dateTimeVersion; + } } diff --git a/src/Bundle/ChillCalendarBundle/Entity/CalendarDoc.php b/src/Bundle/ChillCalendarBundle/Entity/CalendarDoc.php new file mode 100644 index 000000000..458f38654 --- /dev/null +++ b/src/Bundle/ChillCalendarBundle/Entity/CalendarDoc.php @@ -0,0 +1,135 @@ +setCalendar($calendar); + + $this->storedObject = $storedObject; + $this->datetimeVersion = $calendar->getDateTimeVersion(); + } + + public function getCalendar(): Calendar + { + return $this->calendar; + } + + public function getDatetimeVersion(): int + { + return $this->datetimeVersion; + } + + public function getId(): ?int + { + return $this->id; + } + + public function getStoredObject(): StoredObject + { + return $this->storedObject; + } + + public function isTrackDateTimeVersion(): bool + { + return $this->trackDateTimeVersion; + } + + /** + * @internal use @see{Calendar::removeDocument} instead + * + * @param Calendar $calendar + */ + public function setCalendar(?Calendar $calendar): CalendarDoc + { + if (null === $calendar) { + $this->calendar->removeDocument($this); + } else { + $calendar->addDocument($this); + } + + $this->calendar = $calendar; + + $this->datetimeVersion = $calendar->getDateTimeVersion(); + + return $this; + } + + public function setDatetimeVersion(int $datetimeVersion): CalendarDoc + { + $this->datetimeVersion = $datetimeVersion; + + return $this; + } + + public function setStoredObject(StoredObject $storedObject): CalendarDoc + { + $this->storedObject = $storedObject; + + return $this; + } + + public function setTrackDateTimeVersion(bool $trackDateTimeVersion): CalendarDoc + { + $this->trackDateTimeVersion = $trackDateTimeVersion; + + return $this; + } +} diff --git a/src/Bundle/ChillCalendarBundle/Repository/CalendarDocRepository.php b/src/Bundle/ChillCalendarBundle/Repository/CalendarDocRepository.php new file mode 100644 index 000000000..bd1074b5f --- /dev/null +++ b/src/Bundle/ChillCalendarBundle/Repository/CalendarDocRepository.php @@ -0,0 +1,52 @@ +repository = $entityManager->getRepository($this->getClassName()); + } + + public function find($id): ?CalendarDoc + { + return $this->repository->find($id); + } + + public function findAll(): array + { + return $this->repository->findAll(); + } + + public function findBy(array $criteria, ?array $orderBy = null, ?int $limit = null, ?int $offset = null) + { + return $this->repository->findBy($criteria, $orderBy, $limit, $offset); + } + + public function findOneBy(array $criteria): ?CalendarDoc + { + return $this->findOneBy($criteria); + } + + public function getClassName() + { + return CalendarDoc::class; + } +} diff --git a/src/Bundle/ChillCalendarBundle/Repository/CalendarDocRepositoryInterface.php b/src/Bundle/ChillCalendarBundle/Repository/CalendarDocRepositoryInterface.php new file mode 100644 index 000000000..d2b1951df --- /dev/null +++ b/src/Bundle/ChillCalendarBundle/Repository/CalendarDocRepositoryInterface.php @@ -0,0 +1,33 @@ +addSql('DROP SEQUENCE chill_calendar.calendar_doc_id_seq CASCADE'); + $this->addSql('DROP TABLE chill_calendar.calendar_doc'); + $this->addSql('ALTER TABLE chill_calendar.calendar DROP dateTimeVersion'); + } + + public function getDescription(): string + { + return 'Add calendardoc on Calendar'; + } + + public function up(Schema $schema): void + { + $this->addSql('CREATE SEQUENCE chill_calendar.calendar_doc_id_seq INCREMENT BY 1 MINVALUE 1 START 1'); + $this->addSql('CREATE TABLE chill_calendar.calendar_doc (id INT NOT NULL, calendar_id INT NOT NULL, datetimeVersion INT DEFAULT 0 NOT NULL, trackDateTimeVersion BOOLEAN DEFAULT false NOT NULL, createdAt TIMESTAMP(0) WITHOUT TIME ZONE DEFAULT NULL, updatedAt TIMESTAMP(0) WITHOUT TIME ZONE DEFAULT NULL, storedObject_id INT NOT NULL, createdBy_id INT DEFAULT NULL, updatedBy_id INT DEFAULT NULL, PRIMARY KEY(id))'); + $this->addSql('CREATE INDEX IDX_4FD11573A40A2C8 ON chill_calendar.calendar_doc (calendar_id)'); + $this->addSql('CREATE INDEX IDX_4FD115736C99C13A ON chill_calendar.calendar_doc (storedObject_id)'); + $this->addSql('CREATE INDEX IDX_4FD115733174800F ON chill_calendar.calendar_doc (createdBy_id)'); + $this->addSql('CREATE INDEX IDX_4FD1157365FF1AEC ON chill_calendar.calendar_doc (updatedBy_id)'); + $this->addSql('COMMENT ON COLUMN chill_calendar.calendar_doc.createdAt IS \'(DC2Type:datetime_immutable)\''); + $this->addSql('COMMENT ON COLUMN chill_calendar.calendar_doc.updatedAt IS \'(DC2Type:datetime_immutable)\''); + $this->addSql('ALTER TABLE chill_calendar.calendar_doc ADD CONSTRAINT FK_4FD11573A40A2C8 FOREIGN KEY (calendar_id) REFERENCES chill_calendar.calendar (id) NOT DEFERRABLE INITIALLY IMMEDIATE'); + $this->addSql('ALTER TABLE chill_calendar.calendar_doc ADD CONSTRAINT FK_4FD115736C99C13A FOREIGN KEY (storedObject_id) REFERENCES chill_doc.stored_object (id) NOT DEFERRABLE INITIALLY IMMEDIATE'); + $this->addSql('ALTER TABLE chill_calendar.calendar_doc ADD CONSTRAINT FK_4FD115733174800F FOREIGN KEY (createdBy_id) REFERENCES users (id) NOT DEFERRABLE INITIALLY IMMEDIATE'); + $this->addSql('ALTER TABLE chill_calendar.calendar_doc ADD CONSTRAINT FK_4FD1157365FF1AEC FOREIGN KEY (updatedBy_id) REFERENCES users (id) NOT DEFERRABLE INITIALLY IMMEDIATE'); + $this->addSql('ALTER TABLE chill_calendar.calendar ADD dateTimeVersion INT DEFAULT 0 NOT NULL'); + } +}