-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproblem.controller.ts
More file actions
84 lines (68 loc) · 2.87 KB
/
Copy pathproblem.controller.ts
File metadata and controls
84 lines (68 loc) · 2.87 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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
import { BaseController } from "../base.controller";
import type { IProblemService } from "../../services/problems/problem.service";
import { AppError } from "../../utils/app-error";
import { ERRORS } from "../../constants/errors";
import { ApiResponse } from "../../utils/api-response";
import type { ControllerRequest } from "../../types/infrastructure/hono.types";
import type { CreateProblemInput } from "../../validators/problems/problem.validator";
import { createLogger } from "../../libs/utils/logger";
import type { ProblemValidatorService } from "../../services/problems/problem-validator.service";
const logger = createLogger("problem-controller");
import { type ICradle } from "../../libs/awilix-container";
/**
* ProblemController manages problem metadata, search, and pagination.
*/
export class ProblemController extends BaseController {
private readonly problemService: IProblemService;
private readonly problemValidatorService: ProblemValidatorService;
constructor(cradle: ICradle) {
super(cradle);
this.problemService = cradle.problemService;
this.problemValidatorService = cradle.problemValidatorService;
}
async createProblem(req: ControllerRequest<CreateProblemInput>) {
return await this.problemService.upsertProblem(req.body);
}
async getProblemBySlug(req: ControllerRequest<never, { slug: string }>) {
const { slug } = req.params;
// 1. Logic Offloading: Validate slug existence
return await this.problemValidatorService.validateProblemBySlug(slug);
}
async getProblemById(req: ControllerRequest<never, { problem_id: string }>) {
const { problem_id } = req.params;
// 1. Logic Offloading: Validate internal ID existence
return await this.problemValidatorService.validateProblemExists(problem_id);
}
async getProblemsByTopic(
req: ControllerRequest<never, { topic: string }, { limit?: number }>,
) {
const { topic } = req.params;
const { limit } = req.query;
return await this.problemService.searchByTopic(topic, limit);
}
async getProblems(
req: ControllerRequest<never, any, { page?: number; limit?: number; search?: string; topic?: string; difficulty?: string }>
) {
const { page = 1, limit = 20, search, topic, difficulty } = req.query;
const result = await this.problemService.getAllProblems(page, limit, {
search,
topic,
difficulty,
});
// Return a pre-formatted paginated response
// We removed countDocuments for speed; passing dummy totals to satisfy PaginationMeta
return ApiResponse.paginated(result.problems, {
totalItems: 0,
itemCount: result.problems.length,
perPage: limit,
totalPages: 0,
currentPage: page,
});
}
async getUserSolvedProblems(
req: ControllerRequest<never, { userId: string }>,
) {
const { userId } = req.params;
return await this.problemService.getUserSolvedProblems(userId);
}
}