Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Combine handlers for redundant endpoints #129

Merged
merged 1 commit into from
Sep 12, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 9 additions & 55 deletions src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,10 @@ app.listen(PORT, () => {
});

// Educator sign-up
app.post("/educator-sign-up", async (req, res) => {
app.post([
"/educators/create",
"/educator-sign-up", // Old
], async (req, res) => {
const data = req.body;
const valid = (
typeof data.firstName === "string" &&
Expand Down Expand Up @@ -254,13 +257,16 @@ app.post("/educator-sign-up", async (req, res) => {
});

// Student sign-up
app.post("/student-sign-up", async (req, res) => {
app.post([
"/students/create",
"/student-sign-up", // Old
], async (req, res) => {
const data = req.body;
const valid = (
typeof data.username === "string" &&
typeof data.password === "string" &&
((typeof data.institution === "string") || (data.institution == null)) &&
typeof data.email === "string" &&
((typeof data.email === "string") || (data.email == null)) &&
((typeof data.age === "number") || (data.age == null)) &&
((typeof data.gender === "string") || (data.gender == null)) &&
((typeof data.classroom_code === "string") || (data.classroom_code == null))
Expand Down Expand Up @@ -560,58 +566,6 @@ app.post("/classes/join", async (req, res) => {
res.json({ success, message });
});

app.post("/educators/create", async (req, res) => {
const data = req.body;
const valid = (
typeof data.first_name === "string" &&
typeof data.last_name === "string" &&
typeof data.password === "string" &&
((typeof data.institution === "string") || (data.institution == null)) &&
typeof data.email === "string" &&
((typeof data.age === "number") || (data.age == null)) &&
((typeof data.gender === "string") || data.gender == null)
);

let result: SignUpResult;
if (valid) {
result = await signUpEducator(data);
} else {
result = SignUpResult.BadRequest;
res.status(400);
}
res.json({
educator_info: data,
status: result,
success: SignUpResult.success(result)
});
});

app.post("/students/create", async (req, res) => {
const data = req.body;
const valid = (
typeof data.username === "string" &&
typeof data.password === "string" &&
((typeof data.institution === "string") || (data.institution == null)) &&
((typeof data.email === "string") || (data.email == null)) &&
((typeof data.age === "number") || (data.age == null)) &&
((typeof data.gender === "string") || (data.gender == null)) &&
((typeof data.classroom_code === "string") || (data.classroom_code == null))
);

let result: SignUpResult;
if (valid) {
result = await signUpStudent(data);
} else {
result = SignUpResult.BadRequest;
res.status(400);
}
res.json({
student_info: data,
status: result,
success: SignUpResult.success(result)
});
});

/* Classes */
app.post("/classes/create", async (req, res) => {
const data = req.body;
Expand Down
Loading