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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all 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
85 changes: 85 additions & 0 deletions server/routes/events.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
import { Router } from "express";

import { keysToCamel } from "../common/utils";
import { admin } from "../config/firebase";

Check warning on line 4 in server/routes/events.js

View workflow job for this annotation

GitHub Actions / run-checks

'admin' is defined but never used. Allowed unused vars must match /^_/u
import { db } from "../db/db-pgp";
import { verifyRole } from "../src/middleware";

Check warning on line 6 in server/routes/events.js

View workflow job for this annotation

GitHub Actions / run-checks

'verifyRole' is defined but never used. Allowed unused vars must match /^_/u

export const eventRouter = Router();

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

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

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

// Return event by ID
eventRouter.get("/events/: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("/events", 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, eventData.archived]
);
res.status(200).json(keysToCamel(result));
} catch (err) {
res.status(500).send(err.message);
}
});

// Return all data of updated event
eventRouter.put("/events/: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 = $1, description = $2, archived = $3 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);
}
});



Loading