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

28 create teachers crud routes #37

Merged
merged 20 commits into from
Jan 10, 2025
Merged
Show file tree
Hide file tree
Changes from 14 commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
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
171 changes: 171 additions & 0 deletions server/routes/teachers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,171 @@
import express from 'express';
import { keysToCamel } from '../common/utils';
import { db } from "../db/db-pgp"; // TODO: replace this db with

export const teachersRouter = express.Router();
teachersRouter.use(express.json());

// Postman Screenshot: https://img001.prntscr.com/file/img001/r7f400d1Q_K4fpLr42zpxg.png
teachersRouter.get("/:id", async(req, res) => {
try {
const teacherId = req.params.id

const teacher = await db.query(
"SELECT * FROM Teachers INNER JOIN Users ON Users.id = Teachers.id WHERE Teachers.id = $1",
[teacherId]
)

res.status(200).json(keysToCamel(teacher[0]));
} catch (err) {
console.log(err)
res.status(500).json({
status: "Failed",
msg: err.message,
});
}
});

// Postman Screenshot: https://img001.prntscr.com/file/img001/KA0s8VN8RrGbU67hH6rznA.png
teachersRouter.get("/", async(req, res) => {
try {
const teacher = await db.query(
"SELECT * FROM Teachers INNER JOIN Users ON Users.id = Teachers.id"
)

res.status(200).json(keysToCamel(teacher));
} catch (err) {
console.log(err)
res.status(500).json({
status: "Failed",
msg: err.message,
});
}
});

// Postman Screenshot: https://img001.prntscr.com/file/img001/KS97e25SRRSSCmbls-aKIg.png
teachersRouter.get("/classes/:id", async(req, res) => {
try {
const teacherId = req.params.id

const classes = await db.query(
"SELECT classes.id, classes.title FROM classes INNER JOIN classes_taught ON classes_taught.class_id = classes.id WHERE classes_taught.teacher_id = $1",
[teacherId]
)

res.status(200).json(keysToCamel(classes))

} catch(err) {
console.log(err)
res.status(500).json({
status: "Failed",
msg: err.message,
});
}
})

// Postman Screenshot: https://img001.prntscr.com/file/img001/aaW5w8onRLmmqpuvSwdTWQ.png
teachersRouter.post("", async(req, res) => {
stevem-zhou marked this conversation as resolved.
Show resolved Hide resolved
try {
const { firstName, lastName, role, userRole, email, experience, firebaseUid } = req.body;

await db.query(
"INSERT INTO Users (first_name, last_name, role, user_role, email, firebase_uid) VALUES ($1, $2, $3, $4, $5, $6)",
[firstName, lastName, role, userRole, email, firebaseUid]
)

const user = await db.query(
"SELECT * FROM Users WHERE email = $1",
[email]
)

const teacherId = keysToCamel(user)[0].id;

await db.query(
"INSERT INTO Teachers (id, experience, is_activated) OVERRIDING SYSTEM VALUE VALUES ($1, $2, false)",
[teacherId, experience]
)

res.redirect(`/teachers/${teacherId}`)
boyuan12 marked this conversation as resolved.
Show resolved Hide resolved

} catch (err) {
console.log(err)
res.status(500).json({
status: "Failed",
msg: err.message,
});
}
});

// Postman Screenshot: https://img001.prntscr.com/file/img001/GtiUoMMlQz-6eT1Qfq1K9Q.png
teachersRouter.put("/:id", async(req, res) => {
try {
const { experience, isActivated } = req.body;
const teacherId = req.params.id

let query = "UPDATE Teachers SET"
let params = []

if (experience != undefined) {
query += " experience = $1,"
params.push(experience)
} else {
params.push("")
}

if (isActivated != undefined) {
query += " is_activated = $2"
params.push(isActivated)
} else {
params.push("")
}

if (query.charAt(query.length-1) == ",") {
query = query.slice(0, query.length-1)
}

params.push(parseInt(teacherId))
query += " WHERE id = $3 RETURNING *"

console.log(query)
console.log(params)
boyuan12 marked this conversation as resolved.
Show resolved Hide resolved

const updatedTeacher = await db.query(
query,
[experience, isActivated, teacherId]
)

res.status(200).json(keysToCamel(updatedTeacher)[0])
} catch (err) {
console.log(err)
res.status(500).json({
status: "Failed",
msg: err.message,
});
}
})

// Postman Screenshot: https://img001.prntscr.com/file/img001/tJLH9FmfS1i7NSh8OM0OHA.png
teachersRouter.delete("/:id", async(req, res) => {
try {
const teacherId = req.params.id

const teacher = await db.query(
"SELECT * FROM Teachers INNER JOIN Users ON Users.id = Teachers.id WHERE Teachers.id = $1",
[teacherId]
)

await db.query(
"DELETE FROM Teachers WHERE id = $1",
[teacherId]
)
boyuan12 marked this conversation as resolved.
Show resolved Hide resolved

res.status(200).json(keysToCamel(teacher[0]));

} catch (err) {
console.log(err)
res.status(500).json({
status: "Failed",
msg: err.message,
});
}
})
2 changes: 2 additions & 0 deletions server/src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import schedule from "node-schedule"; // TODO: Keep only if scheduling cronjobs

import { sampleRouter } from "../routes/sample"; // TODO: delete sample router
import { usersRouter } from "../routes/users";
import { teachersRouter } from "../routes/teachers"
import { verifyToken } from "./middleware";

dotenv.config();
Expand Down Expand Up @@ -38,6 +39,7 @@ if (process.env.NODE_ENV === "production") {

app.use("/", sampleRouter); // TODO: delete sample endpoint
app.use("/users", usersRouter);
app.use("/teachers", teachersRouter)

app.listen(SERVER_PORT, () => {
console.info(`Server listening on ${SERVER_PORT}`);
Expand Down