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

#23 Create endpoints 4 #33

Merged
merged 6 commits into from
Jan 14, 2025
Merged
Show file tree
Hide file tree
Changes from 3 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
102 changes: 102 additions & 0 deletions server/routes/user.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
import { Router } from "express";
import { keysToCamel } from "../common/utils";
import { db } from "../db/db-pgp";

export const usersRouter = Router();

// Get a user by email
usersRouter.get("/email/:email", async (req, res) => {
try {
const { email } = req.params;

const user = await db.query(
`
SELECT *
FROM users
WHERE email = $1
`,
[email,]);

// User with email not found
if (!(user.length)) {
return res.status(404).json({ message: 'User not found' });
}

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

} catch (err) {
res.status(500).send(err.message);
}
});

// Get a user by ID
usersRouter.get("/:id", async (req, res) => {
try {
const { id } = req.params;

const user = await db.query("SELECT * FROM users WHERE id = $1", [id,]);

if (!(user.length)) {
return res.status(404).json({ message: 'User not found' });
}

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

} catch (err) {
res.status(500).send(err.message);
}
});

// Create new user, returns new user ID
usersRouter.post("/", async (req, res) => {
try {
const { email, firebaseUid, firstName, lastName, editPerms } = req.body;
const user = await db.query (
`
INSERT INTO users (email, firebase_uid, first_name, last_name, edit_perms)
VALUES ($1, $2, $3, $4, $5)
RETURNING id;
`,
[email, firebaseUid, firstName, lastName, editPerms]);
res.status(200).json(keysToCamel(user[0]));

} catch (err) {
res.status(500).send(err.message);
}
});

// Update a user by ID
usersRouter.put('/:id', async (req, res) => {
try {
const { id } = req.params;
const { email, firstName, lastName, editPerms } = req.body;
const user = await db.query(
`
UPDATE users
SET
email = CASE
WHEN $1 IS NOT NULL THEN $1
ELSE email
END,
first_name = CASE
WHEN $2 IS NOT NULL THEN $2
ELSE first_name
END,
last_name = CASE
WHEN $3 IS NOT NULL THEN $3
ELSE last_name
END,
edit_perms = CASE
WHEN $4 IS NOT NULL THEN $4
ELSE edit_perms
END
WHERE id = $5
RETURNING *;
`,
[email, firstName, lastName, editPerms, id]);
res.status(200).json(keysToCamel(user[0]));

} catch (err) {
res.status(500).send(err.message);
}
});
16 changes: 8 additions & 8 deletions server/routes/users.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ import { admin } from "../config/firebase";
import { db } from "../db/db-pgp"; // TODO: replace this db with
import { verifyRole } from "../src/middleware";

export const usersRouter = Router();
export const userRouter = Router();

// Get all users
usersRouter.get("/", async (req, res) => {
userRouter.get("/", async (req, res) => {
try {
const users = await db.query(`SELECT * FROM users ORDER BY id ASC`);

Expand All @@ -19,7 +19,7 @@ usersRouter.get("/", async (req, res) => {
});

// Get a user by ID
usersRouter.get("/:firebaseUid", async (req, res) => {
userRouter.get("/:firebaseUid", async (req, res) => {
try {
const { firebaseUid } = req.params;

Expand All @@ -34,7 +34,7 @@ usersRouter.get("/:firebaseUid", async (req, res) => {
});

// Delete a user by ID, both in Firebase and NPO DB
usersRouter.delete("/:firebaseUid", async (req, res) => {
userRouter.delete("/:firebaseUid", async (req, res) => {
try {
const { firebaseUid } = req.params;

Expand All @@ -50,7 +50,7 @@ usersRouter.delete("/:firebaseUid", async (req, res) => {
});

// Create user
usersRouter.post("/create", async (req, res) => {
userRouter.post("/create", async (req, res) => {
try {
const { email, firebaseUid } = req.body;

Expand All @@ -66,7 +66,7 @@ usersRouter.post("/create", async (req, res) => {
});

// Update a user by ID
usersRouter.put("/update", async (req, res) => {
userRouter.put("/update", async (req, res) => {
try {
const { email, firebaseUid } = req.body;

Expand All @@ -82,7 +82,7 @@ usersRouter.put("/update", async (req, res) => {
});

// Get all users (as admin)
usersRouter.get("/admin/all", verifyRole("admin"), async (req, res) => {
userRouter.get("/admin/all", verifyRole("admin"), async (req, res) => {
try {
const users = await db.query(`SELECT * FROM users`);

Expand All @@ -93,7 +93,7 @@ usersRouter.get("/admin/all", verifyRole("admin"), async (req, res) => {
});

// Update a user's role
usersRouter.put("/update/set-role", verifyRole("admin"), async (req, res) => {
userRouter.put("/update/set-role", verifyRole("admin"), async (req, res) => {
try {
const { role, firebaseUid } = req.body;

Expand Down
7 changes: 5 additions & 2 deletions server/src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ import express from "express";
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 { userRouter } from "../routes/users";
import { usersRouter } from "../routes/user";
import { verifyToken } from "./middleware";

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

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


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