From 506808e2e7bd04b02bb54dd6ccfe59b5e1de5d0a Mon Sep 17 00:00:00 2001 From: Carifio24 Date: Wed, 11 Sep 2024 13:10:36 -0400 Subject: [PATCH] Add endpoint for getting an educator. --- src/database.ts | 13 ++++++++++--- src/server.ts | 23 ++++++++++++++++++++++- 2 files changed, 32 insertions(+), 4 deletions(-) diff --git a/src/database.ts b/src/database.ts index e69051e..6f408ff 100644 --- a/src/database.ts +++ b/src/database.ts @@ -124,19 +124,25 @@ async function _findStudentByEmail(email: string): Promise { export async function findStudentByUsername(username: string): Promise { return Student.findOne({ - where: { username: username } + where: { username } }); } export async function findStudentById(id: number): Promise { return Student.findOne({ - where: { id : id } + where: { id } }); } export async function findEducatorById(id: number): Promise { return Educator.findOne({ - where: { id: id } + where: { id } + }); +} + +export async function findEducatorByUsername(username: string): Promise { + return Educator.findOne({ + where: { username }, }); } @@ -197,6 +203,7 @@ export interface SignUpEducatorOptions { last_name: string; password: string; email: string; + username: string; institution?: string; age?: number; gender?: string; diff --git a/src/server.ts b/src/server.ts index 8959961..948e56b 100644 --- a/src/server.ts +++ b/src/server.ts @@ -39,6 +39,8 @@ import { StageStateQuery, CreateClassResponse, UserType, + findEducatorByUsername, + findEducatorById, } from "./database"; import { getAPIKey, hasPermission } from "./authorization"; @@ -232,6 +234,7 @@ app.post("/educator-sign-up", async (req, res) => { typeof data.password === "string" && ((typeof data.institution === "string") || (data.institution == null)) && typeof data.email === "string" && + typeof data.username === "string" && ((typeof data.age === "number") || (data.age == null)) && ((typeof data.gender === "string") || data.gender == null) ); @@ -464,7 +467,7 @@ app.get([ res.statusCode = 404; } res.json({ - student: student + student, }); }); @@ -495,6 +498,24 @@ app.get("/students/:identifier/classes", async (req, res) => { }); +app.get("/educators/:identifier", async (req, res) => { + const params = req.params; + const id = Number(params.identifier); + + let educator; + if (isNaN(id)) { + educator = await findEducatorByUsername(params.identifier); + } else { + educator = await findEducatorById(id); + } + if (educator == null) { + res.statusCode = 404; + } + res.json({ + educator, + }); +}); + app.post("/classes/join", async (req, res) => { const username = req.body.username as string; const classCode = req.body.class_code as string;