-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEventRepository.php
More file actions
207 lines (178 loc) · 6.55 KB
/
EventRepository.php
File metadata and controls
207 lines (178 loc) · 6.55 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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
<?php
namespace App\Repositories;
use App\Models\Event;
use App\Models\User;
use Exception;
use PDO;
class EventRepository
{
public function __construct(
private PDO $pdo,
private string $prefix
) {}
public function selectList(): array
{
$stmt = $this->pdo->query('
SELECT c.*, u.email as admin
FROM ' . $this->prefix . 'charity_event c
INNER JOIN ' . $this->prefix . 'user_right r ON r.id_charity_event = c.id
INNER JOIN ' . $this->prefix . 'users u ON u.id = r.id_user
ORDER BY c.creation_date DESC
');
$stmt->setFetchMode(PDO::FETCH_CLASS, Event::class);
return $stmt->fetchAll();
}
public function selectListByUser(User $user): array
{
$stmt = $this->pdo->prepare('
SELECT c.*
FROM ' . $this->prefix . 'charity_event c
INNER JOIN ' . $this->prefix . 'user_right ur on ur.id_charity_event = c.id
WHERE ur.id_user = ?
ORDER BY c.creation_date DESC
');
$stmt->setFetchMode(PDO::FETCH_CLASS, Event::class);
$stmt->execute([$user->id]);
return $stmt->fetchAll();
}
public function selectByUserAndId(User $user, int $id): ?Event
{
if ($user->role === "ADMIN") {
$stmt = $this->pdo->prepare('
SELECT c.*
FROM ' . $this->prefix . 'charity_event c
WHERE c.id = ?
');
$stmt->setFetchMode(PDO::FETCH_CLASS, Event::class);
$stmt->execute([$id]);
} else {
$stmt = $this->pdo->prepare('
SELECT c.*
FROM ' . $this->prefix . 'charity_event c
INNER JOIN ' . $this->prefix . 'user_right ur on ur.id_charity_event = c.id
WHERE ur.id_user = ?
AND c.id = ?
');
$stmt->setFetchMode(PDO::FETCH_CLASS, Event::class);
$stmt->execute([$user->id, $id]);
}
$event = $stmt->fetch();
return $event ?: null;
}
public function selectByUserAndGuid(User $user, string $guid): ?Event
{
if ($user->role === "ADMIN") {
$stmt = $this->pdo->prepare('
SELECT c.*
FROM ' . $this->prefix . 'charity_event c
WHERE c.guid = ?
');
$stmt->setFetchMode(PDO::FETCH_CLASS, Event::class);
$stmt->execute([$guid]);
} else {
$stmt = $this->pdo->prepare('
SELECT c.*
FROM ' . $this->prefix . 'charity_event c
INNER JOIN ' . $this->prefix . 'user_right ur on ur.id_charity_event = c.id
WHERE ur.id_user = ?
AND c.guid = ?
');
$stmt->setFetchMode(PDO::FETCH_CLASS, Event::class);
$stmt->execute([$user->id, $guid]);
}
$event = $stmt->fetch();
return $event ?: null;
}
public function selectByGuid(string $guid): ?Event
{
$stmt = $this->pdo->prepare('
SELECT *
FROM ' . $this->prefix . 'charity_event
WHERE guid = ?
');
$stmt->setFetchMode(PDO::FETCH_CLASS, Event::class);
$stmt->execute([$guid]);
$event = $stmt->fetch();
return $event ?: null;
}
public function insert(string $title): Event
{
$this->pdo->beginTransaction();
try {
$guid = bin2hex(random_bytes(16));
$stmt = $this->pdo->prepare('INSERT INTO ' . $this->prefix . 'charity_event (guid, title) VALUES (:guid, :title)');
$stmt->execute([':guid' => $guid, ':title' => $title]);
$id = $this->pdo->lastInsertId();
$stmt = $this->pdo->prepare('INSERT INTO ' . $this->prefix . 'widget_donation_goal_bar (charity_event_guid) VALUES (:guid)');
$stmt->execute([':guid' => $guid]);
try {
$stmt = $this->pdo->prepare('INSERT INTO ' . $this->prefix . 'widget_card (charity_event_guid, description) VALUES (:guid, "")');
$stmt->execute([':guid' => $guid]);
} catch (Exception $e) {
// Table may not exist yet (migration not run) — skip silently
}
$this->pdo->commit();
$event = new Event();
$event->id = $id;
$event->guid = $guid;
$event->title = $title;
return $event;
} catch (Exception $e) {
$this->pdo->rollBack();
throw $e;
}
}
public function update(Event $event, array $data): void
{
$fields = [];
$params = [];
if (array_key_exists('title', $data)) {
$fields[] = 'title = ?';
$params[] = $data['title'];
}
if (array_key_exists('goal', $data)) {
$fields[] = 'goal = ?';
$params[] = $data['goal'];
}
if (empty($fields)) return;
$params[] = $event->id;
$sql = 'UPDATE ' . $this->prefix . 'charity_event SET ' . implode(', ', $fields) . ' WHERE id = ?';
$stmt = $this->pdo->prepare($sql);
$stmt->execute($params);
}
public function delete(Event $event)
{
$this->pdo->beginTransaction();
try {
$query = 'UPDATE ' . $this->prefix . 'charity_stream
SET charity_event_id = null
WHERE charity_event_id = ?';
$stmt = $this->pdo->prepare($query);
$stmt->execute([$event->id]);
$query = 'DELETE FROM ' . $this->prefix . 'user_right
WHERE id_charity_event = ?';
$stmt = $this->pdo->prepare($query);
$stmt->execute([$event->id]);
$query = 'DELETE FROM ' . $this->prefix . 'widget_donation_goal_bar
WHERE charity_event_guid = ?';
$stmt = $this->pdo->prepare($query);
$stmt->execute([$event->guid]);
$query = 'DELETE FROM ' . $this->prefix . 'widget_card
WHERE charity_event_guid = ?';
try {
$stmt = $this->pdo->prepare($query);
$stmt->execute([$event->guid]);
} catch (Exception $e) {
// Table may not exist yet — skip silently
}
$query = 'DELETE FROM ' . $this->prefix . 'charity_event
WHERE id = ?';
$stmt = $this->pdo->prepare($query);
$stmt->execute([$event->id]);
$this->pdo->commit();
} catch (Exception $e) {
$this->pdo->rollBack();
throw $e;
}
}
}