implemented pagination for training reports
This commit is contained in:
@@ -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