Skip to content

Commit

Permalink
Created events and bookings endpoints bc we are goated
Browse files Browse the repository at this point in the history
Co-authored-by: Aston Chan <[email protected]>
  • Loading branch information
brelieu05 and AstonKiChan committed Jan 2, 2025
1 parent 293e922 commit 4df361a
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 0 deletions.
45 changes: 45 additions & 0 deletions server/routes/bookings.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import express from "express";

import { db } from "../db/db-pgp";
import { keysToCamel } from "../common/utils";

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

bookingsRouter.get("/:id", async (req, res) => {
try {
const { id } = req.params;
const data = await db.query(`SELECT * FROM bookings WHERE id = $1`, [
id
]);
res.status(200).json(keysToCamel(data));
} catch (err) {
res.status(500).send(err.message);
}
});

bookingsRouter.get("/event/:id", async (req, res) => {
try {
const { id } = req.params;
const { start, end } = req.query;

let data;
if (start && end)
{
data = await db.query(`SELECT * FROM bookings WHERE event_id = $1 AND start_time = $2 AND end_time = $3`, [
id,
start,
end
]);
}
else if (!(start && end))
{
data = await db.query(`SELECT * FROM bookings WHERE event_id = $1`, [
id
]);
}
res.status(200).json(keysToCamel(data));
} catch (err) {
res.status(500).send(err.message);
}
});
26 changes: 26 additions & 0 deletions server/routes/events.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import express from "express";
import { db } from "../db/db-pgp";
import { keysToCamel } from "../common/utils";

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

eventsRouter.get("/", async (req, res) => {
try {
const data = await db.query(`SELECT * FROM events`);
res.status(200).json(keysToCamel(data));
} catch (err) {
res.status(500).send(err.message);
}
});

eventsRouter.delete("/:id", async (req, res) => {
try {
const { id } = req.params;
const data = await db.query(`DELETE FROM events WHERE id = $1`, [id]);
res.status(200).json(keysToCamel(data));
} catch (err) {
res.status(500).send(err.message);
}
});

4 changes: 4 additions & 0 deletions server/src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ 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 { eventsRouter } from "../routes/events";
import { bookingsRouter } from "../routes/bookings";
import { verifyToken } from "./middleware";

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

app.use("/", sampleRouter); // TODO: delete sample endpoint
app.use("/users", usersRouter);
app.use("/events", eventsRouter);
app.use("/bookings", bookingsRouter);

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

0 comments on commit 4df361a

Please sign in to comment.