We need to implement ordering functionality for three different API endpoints:
Let's look at each endpoint implementation:
// GET /students
router.get('/', async (req, res) => {
// Base query options
const options = {
order: [
['lastName', 'ASC'], // Primary sort by lastName
['firstName', 'ASC'] // Secondary sort by firstName
]
};
const students = await Student.findAll(options);
res.json(students);
});
// GET /classrooms
router.get('/', async (req, res) => {
const options = {
order: [['name', 'ASC']] // Sort by name
};
const classrooms = await Classroom.findAll(options);
res.json(classrooms);
});
// GET /supplies/category/:categoryName
router.get('/category/:categoryName', async (req, res) => {
const options = {
where: {
category: req.params.categoryName
},
order: [
['name', 'ASC'], // Primary sort by name
['handed', 'ASC'] // Secondary sort by handed
]
};
const supplies = await Supply.findAll(options);
res.json(supplies);
});
Ordering data is crucial in real-world applications. Think of:
Expected test cases: