Skip to content

Commit

Permalink
Only return 404 errors for stage 3 data endpoints if one of the IDs i…
Browse files Browse the repository at this point in the history
…s invalid.
  • Loading branch information
Carifio24 committed Jul 25, 2023
1 parent 3d152d2 commit 400f2fc
Showing 1 changed file with 25 additions and 4 deletions.
29 changes: 25 additions & 4 deletions src/stories/hubbles_law/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ import {
} from "./request_results";

import { Router } from "express";
import { findClassById, findStudentById } from "../../database";

const router = Router();

Expand Down Expand Up @@ -267,9 +268,22 @@ router.get("/stage-3-data/:studentID/:classID", async (req, res) => {
if (classID === 0) {
classID = 159;
}

const invalidStudent = (await findStudentById(studentID)) === null;
const invalidClass = (await findClassById(classID)) === null;
if (invalidStudent || invalidClass) {
const invalidItems = [];
if (invalidStudent) { invalidItems.push("student"); }
if (invalidClass) { invalidItems.push("class"); }
const message = `Invalid ${invalidItems.join(" and ")} ID${invalidItems.length == 2 ? "s": ""}`;
res.status(404).json({
message
});
return;
}

const measurements = await getStageThreeMeasurements(studentID, classID, lastChecked);
const status = measurements.length === 0 ? 404 : 200;
res.status(status).json({
res.status(200).json({
studentID,
classID,
measurements
Expand All @@ -279,9 +293,16 @@ router.get("/stage-3-data/:studentID/:classID", async (req, res) => {
router.get("/stage-3-data/:studentID", async (req, res) => {
const params = req.params;
const studentID = parseInt(params.studentID);
const isValidStudent = (await findStudentById(studentID)) !== null;
if (!isValidStudent) {
res.status(404).json({
message: "Invalid student ID"
});
return;
}

const measurements = await getStageThreeMeasurements(studentID, null);
const status = measurements.length === 0 ? 404 : 200;
res.status(status).json({
res.status(200).json({
studentID,
measurements,
classID: null
Expand Down

0 comments on commit 400f2fc

Please sign in to comment.