Skip to content

Commit

Permalink
Merge branch 'main' into create-endpoint-1
Browse files Browse the repository at this point in the history
  • Loading branch information
jessieh9 authored Jan 14, 2025
2 parents 95c40c4 + fce238e commit aee2fff
Show file tree
Hide file tree
Showing 9 changed files with 408 additions and 11 deletions.
6 changes: 6 additions & 0 deletions client/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import {
import { Admin } from "./components/admin/Admin";
import { CatchAll } from "./components/CatchAll";
import { Dashboard } from "./components/dashboard/Dashboard";
//@ts-expect-error - Allow import of JSX page into App.tsx base
import { Playground } from "./components/playground/Playground";
import { Login } from "./components/login/Login";
import { ProtectedRoute } from "./components/ProtectedRoute";
import { Signup } from "./components/signup/Signup";
Expand All @@ -32,6 +34,10 @@ const App = () => {
path="/signup"
element={<Signup />}
/>
<Route
path="/playground"
element={<Playground />}
/>
<Route
path="/dashboard"
element={<ProtectedRoute element={<Dashboard />} />}
Expand Down
15 changes: 15 additions & 0 deletions client/src/components/playground/Playground.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import {
Heading,
VStack,
} from "@chakra-ui/react";

export const Playground = () => {
return (
<VStack
spacing={8}
sx={{ width: 300, marginX: "auto" }}
>
<Heading>Playground</Heading>
</VStack>
);
};
148 changes: 148 additions & 0 deletions server/routes/bookings.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
import express from "express";
import { db } from "../db/db-pgp";
import { keysToCamel } from "../common/utils";

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

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

let query = `SELECT * FROM bookings`;
const params = [];

if (start) {
const [startDate, startTime] = start.split("T");
query += ` WHERE (date > $1 OR (date = $1 AND start_time >= $2))`;
params.push(startDate, startTime);
}

if (end) {
const [endDate, endTime] = end.split("T");
if (params.length === 0) {
query += ` WHERE (date < $1 OR (date = $1 AND end_time <= $2))`;
} else {
query += ` AND (date < $3 OR (date = $3 AND end_time <= $4))`;
}
params.push(endDate, endTime);
}

const data = await db.query(query, params);

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

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 query = `SELECT * FROM bookings WHERE event_id = $1`;
const params = [id];

if (start) {
const [startDate, startTime] = start.split("T");
query += ` AND (date > $2 OR (date = $2 AND start_time >= $3))`;
params.push(startDate, startTime);
}

if (end) {
const [endDate, endTime] = end.split("T");
if (params.length === 1) {
query += ` AND (date < $2 OR (date = $2 AND end_time <= $3))`;
} else {
query += ` AND (date < $4 OR (date = $4 AND end_time <= $5))`;
}
params.push(endDate, endTime);
}

const data = await db.query(query, params);

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

bookingsRouter.post("/", async (req, res) => {
try {
// Get new booking data
const { event_id, room_id, start_time, end_time, date, archived } = req.body;

// Insert new booking into database
const data = await db.query(
`INSERT INTO bookings (event_id, room_id, start_time, end_time, date, archived) VALUES ($1, $2, $3, $4, $5, $6, $7) RETURNING id`,
[ event_id, room_id, start_time, end_time, date, archived ]
);

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

bookingsRouter.put("/:id", async (req, res) => {
try {
const { id } = req.params;

const { event_id, room_id, start_time, end_time, date, archived } = req.body;

// Update booking in database
const data = await db.query(
`
UPDATE bookings
SET
event_id = COALESCE($1, event_id),
room_id = COALESCE($2, room_id),
start_time = COALESCE($3, start_time),
end_time = COALESCE($4, end_time),
date = COALESCE($5, date),
archived = COALESCE($6, archived)
WHERE id = $7
RETURNING *;
`,
[event_id, room_id, start_time, end_time, date, archived, id]
);

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


bookingsRouter.delete("/:id", async (req, res) => {
try {
const { id } = req.params;

// Delete booking from database
const data = db.query("DELETE FROM bookings WHERE id = $1 RETURNING *",
[ id ]);

if (!data) {
return res.status(404).json({result: 'error'});
}

res.status(200).json({result: 'success'});
} catch (err) {
res.status(500).send(err.message);
}
});

export { bookingsRouter };
16 changes: 16 additions & 0 deletions server/routes/clients.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { Router } from "express";
import { keysToCamel } from "../common/utils";
import { db } from "../db/db-pgp"; // TODO: replace this db with

export const clientsRouter = Router();

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

res.status(200).json(keysToCamel(users));
} catch (err) {
res.status(400).send(err.message);
}
});
95 changes: 95 additions & 0 deletions server/routes/comments.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
import { Router } from "express";
import { keysToCamel } from "../common/utils";
import { db } from "../db/db-pgp"; // TODO: replace this db with

export const commentsRouter = Router();

// Delete comment with ID
commentsRouter.delete("/:id", async (req, res) => {
try {
const { id } = req.params;

const comment = await db.query(`DELETE FROM comments WHERE id = $1 RETURNING *`, [
id,
]);

if (comment.length === 0) {
return res.status(404).json({result: 'error'});
}

res.status(200).json({result: 'success', deletedComment: comment});
} catch (err) {
res.status(500).json({result: 'error', message: err.message});
}
});

// Update a comment by ID
commentsRouter.put("/:id", async (req, res) => {
try {
const { id } = req.params;
const { user_id, booking_id, invoice_id, datetime, comment, adjustment_type, adjustment_value } = req.body;

const fields = [];
if (user_id) fields.push(`user_id = $(user_id)`);
if (booking_id) fields.push(`booking_id = $(booking_id)`);
if (invoice_id) fields.push(`invoice_id = $(invoice_id)`);
if (datetime) fields.push(`datetime = $(datetime)`);
if (comment) fields.push(`comment = $(comment)`);
if (adjustment_type) fields.push(`adjustment_type = $(adjustment_type)`);
if (adjustment_value) fields.push(`adjustment_value = $(adjustment_value)`);

// Join the fields to form the SET clause
const setClause = fields.join(", ");

// If no fields are set, return a 400 error
if (!setClause) {
return res.status(400).send("No fields provided to update" );
}

const data = await db.query(
`UPDATE comments
SET ` + setClause + `
WHERE id = $(id)
RETURNING *`,
{
id,
user_id, booking_id, invoice_id, datetime, comment, adjustment_type, adjustment_value
});

// Verify the comment is valid
if (data.length === 0) {
return res.status(404).send("Comment not found");
}

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

// Create comment
commentsRouter.post("/", async (req, res) => {
try {
const { user_id, booking_id, invoice_id, datetime, comment, adjustment_type, adjustment_value } = req.body;

// Insertnew
const inserted_row = await db.query(
`INSERT INTO comments (user_id, booking_id, invoice_id, datetime, comment, adjustment_type, adjustment_value) VALUES ($1, $2, $3, $4, $5, $6, $7) RETURNING id;`,
[user_id, booking_id ?? null, invoice_id, datetime, comment, adjustment_type, adjustment_value]
);

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

// 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);
// }
// });
28 changes: 22 additions & 6 deletions server/routes/events.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ import { Router } from "express";
import { keysToCamel } from "../common/utils";
import { db } from "../db/db-pgp";

const eventRouter = Router();
const eventsRouter = Router();

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

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

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

Expand All @@ -40,7 +40,7 @@ eventRouter.get("/:id", async (req, res) => {
});

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

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

// Return all data of updated event
eventRouter.put("/:id", async (req, res) => {
eventsRouter.put("/:id", async (req, res) => {
try {
const eventData = req.body;
const { id } = req.params;
Expand All @@ -85,4 +85,20 @@ eventRouter.put("/:id", async (req, res) => {
}
});

export {eventRouter};

eventsRouter.delete("/:id", async (req, res) => {
try {
const { id } = req.params;
const data = await db.query(`DELETE FROM events WHERE id = $1 RETURNING *`, [id]);

if(data.length > 0)
res.status(200).json({"result" : "success", "deletedData" : keysToCamel(data)});
else
res.status(404).json({"result" : "error"});
} catch (err) {
res.status(500).send(err.message);
}
});


export { eventsRouter };
File renamed without changes.
Loading

0 comments on commit aee2fff

Please sign in to comment.