Skip to content

Commit 97e6007

Browse files
committed
feat: add pagination to practice problems api
1 parent c8dcd14 commit 97e6007

File tree

3 files changed

+19
-10
lines changed

3 files changed

+19
-10
lines changed

src/common/srm-helper.js

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,9 @@ function getPracticeProblemsQuery(criteria) {
163163
);
164164
}
165165

166-
const query = `SELECT
166+
const queryCount = `SELECT count(*) AS count`;
167+
168+
const querySelect = `SELECT
167169
SKIP ${offset}
168170
FIRST ${criteria.perPage}
169171
p.problem_id AS problemId
@@ -182,8 +184,9 @@ function getPracticeProblemsQuery(criteria) {
182184
WHEN pcs.status_id = 150 THEN 'Solved'::nvarchar(50)
183185
WHEN pcs.status_id >= 120 AND pcs.status_id != 150 THEN 'Viewed'::nvarchar(50)
184186
END AS status
185-
, NVL(pcs.points, 0) AS myPoints
186-
FROM informixoltp:problem p
187+
, NVL(pcs.points, 0) AS myPoints`;
188+
189+
const queryFrom = `FROM informixoltp:problem p
187190
INNER JOIN informixoltp:problem_type_lu ptl ON ptl.problem_type_id = p.problem_type_id
188191
INNER JOIN informixoltp:component c ON c.problem_id = p.problem_id
189192
INNER JOIN informixoltp:round_component rc ON rc.component_id = c.component_id
@@ -193,9 +196,11 @@ function getPracticeProblemsQuery(criteria) {
193196
criteria.userId
194197
}
195198
WHERE ${_.join(filters, " AND ")}
196-
ORDER BY ${sortBy} ${criteria.sortOrder}
197-
`;
198-
return query;
199+
ORDER BY ${sortBy} ${criteria.sortOrder}`;
200+
201+
const query = `${querySelect} ${queryFrom}`;
202+
const countQuery = `${queryCount} ${queryFrom}`;
203+
return { query, countQuery };
199204
}
200205

201206
function convertSRMScheduleQueryOutput(queryOutput) {

src/controllers/ChallengeController.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,8 @@ async function getSRMSchedule(req, res) {
143143
*/
144144
async function getPracticeProblems(req, res) {
145145
const result = await service.getPracticeProblems(req, req.authUser, req.query);
146-
res.send(result);
146+
helper.setResHeaders(req, res, result);
147+
res.send(result.result);
147148
}
148149

149150
module.exports = {

src/services/ChallengeService.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2491,9 +2491,12 @@ getSRMSchedule.schema = {
24912491
*/
24922492
async function getPracticeProblems(currentUser, criteria = {}) {
24932493
criteria.userId = currentUser.userId;
2494-
const sql = getPracticeProblemsQuery(criteria);
2495-
const result = await aclQueryDomain.rawQuery({ sql });
2496-
return convertPracticeProblemsQueryOutput(result);
2494+
const { query, countQuery } = getPracticeProblemsQuery(criteria);
2495+
const resultOutput = await aclQueryDomain.rawQuery({ sql: query });
2496+
const countOutput = await aclQueryDomain.rawQuery({ sql: countQuery });
2497+
const result = convertPracticeProblemsQueryOutput(resultQueryOutput);
2498+
const total = countOutput.rows[0].fields[0].value;
2499+
return { total, page: criteria.page, perPage: criteria.perPage, result };
24972500
}
24982501

24992502
getPracticeProblems.schema = {

0 commit comments

Comments
 (0)