implemented pagination for training reports
This commit is contained in:
@@ -33,23 +33,26 @@ cr.get('/roles', async (req, res) => {
|
||||
})
|
||||
|
||||
er.get('/', async (req: Request, res: Response) => {
|
||||
const allowedSorts = new Map([
|
||||
["ascending", "ASC"],
|
||||
["descending", "DESC"]
|
||||
]);
|
||||
|
||||
const sort = String(req.query.sort || "").toLowerCase();
|
||||
const search = String(req.query.search || "").toLowerCase();
|
||||
if (!allowedSorts.has(sort)) {
|
||||
return res.status(400).json({
|
||||
message: `Invalid sort direction '${req.query.sort}'. Allowed values are 'ascending' or 'descending'.`
|
||||
});
|
||||
}
|
||||
|
||||
const sortDir = allowedSorts.get(sort);
|
||||
|
||||
try {
|
||||
let events = await getCourseEvents(sortDir, search);
|
||||
const allowedSorts = new Map([
|
||||
["ascending", "ASC"],
|
||||
["descending", "DESC"]
|
||||
]);
|
||||
|
||||
const page = Number(req.query.page) || undefined;
|
||||
const pageSize = Number(req.query.pageSize) || undefined;
|
||||
|
||||
const sort = String(req.query.sort || "").toLowerCase();
|
||||
const search = String(req.query.search || "").toLowerCase();
|
||||
if (!allowedSorts.has(sort)) {
|
||||
return res.status(400).json({
|
||||
message: `Invalid sort direction '${req.query.sort}'. Allowed values are 'ascending' or 'descending'.`
|
||||
});
|
||||
}
|
||||
|
||||
const sortDir = allowedSorts.get(sort);
|
||||
|
||||
let events = await getCourseEvents(sortDir, search, page, pageSize);
|
||||
res.status(200).json(events);
|
||||
} catch (error) {
|
||||
console.error('failed to fetch reports', error);
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
import pool from "../db"
|
||||
import { Course, CourseAttendee, CourseAttendeeRole, CourseEventDetails, CourseEventSummary, RawAttendeeRow } from "@app/shared/types/course"
|
||||
import { PagedData } from "@app/shared/types/pagination";
|
||||
import { toDateTime } from "@app/shared/utils/time";
|
||||
export async function getAllCourses(): Promise<Course[]> {
|
||||
const sql = "SELECT * FROM courses WHERE deleted = false ORDER BY name ASC;"
|
||||
@@ -107,7 +108,8 @@ export async function insertCourseEvent(event: CourseEventDetails): Promise<numb
|
||||
}
|
||||
}
|
||||
|
||||
export async function getCourseEvents(sortDir: string, search: string = ""): Promise<CourseEventSummary[]> {
|
||||
export async function getCourseEvents(sortDir: string, search: string = "", page = 1, pageSize = 10): Promise<PagedData<CourseEventSummary>> {
|
||||
const offset = (page - 1) * pageSize;
|
||||
|
||||
let params = [];
|
||||
let searchString = "";
|
||||
@@ -133,11 +135,23 @@ export async function getCourseEvents(sortDir: string, search: string = ""): Pro
|
||||
LEFT JOIN members AS M
|
||||
ON E.created_by = M.id
|
||||
${searchString}
|
||||
ORDER BY E.event_date ${sortDir};`;
|
||||
console.log(sql)
|
||||
console.log(params)
|
||||
let events: CourseEventSummary[] = await pool.query(sql, params);
|
||||
return events;
|
||||
ORDER BY E.event_date ${sortDir}
|
||||
LIMIT ? OFFSET ?;`;
|
||||
|
||||
let countSQL = `SELECT COUNT(*) AS count
|
||||
FROM course_events AS E
|
||||
LEFT JOIN courses AS C
|
||||
ON E.course_id = C.id
|
||||
LEFT JOIN members AS M
|
||||
ON E.created_by = M.id ${searchString};`
|
||||
let recordCount = Number((await pool.query(countSQL, [...params]))[0].count);
|
||||
let pageCount = recordCount / pageSize;
|
||||
|
||||
let events: CourseEventSummary[] = await pool.query(sql, [...params, pageSize, offset]);
|
||||
|
||||
let output: PagedData<CourseEventSummary> = { data: events, pagination: { page: page, pageSize: pageSize, total: recordCount, totalPages: pageCount } }
|
||||
|
||||
return output;
|
||||
}
|
||||
|
||||
export async function getCourseEventRoles(): Promise<CourseAttendeeRole[]> {
|
||||
|
||||
Reference in New Issue
Block a user