From deacd0dfc195bca41af631114804d29937337cd8 Mon Sep 17 00:00:00 2001 From: nsfisis Date: Wed, 17 Jan 2024 02:11:31 +0900 Subject: . --- services/app/src/Forms/AdminTestcaseEditForm.php | 122 +++++++++++++++++++++++ 1 file changed, 122 insertions(+) create mode 100644 services/app/src/Forms/AdminTestcaseEditForm.php (limited to 'services/app/src/Forms/AdminTestcaseEditForm.php') diff --git a/services/app/src/Forms/AdminTestcaseEditForm.php b/services/app/src/Forms/AdminTestcaseEditForm.php new file mode 100644 index 0000000..aa87c8a --- /dev/null +++ b/services/app/src/Forms/AdminTestcaseEditForm.php @@ -0,0 +1,122 @@ + $testcase->input, + 'expected_result' => $testcase->expected_result, + ]); + } + parent::__construct($state); + } + + public function pageTitle(): string + { + return "管理画面 - 問題 #{$this->quiz->quiz_id} - テストケース #{$this->testcase->testcase_id} - 編集"; + } + + public function redirectUrl(): string + { + return $this->routeParser->urlFor('admin_testcase_list', ['qslug' => $this->quiz->slug]); + } + + protected function submitLabel(): string + { + return '保存'; + } + + /** + * @return list + */ + protected function items(): array + { + return [ + new FormItem( + name: 'input', + type: 'textarea', + label: '標準入力', + extra: 'rows="10" cols="80"', + ), + new FormItem( + name: 'expected_result', + type: 'textarea', + label: '期待する出力', + isRequired: true, + extra: 'rows="10" cols="80"', + ), + ]; + } + + /** + * @return array{testcase: Testcase, quiz: Quiz} + */ + public function getRenderContext(): array + { + return [ + 'testcase' => $this->testcase, + 'quiz' => $this->quiz, + ]; + } + + public function submit(): void + { + $input = $this->state->get('input') ?? ''; + $expected_result = $this->state->get('expected_result') ?? ''; + + $errors = []; + if ($expected_result === '') { + $errors['expected_result'] = '期待する出力は必須です'; + } + if (0 < count($errors)) { + $this->state->setErrors($errors); + throw new FormSubmissionFailureException(); + } + + try { + $this->conn->transaction(function () use ($input, $expected_result): void { + $quiz_id = $this->quiz->quiz_id; + + $this->testcaseRepo->update( + testcase_id: $this->testcase->testcase_id, + input: $input, + expected_result: $expected_result, + ); + $this->answerRepo->markAllAsPending($quiz_id); + $this->testcaseExecutionRepo->markAllAsPendingByTestcaseId( + testcase_id: $this->testcase->testcase_id, + ); + }); + } catch (EntityValidationException $e) { + $this->state->setErrors($e->toFormErrors()); + throw new FormSubmissionFailureException(previous: $e); + } + } +} -- cgit v1.2.3-70-g09d2