Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add endpoint for returning the list of students in a given class #137

Merged
merged 1 commit into from
Sep 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading