-
-
Notifications
You must be signed in to change notification settings - Fork 679
Expand file tree
/
Copy pathEventRepository.php
More file actions
168 lines (144 loc) · 6.83 KB
/
Copy pathEventRepository.php
File metadata and controls
168 lines (144 loc) · 6.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
<?php
declare(strict_types=1);
namespace HiEvents\Repository\Eloquent;
use HiEvents\DomainObjects\AccountDomainObject;
use HiEvents\DomainObjects\EventDomainObject;
use HiEvents\DomainObjects\EventStatisticDomainObject;
use HiEvents\DomainObjects\Generated\EventDomainObjectAbstract;
use HiEvents\DomainObjects\Generated\EventSettingDomainObjectAbstract;
use HiEvents\DomainObjects\OrganizerDomainObject;
use HiEvents\DomainObjects\Status\EventStatus;
use HiEvents\Http\DTO\QueryParamsDTO;
use HiEvents\Models\Event;
use HiEvents\Repository\Eloquent\Value\Relationship;
use HiEvents\Repository\Interfaces\EventRepositoryInterface;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Pagination\LengthAwarePaginator;
class EventRepository extends BaseRepository implements EventRepositoryInterface
{
protected function getModel(): string
{
return Event::class;
}
public function getDomainObject(): string
{
return EventDomainObject::class;
}
public function findEventsForOrganizer(int $organizerId, int $accountId, QueryParamsDTO $params): LengthAwarePaginator
{
$where[] = static function (Builder $builder) use ($accountId, $organizerId) {
$builder
->whereIn(EventDomainObjectAbstract::STATUS, [
EventStatus::LIVE->name,
EventStatus::DRAFT->name,
])
->where(EventDomainObjectAbstract::ORGANIZER_ID, $organizerId)
->where(EventDomainObjectAbstract::ACCOUNT_ID, $accountId);
};
return $this->findEvents($where, $params);
}
public function findEvents(array $where, QueryParamsDTO $params): LengthAwarePaginator
{
if (!empty($params->query)) {
$where[] = static function (Builder $builder) use ($params) {
$builder
->where(EventDomainObjectAbstract::TITLE, 'ilike', '%' . $params->query . '%');
};
}
$upcomingEventsFilter = $params->query_params->get('eventsStatus') === 'upcoming';
if (!empty($params->filter_fields) && !$upcomingEventsFilter) {
$this->applyFilterFields($params, EventDomainObject::getAllowedFilterFields());
}
// Apply custom filter for upcoming events, as it keeps things less complex on the front-end
if ($upcomingEventsFilter) {
$where[] = static function (Builder $builder) {
$builder
->where(EventDomainObjectAbstract::STATUS, '!=', EventStatus::ARCHIVED->getName())
->where(function ($query) {
$query->whereNull(EventDomainObjectAbstract::END_DATE)
->orWhere(EventDomainObjectAbstract::END_DATE, '>=', now());
});
};
$organizerId = $params->filter_fields->first(fn($filter) => $filter->field === EventDomainObjectAbstract::ORGANIZER_ID)?->value;
if ($organizerId) {
$this->model = $this->model->where(EventDomainObjectAbstract::ORGANIZER_ID, $organizerId);
}
}
$this->model = $this->model->orderBy(
$params->sort_by ?? EventDomainObject::getDefaultSort(),
$params->sort_direction ?? EventDomainObject::getDefaultSortDirection(),
);
return $this->paginateWhere(
where: $where,
limit: $params->per_page,
page: $params->page,
);
}
public function getUpcomingEventsForAdmin(int $perPage): LengthAwarePaginator
{
$now = now();
$next24Hours = now()->addDay();
return $this->handleResults($this->model
->select('events.*')
->with(['account', 'organizer'])
->where(EventDomainObjectAbstract::START_DATE, '>=', $now)
->where(EventDomainObjectAbstract::START_DATE, '<=', $next24Hours)
->whereIn(EventDomainObjectAbstract::STATUS, [
EventStatus::LIVE->name,
])
->orderBy(EventDomainObjectAbstract::START_DATE, 'asc')
->paginate($perPage));
}
public function getAllEventsForAdmin(
?string $search = null,
int $perPage = 20,
?string $sortBy = 'start_date',
?string $sortDirection = 'desc'
): LengthAwarePaginator {
$this->model = $this->model
->select('events.*')
->withCount('attendees');
if ($search) {
$this->model = $this->model->where(function ($q) use ($search) {
$q->where(EventDomainObjectAbstract::TITLE, 'ilike', '%' . $search . '%')
->orWhereHas('organizer', function ($orgQuery) use ($search) {
$orgQuery->where('name', 'ilike', '%' . $search . '%');
});
});
}
$allowedSortColumns = ['start_date', 'end_date', 'title', 'created_at'];
$sortColumn = in_array($sortBy, $allowedSortColumns, true) ? $sortBy : 'start_date';
$sortDir = in_array(strtolower($sortDirection), ['asc', 'desc']) ? $sortDirection : 'desc';
$this->model = $this->model->orderBy($sortColumn, $sortDir);
$this->loadRelation(new Relationship(OrganizerDomainObject::class, name: 'organizer'));
$this->loadRelation(new Relationship(AccountDomainObject::class, name: 'account'));
$this->loadRelation(new Relationship(EventStatisticDomainObject::class, name: 'event_statistics'));
return $this->paginate($perPage);
}
public function getSitemapEvents(int $page, int $perPage): LengthAwarePaginator
{
return $this->handleResults($this->model
->select([
'events.' . EventDomainObjectAbstract::ID,
'events.' . EventDomainObjectAbstract::TITLE,
'events.' . EventDomainObjectAbstract::UPDATED_AT,
'events.' . EventDomainObjectAbstract::START_DATE,
])
->join('event_settings', 'events.id', '=', 'event_settings.event_id')
->where('events.' . EventDomainObjectAbstract::STATUS, EventStatus::LIVE->name)
->where('event_settings.' . EventSettingDomainObjectAbstract::ALLOW_SEARCH_ENGINE_INDEXING, true)
->whereNull('events.' . EventDomainObjectAbstract::DELETED_AT)
->orderBy('events.' . EventDomainObjectAbstract::ID)
->paginate($perPage, ['*'], 'page', $page));
}
public function getSitemapEventCount(): int
{
return $this->model
->newQuery()
->join('event_settings', 'events.id', '=', 'event_settings.event_id')
->where('events.' . EventDomainObjectAbstract::STATUS, EventStatus::LIVE->name)
->where('event_settings.' . EventSettingDomainObjectAbstract::ALLOW_SEARCH_ENGINE_INDEXING, true)
->whereNull('events.' . EventDomainObjectAbstract::DELETED_AT)
->count();
}
}