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

Added endpoints for events table #28

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

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

const eventRouter = Router();

// Return all events
eventRouter.get("/", async (req, res) => {
try {
const events = await db.any(`SELECT * FROM events ORDER BY id ASC`);

if(events.length === 0){
return res.status(404).json({ error: "No events found." });
}

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

// Return event by ID
eventRouter.get("/:id", async (req, res) => {
try {
const { id } = req.params;

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

if(event.length === 0){
return res.status(404).json({ error: "Event does not exist." });
}

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

// Create event, return event ID
eventRouter.post("/", async (req, res) => {
try {
const eventData = req.body;

if (!eventData){
return res.status(404).json({ error: "Event data is required" });
}

const result = await db.query(
"INSERT INTO events (name, description, archived) VALUES ($1, $2, $3) RETURNING id",
[eventData.name, eventData.description || null, eventData.archived ?? false]
);
res.status(201).json({ id: result[0].id });
} catch (err) {
res.status(500).send(err.message);
}
});

// Return all data of updated event
eventRouter.put("/:id", async (req, res) => {
try {
const eventData = req.body;
const { id } = req.params;

if (!eventData){
return res.status(404).json({ error: "Event data is required" });
}

const event = await db.query(
`UPDATE events
SET
name = COALESCE($1, name),
description = COALESCE($2, description),
archived = COALESCE($3, archived)
WHERE id = $4
RETURNING *`,
[eventData.name, eventData.description, eventData.archived, id]
);

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

export {eventRouter};
3 changes: 3 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 { eventRouter } from "../routes/events";

import { verifyToken } from "./middleware";

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

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

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