Skip to content

Commit 0b40d0e

Browse files
committed
feat: add pagination to srm schedule
1 parent c9695b5 commit 0b40d0e

File tree

2 files changed

+24
-5
lines changed

2 files changed

+24
-5
lines changed

src/common/srm-helper.js

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,21 @@ const PracticeProblemsKeyMappings = _.reduce(
4848
* @param {Array<String>} filter.statuses the statues
4949
* @param {Date} filter.registrationStartTimeAfter the start of the registration time
5050
* @param {Date=} filter.registrationStartTimeBefore the end of the registration time
51+
* @param {String} filter.sortBy the sort field
52+
* @param {String} filter.sortOrder the sort order
53+
* @param {Number} filter.page the sort order
54+
* @param {Number} filter.perPage the sort order
5155
*/
5256
function getSRMScheduleQuery(filter) {
57+
const offset = (filter.page - 1) * filter.perPage;
58+
let sortBy = filter.sortBy;
59+
if (criteria.sortBy === "registrationStartTime") {
60+
sortBy = "reg.start_time";
61+
} else if (criteria.sortBy === "codingStartTime") {
62+
sortBy = "coding.start_time";
63+
} else if (criteria.sortBy === "challengeStartTime") {
64+
sortBy = "challenge.start_time";
65+
}
5366
const statuses = _.join(
5467
_.map(filter.statuses, (s) => `'${_.toUpper(s)}'`),
5568
","
@@ -64,8 +77,9 @@ function getSRMScheduleQuery(filter) {
6477
: ""
6578
}`;
6679

67-
const query = `SELECT
68-
FIRST 50
80+
const query = `SELECT
81+
SKIP ${offset}
82+
FIRST ${filter.perPage}
6983
r.round_id AS roundId
7084
, r.name AS name
7185
, r.short_name AS shortName
@@ -95,8 +109,7 @@ function getSRMScheduleQuery(filter) {
95109
r.round_type_id in (1,2,10) AND
96110
UPPER(r.status) in (${statuses}) AND
97111
${registrationTimeFilter}
98-
ORDER BY
99-
reg.start_time DESC`;
112+
ORDER BY ${sortBy} ${filter.sortOrder}`;
100113
return query;
101114
}
102115

src/services/ChallengeService.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2481,6 +2481,12 @@ getSRMSchedule.schema = {
24812481
statuses: Joi.array()
24822482
.items(Joi.string().valid(["A", "F", "P"]))
24832483
.default(["A", "F", "P"]),
2484+
sortBy: Joi.string()
2485+
.valid(["registrationStartTime", "codingStartTime", "challengeStartTime"])
2486+
.default("registrationStartTime"),
2487+
sortOrder: Joi.string().valid(["asc", "desc"]).default("asc"),
2488+
page: Joi.page(),
2489+
perPage: Joi.perPage(),
24842490
}),
24852491
};
24862492

@@ -2494,7 +2500,7 @@ async function getPracticeProblems(currentUser, criteria = {}) {
24942500
const { query, countQuery } = getPracticeProblemsQuery(criteria);
24952501
const resultOutput = await aclQueryDomain.rawQuery({ sql: query });
24962502
const countOutput = await aclQueryDomain.rawQuery({ sql: countQuery });
2497-
const result = convertPracticeProblemsQueryOutput(resultQueryOutput);
2503+
const result = convertPracticeProblemsQueryOutput(resultOutput);
24982504
const total = countOutput.rows[0].fields[0].value;
24992505
return { total, page: criteria.page, perPage: criteria.perPage, result };
25002506
}

0 commit comments

Comments
 (0)