Skip to content

Commit

Permalink
Merge pull request #32 from ctc-uci/25-create-endpoints-5 #25
Browse files Browse the repository at this point in the history
25 create endpoints 5
  • Loading branch information
theNatePi authored Jan 14, 2025
2 parents 67d0d42 + c52eca6 commit 1154de2
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 10 deletions.
46 changes: 36 additions & 10 deletions server/routes/comments.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,41 @@ import { Router } from "express";
import { keysToCamel } from "../common/utils";
import { db } from "../db/db-pgp"; // TODO: replace this db with

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

commentsRouter.get("/:id", async (req, res) => {
try {
const {id} = req.params;
const data = await db.query("SELECT * FROM comments WHERE id = $1", [id]);

res.status(200).json(keysToCamel(data));
} catch (err) {
res.status(500).send(err.message);
}
});

commentsRouter.get("/invoice/:id", async (req, res) => {
try {
const {id} = req.params;
const data = await db.query("SELECT * FROM comments WHERE invoice_id = $1", [id]);

res.status(200).json(keysToCamel(data));
} catch (err) {
res.status(500).send(err.message);
}
});

commentsRouter.get("/booking/:id", async (req, res) => {
try {
const {id} = req.params;
const data = await db.query("SELECT * FROM comments WHERE booking_id = $1", [id]);

res.status(200).json(keysToCamel(data));
} catch (err) {
res.status(500).send(err.message);
}
});

// Delete comment with ID
commentsRouter.delete("/:id", async (req, res) => {
Expand Down Expand Up @@ -84,12 +118,4 @@ commentsRouter.post("/", async (req, res) => {
}
});

// Get endpoint for testing purpose only
// commentsRouter.get("/", async (req, res) => {
// try {
// const comments = await db.query("SELECT * FROM comments");
// res.status(200).json(keysToCamel(comments));
// } catch (err) {
// res.status(500).send(err.message);
// }
// });
export { commentsRouter };
16 changes: 16 additions & 0 deletions server/routes/users.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,22 @@ usersRouter.delete("/:firebaseUid", async (req, res) => {
}
});

// Delete a user by their account ID (not based on Firebase Uid)
usersRouter.delete("/id/:id", async (req, res) => {
try {
const { id } = req.params;

// first delete all associated comments
await db.query("DELETE FROM comments WHERE user_id = $1", [id]); // theres a foreign key constraint, comments requires a user_id to exist, so must delete all comments associated first
// then delete the user
await db.query("DELETE FROM users WHERE id = $1", [id]);

res.status(200).json({ result: "success" });
} catch (err) {

Check warning on line 63 in server/routes/users.js

View workflow job for this annotation

GitHub Actions / run-checks

'err' is defined but never used. Allowed unused caught errors must match /^_/u
res.status(400).json({ result: "error" });
}
});

// Create user
usersRouter.post("/create", async (req, res) => {
try {
Expand Down
2 changes: 2 additions & 0 deletions server/src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import express from "express";
import schedule from "node-schedule"; // TODO: Keep only if scheduling cronjobs

import { usersRouter } from "../routes/users";
import { commentsRouter } from '../routes/comments';
import { eventsRouter } from "../routes/events";
import { bookingsRouter } from "../routes/bookings";
import { verifyToken } from "./middleware";
Expand Down Expand Up @@ -40,6 +41,7 @@ if (process.env.NODE_ENV === "production") {
}

app.use("/users", usersRouter);
app.use('/comments', commentsRouter);
app.use("/bookings", bookingsRouter);
app.use("/events", eventsRouter);
app.use("/bookings", bookingsRouter);
Expand Down

0 comments on commit 1154de2

Please sign in to comment.