fix selectUpcomingAndRecent when used with other selects
All checks were successful
rvr-nextgen/pipeline/pr-master This commit looks good

This commit is contained in:
Bence Pőcze 2023-08-06 22:06:42 +02:00
parent 2d2cf9c6f0
commit 7cb406cc49
Signed by: bence
GPG Key ID: DC5BD6E95A333E6D

View File

@ -23,19 +23,24 @@ class EventRepository
public function getAllByCommunity(Community $community, bool $useRelations = false, array $withRelations = []): Generator public function getAllByCommunity(Community $community, bool $useRelations = false, array $withRelations = []): Generator
{ {
$select = $this->selectAllByCommunity($community); $select = new Select(Container::$dbConnection, Event::getTable());
$this->selectAllByCommunity($select, $community);
yield from Container::$persistentDataManager->selectMultipleFromDb($select, Event::class, $useRelations, $withRelations); yield from Container::$persistentDataManager->selectMultipleFromDb($select, Event::class, $useRelations, $withRelations);
} }
public function countAllByCommunity(Community $community): int public function countAllByCommunity(Community $community): int
{ {
return $this->selectAllByCommunity($community)->count(); $select = new Select(Container::$dbConnection, Event::getTable());
$this->selectAllByCommunity($select, $community);
return $select->count();
} }
public function getUpcomingAndRecentByCommunity(Community $community, DateTime $from, int $days, int $limit, bool $useRelations = false, array $withRelations = []): Generator public function getUpcomingAndRecentByCommunity(Community $community, DateTime $from, int $days, int $limit, bool $useRelations = false, array $withRelations = []): Generator
{ {
$select = $this->selectAllByCommunity($community); $select = new Select(Container::$dbConnection, Event::getTable());
$this->selectAllByCommunity($select, $community);
$this->selectUpcomingAndRecent($select, $from, $days); $this->selectUpcomingAndRecent($select, $from, $days);
$select->orderBy('start', 'DESC'); $select->orderBy('start', 'DESC');
$select->limit($limit, 0); $select->limit($limit, 0);
@ -45,7 +50,8 @@ class EventRepository
public function getUpcomingAndRecentByUser(User $user, DateTime $from, int $days, int $limit, bool $useRelations = false, array $withRelations = []): Generator public function getUpcomingAndRecentByUser(User $user, DateTime $from, int $days, int $limit, bool $useRelations = false, array $withRelations = []): Generator
{ {
$select = $this->selectAllByUser($user); $select = new Select(Container::$dbConnection, Event::getTable());
$this->selectAllByUser($select, $user);
$this->selectUpcomingAndRecent($select, $from, $days); $this->selectUpcomingAndRecent($select, $from, $days);
$select->orderBy('start', 'DESC'); $select->orderBy('start', 'DESC');
$select->limit($limit, 0); $select->limit($limit, 0);
@ -55,7 +61,8 @@ class EventRepository
public function getCurrentByUser(User $user, DateTime $from, int $days, bool $useRelations = false, array $withRelations = []): ?Event public function getCurrentByUser(User $user, DateTime $from, int $days, bool $useRelations = false, array $withRelations = []): ?Event
{ {
$select = $this->selectAllByUser($user); $select = new Select(Container::$dbConnection, Event::getTable());
$this->selectAllByUser($select, $user);
$this->selectUpcomingAndRecent($select, $from, $days); $this->selectUpcomingAndRecent($select, $from, $days);
$select->orderBy('start', 'DESC'); $select->orderBy('start', 'DESC');
$select->limit(1, 0); $select->limit(1, 0);
@ -65,7 +72,8 @@ class EventRepository
public function getPagedByCommunity(Community $community, int $page, int $itemsPerPage, bool $useRelations = false, array $withRelations = []): Generator public function getPagedByCommunity(Community $community, int $page, int $itemsPerPage, bool $useRelations = false, array $withRelations = []): Generator
{ {
$select = $this->selectAllByCommunity($community); $select = new Select(Container::$dbConnection, Event::getTable());
$this->selectAllByCommunity($select, $community);
$select->orderBy('start', 'DESC'); $select->orderBy('start', 'DESC');
$select->paginate($page, $itemsPerPage); $select->paginate($page, $itemsPerPage);
@ -74,7 +82,8 @@ class EventRepository
public function searchByTitle(Community $community, string $title): Generator public function searchByTitle(Community $community, string $title): Generator
{ {
$select = $this->selectAllByCommunity($community); $select = new Select(Container::$dbConnection, Event::getTable());
$this->selectAllByCommunity($select, $community);
$select->where('title', 'LIKE', '%' . $title . '%'); $select->where('title', 'LIKE', '%' . $title . '%');
$select->orderBy('start', 'DESC'); $select->orderBy('start', 'DESC');
$select->limit(10); $select->limit(10);
@ -82,31 +91,29 @@ class EventRepository
yield from Container::$persistentDataManager->selectMultipleFromDb($select, Event::class); yield from Container::$persistentDataManager->selectMultipleFromDb($select, Event::class);
} }
private function selectAllByCommunity(Community $community) private function selectAllByCommunity(Select $select, Community $community): void
{ {
$select = new Select(Container::$dbConnection, Event::getTable());
$select->where('community_id', '=', $community->getId()); $select->where('community_id', '=', $community->getId());
return $select;
} }
private function selectAllByUser(User $user) private function selectAllByUser(Select $select, User $user): void
{ {
$select = new Select(Container::$dbConnection, Event::getTable());
$select->innerJoin('communities', ['communities', 'id'], '=', ['events', 'community_id']); $select->innerJoin('communities', ['communities', 'id'], '=', ['events', 'community_id']);
$select->innerJoin('community_members', ['communities', 'id'], '=', ['community_members', 'community_id']); $select->innerJoin('community_members', ['communities', 'id'], '=', ['community_members', 'community_id']);
$select->where(['community_members', 'user_id'], '=', $user->getId()); $select->where(['community_members', 'user_id'], '=', $user->getId());
return $select;
} }
private function selectUpcomingAndRecent(Select $select, DateTime $from, int $days) private function selectUpcomingAndRecent(Select $select, DateTime $from, int $days): void
{ {
$select->where(function (Select $select) use ($from, $days) { $select->where(function (Select $select) use ($from, $days) {
$select->where('start', '<', (clone $from)->add(DateInterval::createFromDateString("$days days"))->format('Y-m-d H:i:s')); $select->where(function (Select $select) use ($from, $days) {
$select->where('end', '>', $from->format('Y-m-d H:i:s')); $select->where('start', '<', (clone $from)->add(DateInterval::createFromDateString("$days days"))->format('Y-m-d H:i:s'));
}); $select->where('end', '>', $from->format('Y-m-d H:i:s'));
$select->orWhere(function (Select $select) use ($from, $days) { });
$select->where('end', '>', (clone $from)->sub(DateInterval::createFromDateString("$days days"))->format('Y-m-d H:i:s')); $select->orWhere(function (Select $select) use ($from, $days) {
$select->where('start', '<', $from->format('Y-m-d H:i:s')); $select->where('end', '>', (clone $from)->sub(DateInterval::createFromDateString("$days days"))->format('Y-m-d H:i:s'));
$select->where('start', '<', $from->format('Y-m-d H:i:s'));
});
}); });
} }
} }