added searching and sorting system
This commit is contained in:
@@ -26,8 +26,23 @@ courseRouter.get('/roles', async (req, res) => {
|
||||
})
|
||||
|
||||
eventRouter.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();
|
||||
let events = await getCourseEvents(sortDir, search);
|
||||
res.status(200).json(events);
|
||||
} catch (error) {
|
||||
console.error('failed to fetch reports', error);
|
||||
|
||||
Reference in New Issue
Block a user