Skip to content

Commit 87bb930

Browse files
committed
Add endpoint for getting a class's measurements without reference to a specific student.
1 parent 8ed871c commit 87bb930

File tree

2 files changed

+82
-13
lines changed

2 files changed

+82
-13
lines changed

src/stories/hubbles_law/database.ts

Lines changed: 58 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,55 @@ async function getHubbleMeasurementsForStudentClass(studentID: number,
287287
});
288288
}
289289

290+
export async function getClassMeasurements(classID: number,
291+
includeMergedClasses: boolean = true,
292+
excludeWithNull: boolean = false
293+
): Promise<HubbleMeasurement[]> {
294+
295+
const classWhereConditions: WhereOptions<Class> = [];
296+
if (includeMergedClasses) {
297+
const classIDs = await getMergedIDsForClass(classID);
298+
classWhereConditions.push({ id: { [Op.in]: classIDs } });
299+
} else {
300+
classWhereConditions.push({ id: classID });
301+
}
302+
303+
const measurementWhereConditions: WhereOptions<HubbleMeasurement> = [];
304+
if (excludeWithNull) {
305+
measurementWhereConditions.push(EXCLUDE_MEASUREMENTS_WITH_NULL_CONDITION);
306+
}
307+
308+
return HubbleMeasurement.findAll({
309+
where: measurementWhereConditions,
310+
include: [{
311+
model: Student,
312+
attributes: ["id"],
313+
as: "student",
314+
required: true,
315+
include: [{
316+
model: Class,
317+
attributes: ["id"],
318+
where: classWhereConditions,
319+
},
320+
{
321+
model: IgnoreStudent,
322+
required: false,
323+
attributes: [],
324+
where: {
325+
story_name: "hubbles_law",
326+
student_id: { [Op.is]: null },
327+
}
328+
}],
329+
},
330+
{
331+
model: Galaxy,
332+
attributes: galaxyAttributes,
333+
as: "galaxy",
334+
required: true,
335+
}]
336+
});
337+
}
338+
290339
async function getHubbleStudentDataForClasses(classIDs: number[]): Promise<HubbleStudentData[]> {
291340
return HubbleStudentData.findAll({
292341
include: [{
@@ -390,11 +439,11 @@ export async function getClassDataIDsForStudent(studentID: number): Promise<numb
390439
return state?.getDataValue("class_data_students") ?? [];
391440
}
392441

393-
export async function getClassMeasurements(studentID: number,
394-
classID: number,
395-
lastChecked: number | null = null,
396-
excludeWithNull: boolean = false,
397-
excludeStudent: boolean = false,
442+
export async function getClassMeasurementsForStudent(studentID: number,
443+
classID: number,
444+
lastChecked: number | null = null,
445+
excludeWithNull: boolean = false,
446+
excludeStudent: boolean = false,
398447
): Promise<HubbleMeasurement[]> {
399448
let data = await getHubbleMeasurementsForStudentClass(studentID, classID, excludeWithNull, excludeStudent);
400449
if (data.length > 0 && lastChecked != null) {
@@ -409,11 +458,11 @@ export async function getClassMeasurements(studentID: number,
409458
// The advantage of this over the function above is that it saves bandwidth,
410459
// since we aren't sending the data itself.
411460
// This is intended to be used with cases where we need to frequently check the number of measurements
412-
export async function getClassMeasurementCount(studentID: number,
413-
classID: number,
414-
excludeWithNull: boolean = false,
461+
export async function getClassMeasurementCountForStudent(studentID: number,
462+
classID: number,
463+
excludeWithNull: boolean = false,
415464
): Promise<number> {
416-
const data = await getClassMeasurements(studentID, classID, null, excludeWithNull);
465+
const data = await getClassMeasurementsForStudent(studentID, classID, null, excludeWithNull);
417466
return data?.length ?? 0;
418467
}
419468

src/stories/hubbles_law/router.ts

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import {
2424
setGalaxySpectrumStatus,
2525
getUncheckedSpectraGalaxies,
2626
getClassMeasurements,
27+
getClassMeasurementsForStudent,
2728
getAllHubbleMeasurements,
2829
getAllHubbleStudentData,
2930
getAllHubbleClassData,
@@ -35,7 +36,7 @@ import {
3536
getGalaxyById,
3637
removeSampleHubbleMeasurement,
3738
getAllNthSampleHubbleMeasurements,
38-
getClassMeasurementCount,
39+
getClassMeasurementCountForStudent,
3940
getStudentsWithCompleteMeasurementsCount,
4041
getMergedIDsForClass,
4142
addClassToMergeGroup,
@@ -203,6 +204,25 @@ router.delete("/sample-measurement/:studentID/:measurementNumber", async (req, r
203204
});
204205
});
205206

207+
router.get("/measurements/classes/:classID", async (req, res) => {
208+
const classID = parseInt(req.params.classID);
209+
const isValidClass = (await findClassById(classID)) !== null;
210+
if (!isValidClass) {
211+
res.status(404).json({
212+
message: `No class with ID ${classID}`,
213+
});
214+
return;
215+
}
216+
217+
const completeOnly = (req.query.complete_only as string)?.toLowerCase() === "true";
218+
const excludeMergedClasses = (req.query.exclude_merge as string)?.toLowerCase() === "true";
219+
const measurements = await getClassMeasurements(classID, !excludeMergedClasses, completeOnly);
220+
res.status(200).json({
221+
class_id: classID,
222+
measurements,
223+
});
224+
});
225+
206226
router.get("/measurements/:studentID", async (req, res) => {
207227
const params = req.params;
208228
const studentID = parseInt(params.studentID);
@@ -298,7 +318,7 @@ router.get("/class-measurements/size/:studentID/:classID", async (req, res) => {
298318
}
299319

300320
const completeOnly = (req.query.complete_only as string)?.toLowerCase() === "true";
301-
const count = await getClassMeasurementCount(studentID, classID, completeOnly);
321+
const count = await getClassMeasurementCountForStudent(studentID, classID, completeOnly);
302322
res.status(200).json({
303323
student_id: studentID,
304324
class_id: classID,
@@ -369,7 +389,7 @@ router.get(["/class-measurements/:studentID/:classID", "/stage-3-data/:studentID
369389
return;
370390
}
371391

372-
const measurements = await getClassMeasurements(student.id, cls.id, lastChecked, completeOnly, excludeStudent);
392+
const measurements = await getClassMeasurementsForStudent(student.id, cls.id, lastChecked, completeOnly, excludeStudent);
373393
res.status(200).json({
374394
student_id: studentID,
375395
class_id: classID,
@@ -397,7 +417,7 @@ router.get(["/class-measurements/:studentID", "stage-3-measurements/:studentID"]
397417
}
398418

399419
const excludeStudent = (req.query.exclude_student as string)?.toLowerCase() === "true";
400-
const measurements = await getClassMeasurements(studentID, cls.id, null, excludeStudent);
420+
const measurements = await getClassMeasurementsForStudent(studentID, cls.id, null, excludeStudent);
401421
res.status(200).json({
402422
student_id: studentID,
403423
class_id: null,

0 commit comments

Comments
 (0)