Order Query Results

Understanding the Problem

We need to implement ordering functionality for three different API endpoints:

Devising a Plan

  1. Understand Sequelize order syntax
  2. Modify student endpoint query
  3. Modify classroom endpoint query
  4. Modify supplies endpoint query
  5. Test each endpoint

Solution Implementation

Let's look at each endpoint implementation:

Student Endpoint (routes/api/students.js)

// 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);
});
    

Classroom Endpoint (routes/api/classrooms.js)

// GET /classrooms  
router.get('/', async (req, res) => {
    const options = {
        order: [['name', 'ASC']]  // Sort by name
    };

    const classrooms = await Classroom.findAll(options);
    res.json(classrooms);
});
    

Supplies Endpoint (routes/api/supplies.js)

// 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);
});
    

Real World Application

Ordering data is crucial in real-world applications. Think of:

Testing the Solution

Expected test cases:

Common Pitfalls

Additional Resources