aboutsummaryrefslogtreecommitdiffhomepage
path: root/services/app/src/Repositories/TestcaseRepository.php
diff options
context:
space:
mode:
authornsfisis <nsfisis@gmail.com>2024-01-17 02:11:31 +0900
committernsfisis <nsfisis@gmail.com>2024-01-17 02:11:31 +0900
commitdeacd0dfc195bca41af631114804d29937337cd8 (patch)
treef1f83580e5bc892c0794ac41632bc0cce3498f65 /services/app/src/Repositories/TestcaseRepository.php
parent38ddeb28ec846ee966d0fe6873585d697a9ef373 (diff)
downloadphperkaigi-2024-albatross-deacd0dfc195bca41af631114804d29937337cd8.tar.gz
phperkaigi-2024-albatross-deacd0dfc195bca41af631114804d29937337cd8.tar.zst
phperkaigi-2024-albatross-deacd0dfc195bca41af631114804d29937337cd8.zip
.
Diffstat (limited to 'services/app/src/Repositories/TestcaseRepository.php')
-rw-r--r--services/app/src/Repositories/TestcaseRepository.php143
1 files changed, 143 insertions, 0 deletions
diff --git a/services/app/src/Repositories/TestcaseRepository.php b/services/app/src/Repositories/TestcaseRepository.php
new file mode 100644
index 0000000..4fd3297
--- /dev/null
+++ b/services/app/src/Repositories/TestcaseRepository.php
@@ -0,0 +1,143 @@
+<?php
+
+declare(strict_types=1);
+
+namespace Nsfisis\Albatross\Repositories;
+
+use Nsfisis\Albatross\Database\Connection;
+use Nsfisis\Albatross\Exceptions\EntityValidationException;
+use Nsfisis\Albatross\Models\Testcase;
+use PDOException;
+
+final class TestcaseRepository
+{
+ private const TESTCASE_FIELDS = [
+ 'testcase_id',
+ 'quiz_id',
+ 'input',
+ 'expected_result',
+ ];
+
+ public function __construct(
+ private readonly Connection $conn,
+ ) {
+ }
+
+ public function findByQuizIdAndTestcaseId(
+ int $quiz_id,
+ int $testcase_id,
+ ): ?Testcase {
+ $result = $this->conn
+ ->query()
+ ->select('testcases')
+ ->fields(self::TESTCASE_FIELDS)
+ ->where('quiz_id = :quiz_id AND testcase_id = :testcase_id')
+ ->first()
+ ->execute([
+ 'quiz_id' => $quiz_id,
+ 'testcase_id' => $testcase_id,
+ ]);
+ return isset($result) ? $this->mapRawRowToTestcase($result) : null;
+ }
+
+ /**
+ * @return Testcase[]
+ */
+ public function listByQuizId(int $quiz_id): array
+ {
+ $result = $this->conn
+ ->query()
+ ->select('testcases')
+ ->fields(self::TESTCASE_FIELDS)
+ ->where('quiz_id = :quiz_id')
+ ->orderBy([['testcase_id', 'ASC']])
+ ->execute(['quiz_id' => $quiz_id]);
+ return array_map($this->mapRawRowToTestcase(...), $result);
+ }
+
+ public function create(
+ int $quiz_id,
+ string $input,
+ string $expected_result,
+ ): int {
+ $testcase = Testcase::create(
+ quiz_id: $quiz_id,
+ input: $input,
+ expected_result: $expected_result,
+ );
+
+ $values = [
+ 'quiz_id' => $testcase->quiz_id,
+ 'input' => $testcase->input,
+ 'expected_result' => $testcase->expected_result,
+ ];
+
+ try {
+ return $this->conn
+ ->query()
+ ->insert('testcases')
+ ->values($values)
+ ->execute();
+ } catch (PDOException $e) {
+ throw new EntityValidationException(
+ message: 'テストケースの作成に失敗しました',
+ previous: $e,
+ );
+ }
+ }
+
+ public function update(
+ int $testcase_id,
+ string $input,
+ string $expected_result,
+ ): void {
+ try {
+ $this->conn
+ ->query()
+ ->update('testcases')
+ ->set([
+ 'input' => $input,
+ 'expected_result' => $expected_result,
+ ])
+ ->where('testcase_id = :testcase_id')
+ ->execute([
+ 'testcase_id' => $testcase_id,
+ ]);
+ } catch (PDOException $e) {
+ throw new EntityValidationException(
+ message: 'テストケースの更新に失敗しました',
+ previous: $e,
+ );
+ }
+ }
+
+ public function delete(int $testcase_id): void
+ {
+ $this->conn
+ ->query()
+ ->delete('testcases')
+ ->where('testcase_id = :testcase_id')
+ ->execute(['testcase_id' => $testcase_id]);
+ }
+
+ /**
+ * @param array<string, string> $row
+ */
+ private function mapRawRowToTestcase(array $row): Testcase
+ {
+ assert(isset($row['testcase_id']));
+ assert(isset($row['quiz_id']));
+ assert(isset($row['input']));
+ assert(isset($row['expected_result']));
+
+ $testcase_id = (int) $row['testcase_id'];
+ $quiz_id = (int) $row['quiz_id'];
+
+ return new Testcase(
+ testcase_id: $testcase_id,
+ quiz_id: $quiz_id,
+ input: $row['input'],
+ expected_result: $row['expected_result'],
+ );
+ }
+}