RVRNEXT-11 add model and repository for events
This commit is contained in:
parent
224cdc0eda
commit
63ca6e7557
107
src/PersistentData/Model/Event.php
Normal file
107
src/PersistentData/Model/Event.php
Normal file
@ -0,0 +1,107 @@
|
||||
<?php namespace RVR\PersistentData\Model;
|
||||
|
||||
use DateTime;
|
||||
use SokoWeb\PersistentData\Model\ModelWithSlug;
|
||||
|
||||
class Event extends ModelWithSlug
|
||||
{
|
||||
protected static string $table = 'events';
|
||||
|
||||
protected static array $fields = ['community_id', 'start', 'end', 'title', 'description'];
|
||||
|
||||
protected static array $relations = ['community' => 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;
|
||||
}
|
||||
}
|
89
src/Repository/EventRepository.php
Normal file
89
src/Repository/EventRepository.php
Normal file
@ -0,0 +1,89 @@
|
||||
<?php namespace RVR\Repository;
|
||||
|
||||
use Container;
|
||||
use DateTime;
|
||||
use Generator;
|
||||
use RVR\PersistentData\Model\Community;
|
||||
use RVR\PersistentData\Model\Event;
|
||||
use RVR\PersistentData\Model\User;
|
||||
use SokoWeb\Database\Query\Select;
|
||||
|
||||
class EventRepository
|
||||
{
|
||||
public function getById(int $id): ?Event
|
||||
{
|
||||
return Container::$persistentDataManager->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;
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user