From deacd0dfc195bca41af631114804d29937337cd8 Mon Sep 17 00:00:00 2001 From: nsfisis Date: Wed, 17 Jan 2024 02:11:31 +0900 Subject: . --- .../app/src/Repositories/TestcaseRepository.php | 143 +++++++++++++++++++++ 1 file changed, 143 insertions(+) create mode 100644 services/app/src/Repositories/TestcaseRepository.php (limited to 'services/app/src/Repositories/TestcaseRepository.php') 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 @@ +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 $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'], + ); + } +} -- cgit v1.2.3-70-g09d2