generated from ctc-uci/npo-template-merged
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into create-endpoint-1
- Loading branch information
Showing
9 changed files
with
408 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
// } | ||
// }); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
Oops, something went wrong.