aboutsummaryrefslogtreecommitdiffhomepage
path: root/services/app/src/Repositories/TestcaseRepository.php
blob: 4fd329700deeb068a22ca10972d98714902f0ee9 (plain)
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
<?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'],
        );
    }
}