-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproblem-test.controller.ts
More file actions
63 lines (51 loc) · 1.89 KB
/
Copy pathproblem-test.controller.ts
File metadata and controls
63 lines (51 loc) · 1.89 KB
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
import type { Context } from "hono";
import type {
AppEnv,
ValidatedContext,
} from "../../types/infrastructure/hono.types";
import type { UpsertTestsInput } from "../../validators/problems/problem-test.validator";
import type { IProblemTestService } from "../../services/problems/problem-test.service";
import { ApiResponse } from "../../utils/api-response";
import { AppError } from "../../utils/app-error";
import { BaseController } from "../base.controller";
import { type ICradle } from "../../libs/awilix-container";
export class ProblemTestController extends BaseController {
private readonly problemTestService: IProblemTestService;
constructor(cradle: ICradle) {
super(cradle);
this.problemTestService = cradle.problemTestService;
}
async upsertTests(
c: Context<AppEnv, any, ValidatedContext<UpsertTestsInput>>,
) {
const problem_id = c.req.param("problem_id")!;
const body = c.req.valid("json");
const { type, cases } = body;
const problemTest = await this.problemTestService.upsertTests({
problem_id,
type,
cases,
});
const response = ApiResponse.success(problemTest);
return c.json(response.toJSON(), 201);
}
async getTestsForProblem(c: Context) {
const problem_id = c.req.param("problem_id")!;
const tests = await this.problemTestService.getTestsForProblem(problem_id);
const response = ApiResponse.success(tests);
return c.json(response.toJSON());
}
async getTestsForProblemAndType(c: Context) {
const problem_id = c.req.param("problem_id")!;
const type = c.req.param("type")!.toLowerCase();
const tests = await this.problemTestService.getTestsForProblemAndType(
problem_id,
type as any,
);
if (!tests) {
throw AppError.notFound("Tests not found for given problem and type");
}
const response = ApiResponse.success(tests);
return c.json(response.toJSON());
}
}