diff --git a/apps/api/src/routes/v1/rest/courses/{id}/+endpoint.ts b/apps/api/src/routes/v1/rest/courses/{id}/+endpoint.ts index 4af644e9..d1a2486f 100644 --- a/apps/api/src/routes/v1/rest/courses/{id}/+endpoint.ts +++ b/apps/api/src/routes/v1/rest/courses/{id}/+endpoint.ts @@ -4,16 +4,17 @@ import { courses } from "virtual:courses"; export const GET = createHandler(async (event, context, res) => { const headers = event.headers; const requestId = context.awsRequestId; - const params = event.pathParameters; + const { id } = event.pathParameters ?? {}; - if (params?.id == null) { - return res.createErrorResult(400, "Course number not provided", requestId); + switch (id) { + case null: + case undefined: + return res.createErrorResult(400, "Course number not provided", requestId); + case "all": + return res.createOKResult(Object.values(courses), headers, requestId); + default: + return courses[decodeURIComponent(id)] + ? res.createOKResult(courses[decodeURIComponent(id)], headers, requestId) + : res.createErrorResult(404, `Course ${id} not found`, requestId); } - if (params?.id === "all") { - return res.createOKResult(Object.values(courses), headers, requestId); - } - if (courses[decodeURIComponent(params.id)]) { - return res.createOKResult(courses[decodeURIComponent(params.id)], headers, requestId); - } - return res.createErrorResult(404, `Course ${params.id} not found`, requestId); }); diff --git a/apps/api/src/routes/v1/rest/instructors/{id}/+endpoint.ts b/apps/api/src/routes/v1/rest/instructors/{id}/+endpoint.ts index 03b67d4f..2e96e136 100644 --- a/apps/api/src/routes/v1/rest/instructors/{id}/+endpoint.ts +++ b/apps/api/src/routes/v1/rest/instructors/{id}/+endpoint.ts @@ -4,16 +4,17 @@ import { instructors } from "virtual:instructors"; export const GET = createHandler(async (event, context, res) => { const headers = event.headers; const requestId = context.awsRequestId; - const params = event.pathParameters; + const { id } = event.pathParameters ?? {}; - if (params?.id == null) { - return res.createErrorResult(400, "Instructor UCInetID not provided", requestId); + switch (id) { + case null: + case undefined: + return res.createErrorResult(400, "Instructor UCInetID not provided", requestId); + case "all": + return res.createOKResult(Object.values(instructors), headers, requestId); + default: + return instructors[decodeURIComponent(id)] + ? res.createOKResult(instructors[decodeURIComponent(id)], headers, requestId) + : res.createErrorResult(404, `Instructor ${id} not found`, requestId); } - if (params.id === "all") { - return res.createOKResult(Object.values(instructors), headers, requestId); - } - if (instructors[decodeURIComponent(params.id)]) { - return res.createOKResult(instructors[decodeURIComponent(params.id)], headers, requestId); - } - return res.createErrorResult(404, `Instructor ${params.id} not found`, requestId); });