Skip to content

Commit

Permalink
Add endpoint for getting an educator.
Browse files Browse the repository at this point in the history
  • Loading branch information
Carifio24 committed Sep 11, 2024
1 parent 36b5373 commit 506808e
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 4 deletions.
13 changes: 10 additions & 3 deletions src/database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,19 +124,25 @@ async function _findStudentByEmail(email: string): Promise<Student | null> {

export async function findStudentByUsername(username: string): Promise<Student | null> {
return Student.findOne({
where: { username: username }
where: { username }
});
}

export async function findStudentById(id: number): Promise<Student | null> {
return Student.findOne({
where: { id : id }
where: { id }
});
}

export async function findEducatorById(id: number): Promise<Educator | null> {
return Educator.findOne({
where: { id: id }
where: { id }
});
}

export async function findEducatorByUsername(username: string): Promise<Educator | null> {
return Educator.findOne({
where: { username },
});
}

Expand Down Expand Up @@ -197,6 +203,7 @@ export interface SignUpEducatorOptions {
last_name: string;
password: string;
email: string;
username: string;
institution?: string;
age?: number;
gender?: string;
Expand Down
23 changes: 22 additions & 1 deletion src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ import {
StageStateQuery,
CreateClassResponse,
UserType,
findEducatorByUsername,
findEducatorById,
} from "./database";

import { getAPIKey, hasPermission } from "./authorization";
Expand Down Expand Up @@ -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)
);
Expand Down Expand Up @@ -464,7 +467,7 @@ app.get([
res.statusCode = 404;
}
res.json({
student: student
student,
});
});

Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit 506808e

Please sign in to comment.