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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
|
<?php
declare(strict_types=1);
namespace Nsfisis\Albatross\Repositories;
use DateTimeImmutable;
use Nsfisis\Albatross\Database\Connection;
use Nsfisis\Albatross\Exceptions\EntityValidationException;
use Nsfisis\Albatross\Models\AggregatedExecutionStatus;
use Nsfisis\Albatross\Models\Answer;
use Nsfisis\Albatross\Sql\DateTimeParser;
use PDOException;
final class AnswerRepository
{
private const ANSWER_FIELDS = [
'answer_id',
'quiz_id',
'answer_number',
'submitted_at',
'author_id',
'code',
'code_size',
'execution_status',
];
private const ANSWER_JOIN_USER_FIELDS = [
'users.username AS author_name',
'users.is_admin AS author_is_admin',
];
public function __construct(
private readonly Connection $conn,
) {
}
/**
* @return Answer[]
*/
public function listByQuizId(int $quiz_id): array
{
$result = $this->conn
->query()
->select('answers')
->leftJoin('users', 'answers.author_id = users.user_id')
->fields([...self::ANSWER_FIELDS, ...self::ANSWER_JOIN_USER_FIELDS])
->where('quiz_id = :quiz_id')
->orderBy([['execution_status', 'DESC'], ['code_size', 'ASC'], ['submitted_at', 'ASC']])
->execute(['quiz_id' => $quiz_id]);
return array_map($this->mapRawRowToAnswer(...), $result);
}
/**
* @return Answer[]
*/
public function listByQuizIdAndAuthorId(int $quiz_id, int $author_id): array
{
$result = $this->conn
->query()
->select('answers')
->leftJoin('users', 'answers.author_id = users.user_id')
->fields([...self::ANSWER_FIELDS, ...self::ANSWER_JOIN_USER_FIELDS])
->where('quiz_id = :quiz_id AND author_id = :author_id')
->orderBy([['execution_status', 'DESC'], ['code_size', 'ASC'], ['submitted_at', 'ASC']])
->execute(['quiz_id' => $quiz_id, 'author_id' => $author_id]);
return array_map($this->mapRawRowToAnswer(...), $result);
}
public function findByQuizIdAndAnswerNumber(int $quiz_id, int $answer_number): ?Answer
{
$result = $this->conn
->query()
->select('answers')
->leftJoin('users', 'answers.author_id = users.user_id')
->fields([...self::ANSWER_FIELDS, ...self::ANSWER_JOIN_USER_FIELDS])
->where('quiz_id = :quiz_id AND answer_number = :answer_number')
->first()
->execute(['quiz_id' => $quiz_id, 'answer_number' => $answer_number]);
return isset($result) ? $this->mapRawRowToAnswer($result) : null;
}
public function findById(int $answer_id): ?Answer
{
$result = $this->conn
->query()
->select('answers')
->leftJoin('users', 'answers.author_id = users.user_id')
->fields([...self::ANSWER_FIELDS, ...self::ANSWER_JOIN_USER_FIELDS])
->where('answer_id = :answer_id')
->first()
->execute(['answer_id' => $answer_id]);
return isset($result) ? $this->mapRawRowToAnswer($result) : null;
}
/**
* @param ?positive-int $upto
* @return Answer[]
*/
public function getRankingByBestScores(int $quiz_id, ?int $upto): array
{
$q = $this->conn
->query()
->select('answers')
->leftJoin('users', 'answers.author_id = users.user_id')
->fields([
...self::ANSWER_FIELDS,
...self::ANSWER_JOIN_USER_FIELDS,
'ROW_NUMBER() OVER(PARTITION BY answers.author_id ORDER BY answers.code_size ASC, answers.submitted_at ASC) AS r',
])
->where('quiz_id = :quiz_id AND execution_status = :execution_status');
$query = $this->conn
->query()
->select($q)
->fields([
...self::ANSWER_FIELDS,
'author_name',
'author_is_admin',
])
->where('r = 1')
->orderBy([['code_size', 'ASC'], ['submitted_at', 'ASC']]);
if ($upto !== null) {
$query = $query->limit($upto);
}
$result = $query
->execute(['quiz_id' => $quiz_id, 'execution_status' => AggregatedExecutionStatus::OK->toInt()]);
return array_map($this->mapRawRowToAnswer(...), $result);
}
public function getBestCode(int $quiz_id): ?string
{
$result = $this->conn
->query()
->select('answers')
->leftJoin('users', 'answers.author_id = users.user_id')
->fields([...self::ANSWER_FIELDS, ...self::ANSWER_JOIN_USER_FIELDS])
->where('quiz_id = :quiz_id AND execution_status = :execution_status')
->orderBy([['code_size', 'ASC'], ['submitted_at', 'ASC']])
->first()
->execute(['quiz_id' => $quiz_id, 'execution_status' => AggregatedExecutionStatus::OK->toInt()]);
return isset($result) ? $this->mapRawRowToAnswer($result)->code : null;
}
/**
* @return Answer[]
*/
public function listAllCorrectAnswers(int $quiz_id): array
{
$result = $this->conn
->query()
->select('answers')
->leftJoin('users', 'answers.author_id = users.user_id')
->fields([...self::ANSWER_FIELDS, ...self::ANSWER_JOIN_USER_FIELDS])
->where('quiz_id = :quiz_id AND execution_status = :execution_status')
->orderBy([['submitted_at', 'ASC']])
->execute(['quiz_id' => $quiz_id, 'execution_status' => AggregatedExecutionStatus::OK->toInt()]);
return array_map($this->mapRawRowToAnswer(...), $result);
}
public function countUniqueAuthors(): int
{
$result = $this->conn
->query()
->select('answers')
->leftJoin('users', 'answers.author_id = users.user_id')
->fields(['COUNT(DISTINCT author_id) AS count'])
->first()
->execute();
assert(isset($result['count']));
return (int) $result['count'];
}
public function countAll(): int
{
$result = $this->conn
->query()
->select('answers')
->leftJoin('users', 'answers.author_id = users.user_id')
->fields(['COUNT(*) AS count'])
->first()
->execute();
assert(isset($result['count']));
return (int) $result['count'];
}
public function create(
int $quiz_id,
int $author_id,
string $code,
): int {
$answer = Answer::create(
quiz_id: $quiz_id,
author_id: $author_id,
code: $code,
);
$next_answer_number_query = $this->conn
->query()
->select('answers')
->fields(['COALESCE(MAX(answer_number), 0) + 1'])
->where('quiz_id = :quiz_id')
->limit(1);
try {
return $this->conn
->query()
->insert('answers')
->values([
'quiz_id' => $answer->quiz_id,
'answer_number' => $next_answer_number_query,
'author_id' => $answer->author_id,
'code' => $answer->code,
'code_size' => $answer->code_size,
'execution_status' => $answer->execution_status->toInt(),
])
->execute();
} catch (PDOException $e) {
throw new EntityValidationException(
message: '回答の投稿に失敗しました',
previous: $e,
);
}
}
public function markAllAsPending(int $quiz_id): void
{
$this->conn
->query()
->update('answers')
->set(['execution_status' => AggregatedExecutionStatus::Pending->toInt()])
->where('quiz_id = :quiz_id')
->execute(['quiz_id' => $quiz_id]);
}
public function markAllAsUpdateNeeded(int $quiz_id): void
{
$this->conn
->query()
->update('answers')
->set(['execution_status' => AggregatedExecutionStatus::UpdateNeeded->toInt()])
->where('quiz_id = :quiz_id')
->execute(['quiz_id' => $quiz_id]);
}
public function markAsPending(int $answer_id): void
{
$this->conn
->query()
->update('answers')
->set(['execution_status' => AggregatedExecutionStatus::Pending->toInt()])
->where('answer_id = :answer_id')
->execute(['answer_id' => $answer_id]);
}
public function tryGetNextUpdateNeededAnswer(): ?Answer
{
$result = $this->conn
->query()
->select('answers')
->leftJoin('users', 'answers.author_id = users.user_id')
->fields([...self::ANSWER_FIELDS, ...self::ANSWER_JOIN_USER_FIELDS])
->where('execution_status = :execution_status')
->orderBy([['submitted_at', 'ASC']])
->first()
->execute(['execution_status' => AggregatedExecutionStatus::UpdateNeeded->toInt()]);
return isset($result) ? $this->mapRawRowToAnswer($result) : null;
}
public function updateExecutionStatus(
int $answer_id,
AggregatedExecutionStatus $execution_status,
): void {
$this->conn
->query()
->update('answers')
->set(['execution_status' => $execution_status->toInt()])
->where('answer_id = :answer_id')
->execute(['answer_id' => $answer_id]);
}
public function deleteAllByQuizId(int $quiz_id): void
{
$this->conn
->query()
->delete('answers')
->where('quiz_id = :quiz_id')
->execute(['quiz_id' => $quiz_id]);
}
public function deleteAllByUserId(int $user_id): void
{
$this->conn
->query()
->delete('answers')
->where('author_id = :author_id')
->execute(['author_id' => $user_id]);
}
/**
* @param array<string, ?string> $row
*/
private function mapRawRowToAnswer(array $row): Answer
{
assert(isset($row['answer_id']));
assert(isset($row['quiz_id']));
assert(isset($row['answer_number']));
assert(isset($row['submitted_at']));
assert(isset($row['author_id']));
assert(isset($row['code']));
assert(isset($row['code_size']));
assert(isset($row['execution_status']));
$answer_id = (int) $row['answer_id'];
$quiz_id = (int) $row['quiz_id'];
$answer_number = (int) $row['answer_number'];
$submitted_at = DateTimeParser::parse($row['submitted_at']);
assert($submitted_at instanceof DateTimeImmutable, "Failed to parse " . $row['submitted_at']);
$author_id = (int) $row['author_id'];
return new Answer(
answer_id: $answer_id,
quiz_id: $quiz_id,
answer_number: $answer_number,
submitted_at: $submitted_at,
author_id: $author_id,
code: $row['code'],
code_size: (int) $row['code_size'],
execution_status: AggregatedExecutionStatus::fromInt((int)$row['execution_status']),
author_name: $row['author_name'] ?? null,
author_is_admin: (bool) ($row['author_is_admin'] ?? null),
);
}
}
|