From 82309b81c44ac9ca5ceea7153f6a484617e19536 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?P=C5=91cze=20Bence?= Date: Sun, 28 May 2023 03:42:13 +0200 Subject: [PATCH] RVRNEXT-11 add model and repository for events --- src/PersistentData/Model/Event.php | 107 +++++++++++++++++++++++++++++ src/Repository/EventRepository.php | 69 +++++++++++++++++++ 2 files changed, 176 insertions(+) create mode 100644 src/PersistentData/Model/Event.php create mode 100644 src/Repository/EventRepository.php diff --git a/src/PersistentData/Model/Event.php b/src/PersistentData/Model/Event.php new file mode 100644 index 0000000..0812971 --- /dev/null +++ b/src/PersistentData/Model/Event.php @@ -0,0 +1,107 @@ + Community::class]; + + protected static string $slugSource = 'title'; + + private ?Community $community = null; + + private int $communityId; + + private DateTime $start; + + private DateTime $end; + + private string $title = ''; + + private string $description = ''; + + public function setCommunity(Community $community): void + { + $this->community = $community; + } + + public function setCommunityId(int $communityId): void + { + $this->communityId = $communityId; + } + + public function setStartDate(DateTime $start): void + { + $this->start = $start; + } + + public function setStart(string $start): void + { + $this->start = new DateTime($start); + } + + public function setEndDate(DateTime $end): void + { + $this->end = $end; + } + + public function setEnd(string $end): void + { + $this->end = new DateTime($end); + } + + public function setTitle(string $title): void + { + $this->title = $title; + } + + public function setDescription(string $description): void + { + $this->description = $description; + } + + public function getCommunity(): ?Community + { + return $this->community; + } + + public function getCommunityId(): int + { + return $this->communityId; + } + + public function getStartDate(): DateTime + { + return $this->start; + } + + public function getStart(): string + { + return $this->start->format('Y-m-d H:i:s'); + } + + public function getEndDate(): DateTime + { + return $this->end; + } + + public function getEnd(): string + { + return $this->end->format('Y-m-d H:i:s'); + } + + public function getTitle(): string + { + return $this->title; + } + + public function getDescription(): string + { + return $this->description; + } +} diff --git a/src/Repository/EventRepository.php b/src/Repository/EventRepository.php new file mode 100644 index 0000000..dbcf503 --- /dev/null +++ b/src/Repository/EventRepository.php @@ -0,0 +1,69 @@ +selectFromDbById($id, Event::class); + } + + public function getBySlug(string $slug): ?Event + { + return \Container::$persistentDataManager->selectFromDbBySlug($slug, Event::class); + } + + public function getAllByCommunity(Community $community, bool $useRelations = false, array $withRelations = []): Generator + { + $select = $this->selectAllByCommunity($community); + + yield from Container::$persistentDataManager->selectMultipleFromDb($select, Event::class, $useRelations, $withRelations); + } + + public function countAllByCommunity(Community $community): int + { + return $this->selectAllByCommunity($community)->count(); + } + + public function getUpcomingByCommunity(Community $community, DateTime $from, int $limit, bool $useRelations = false, array $withRelations = []): Generator + { + $select = $this->selectAllByCommunity($community); + $select->where('end', '>', $from->format('Y-m-d H:i:s')); + $select->orderBy('start', 'ASC'); + $select->limit($limit, 0); + + yield from Container::$persistentDataManager->selectMultipleFromDb($select, Event::class, $useRelations, $withRelations); + } + + public function getPagedByCommunity(Community $community, int $start, int $limit, bool $useRelations = false, array $withRelations = []): Generator + { + $select = $this->selectAllByCommunity($community); + $select->orderBy('start', 'DESC'); + $select->limit($limit, $start); + + yield from Container::$persistentDataManager->selectMultipleFromDb($select, Event::class, $useRelations, $withRelations); + } + + public function searchByTitle(Community $community, string $title): Generator + { + $select = $this->selectAllByCommunity($community); + $select->where('title', 'LIKE', '%' . $title . '%'); + $select->limit(10); + + yield from Container::$persistentDataManager->selectMultipleFromDb($select, Event::class); + } + + private function selectAllByCommunity(Community $community) + { + $select = new Select(Container::$dbConnection, Event::getTable()); + $select->where('community_id', '=', $community->getId()); + $select->orderBy('start', 'DESC'); + return $select; + } +}