From 63ca6e7557dcdc3246d8bc6a103a17117fb9b717 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 | 89 ++++++++++++++++++++++++ 2 files changed, 196 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..9e568e4 --- /dev/null +++ b/src/Repository/EventRepository.php @@ -0,0 +1,89 @@ +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 getUpcomingByUser(User $user, DateTime $from, int $limit, bool $useRelations = false, array $withRelations = []): Generator + { + $select = $this->selectAllByUser($user); + $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->orderBy('start', 'DESC'); + $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()); + return $select; + } + + private function selectAllByUser(User $user) + { + $select = new Select(Container::$dbConnection, Event::getTable()); + $select->innerJoin('communities', ['communities', 'id'], '=', ['events', 'community_id']); + $select->innerJoin('community_members', ['communities', 'id'], '=', ['community_members', 'community_id']); + $select->where(['community_members', 'user_id'], '=', $user->getId()); + return $select; + } +}