Skip to content

Commit

Permalink
Merge pull request #137 from Carifio24/class-roster
Browse files Browse the repository at this point in the history
Add endpoint for returning the list of students in a given class
  • Loading branch information
Carifio24 authored Sep 16, 2024
2 parents 9be9e46 + ba59005 commit 0db392a
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 2 deletions.
9 changes: 9 additions & 0 deletions src/associations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,15 @@ export function setUpAssociations() {
foreignKey: "class_id"
});

Student.hasMany(StudentsClasses, {
foreignKey: "student_id",
});
StudentsClasses.belongsTo(Student, {
as: "student",
targetKey: "id",
foreignKey: "student_id",
});

Class.hasMany(StudentsClasses, {
foreignKey: "class_id"
});
Expand Down
13 changes: 13 additions & 0 deletions src/database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -611,6 +611,19 @@ export async function getRosterInfo(classID: number, useDisplayNames = true): Pr
}, {});
}

export async function getClassRoster(classID: number): Promise<Student[]> {
return Student.findAll({
include: [{
model: StudentsClasses,
required: true,
attributes: [],
where: {
class_id: classID,
},
}]
});
}

/** These functions are for testing purposes only */
export async function newDummyClassForStory(storyName: string): Promise<{cls: Class, dummy: DummyClass}> {
const ct = await Class.count({
Expand Down
16 changes: 14 additions & 2 deletions src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ import {
findEducatorById,
CreateClassSchema,
QuestionInfoSchema,
getClassRoster,
} from "./database";

import {
Expand Down Expand Up @@ -487,6 +488,19 @@ export function createApp(db: Sequelize): Express {
size
});
});

app.get("/classes/roster/:classID", async (req, res) => {
const classID = Number(req.params.classID);
const cls = await findClassById(classID);
if (cls === null) {
res.status(404).json({
error: `No class found with ID ${classID}`,
});
}

const students = await getClassRoster(classID);
res.json(students);
});

app.get("/story-state/:studentID/:storyName", async (req, res) => {
const params = req.params;
Expand Down Expand Up @@ -719,7 +733,6 @@ export function createApp(db: Sequelize): Express {
});
});


app.post("/question/:tag", async (req, res) => {

const data = { ...req.body, tag: req.params.tag };
Expand Down Expand Up @@ -750,7 +763,6 @@ export function createApp(db: Sequelize): Express {
});
});


app.get("/questions/:storyName", async (req, res) => {
const storyName = req.params.storyName;
const newestOnlyString = req.query.newest_only as string;
Expand Down

0 comments on commit 0db392a

Please sign in to comment.