generated from ctc-uci/npo-backend-template
-
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 'dev' into 44-publish-schedule-group-by-season
- Loading branch information
Showing
6 changed files
with
360 additions
and
40 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
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,103 @@ | ||
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, location, notes } = req.body; | ||
const newDay = await db.query( | ||
` | ||
INSERT INTO day ( | ||
id, | ||
event_date, | ||
start_time, | ||
end_time, | ||
location, | ||
notes, | ||
day_count | ||
) VALUES ( | ||
nextval('day_id_seq'), $1, '23:59:59', '00:00:00', $2, $3, 0 | ||
) RETURNING id; | ||
`, | ||
[eventDate, 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; |
Oops, something went wrong.