Skip to content

Commit

Permalink
Added basic routes for day table, updated app.js
Browse files Browse the repository at this point in the history
  • Loading branch information
h0ethan04 committed Feb 11, 2024
1 parent 2434108 commit 134b1c3
Show file tree
Hide file tree
Showing 2 changed files with 104 additions and 0 deletions.
2 changes: 2 additions & 0 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ const email = require('./routes/nodeMailer');
const app = express();

const catalogRouter = require('./routes/catalog');
const dayRouter = require('./routes/day');

const PORT = process.env.PORT || 3001;

Expand All @@ -33,6 +34,7 @@ app.use('/users', users);
app.use('/catalog', catalogRouter);
app.use('/nodeMailer', email);
app.use('/auth', authRouter);
app.use('/day', dayRouter);

app.listen(PORT, () => {
console.log(`Server listening on ${PORT}`);
Expand Down
102 changes: 102 additions & 0 deletions routes/day.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
const express = require('express');
const { db } = require('../server/db');
const { keysToCamel } = require('../common/utils');

const dayRouter = express.Router();

// GET - returns all days in the table
dayRouter.get('/', async (req, res) => {
try {
const allDays = await db.query(`SELECT * FROM day;`);
res.status(200).json(keysToCamel(allDays));
} catch (err) {
res.status(500).send(err.message);
}
});

// GET - returns a day by eventDate parameter
dayRouter.get('/date', async (req, res) => {
try {
const { eventDate } = req.query;
const dayByDate = await db.query(`SELECT * FROM day WHERE event_date = $1;`, [eventDate]);
res.status(200).json(keysToCamel(dayByDate));
} catch (err) {
res.status(500).send(err.message);
}
});

// GET - returns a day by id
dayRouter.get('/:id', async (req, res) => {
try {
const { id } = req.params;
const dayById = await db.query(`SELECT * FROM day WHERE id = $1;`, [id]);
res.status(200).json(keysToCamel(dayById));
} catch (err) {
res.status(500).send(err.message);
}
});

// POST - creates a new day
dayRouter.post('/', async (req, res) => {
try {
const { eventDate, startTime, endTime, location, notes } = req.body;
const newDay = await db.query(
`
INSERT INTO day (
id,
event_date,
start_time,
end_time,
location,
notes
) VALUES (
nextval('day_id_seq'), $1, $2, $3, $4, $5
) RETURNING id;
`,
[eventDate, startTime, endTime, location, notes],
);
res.status(201).json({
status: 'Success',
id: newDay[0].id,
});
} catch (err) {
res.status(500).send(err.message);
}
});

// PUT - modifies an existing day
dayRouter.put('/:id', async (req, res) => {
try {
const { id } = req.params;
const { eventDate, startTime, endTime, location, notes } = req.body;
const updatedDay = await db.query(
`
UPDATE day
SET
event_date = COALESCE($1, event_date),
start_time = COALESCE($2, start_time),
end_time = COALESCE($3, end_time),
location = COALESCE($4, location),
notes = COALESCE($5, notes)
WHERE id = $6
RETURNING *;
`,
[eventDate, startTime, endTime, location, notes, id],
);
res.status(200).send(keysToCamel(updatedDay));
} catch (err) {
res.status(500).send(err.message);
}
});

// DELETE - deletes an existing day
dayRouter.delete('/:id', async (req, res) => {
try {
const { id } = req.params;
const deletedDay = await db.query(`DELETE FROM day WHERE id = $1 RETURNING *;`, [id]);
res.status(200).send(keysToCamel(deletedDay));
} catch (err) {
res.status(500).send(err.message);
}
});
module.exports = dayRouter;

0 comments on commit 134b1c3

Please sign in to comment.