From cfedf475683047c85a88f2717b5846aa09dbef40 Mon Sep 17 00:00:00 2001 From: rui hildt Date: Tue, 5 May 2020 11:34:30 +0200 Subject: [PATCH] Improve error messages with id --- api/models/possibleDateModel.js | 16 +++++++++++++++ api/routes/accountRoute.js | 4 ++-- api/routes/meetingRoute.js | 4 ++-- api/routes/participantRoute.js | 5 ++++- api/routes/possibleDateRoute.js | 36 +++++++++++++++++++++++++++++++++ api/server.js | 2 ++ 6 files changed, 62 insertions(+), 5 deletions(-) create mode 100644 api/models/possibleDateModel.js create mode 100644 api/routes/possibleDateRoute.js diff --git a/api/models/possibleDateModel.js b/api/models/possibleDateModel.js new file mode 100644 index 0000000..a489d7c --- /dev/null +++ b/api/models/possibleDateModel.js @@ -0,0 +1,16 @@ +const db = require('../../data/db'); + +module.exports = { + addPossibleDate, + deletePossibleDate, +}; + +function addPossibleDate(data) { + return db('possible_date') + .insert(data) + .returning(['id', 'meeting_id', 'possible_date']); +} + +function deletePossibleDate(id) { + return db('possible_date').where({ id }).del(); +} diff --git a/api/routes/accountRoute.js b/api/routes/accountRoute.js index a2385fd..7211976 100644 --- a/api/routes/accountRoute.js +++ b/api/routes/accountRoute.js @@ -22,7 +22,7 @@ router.put('/:id', async (req, res) => { const account = await Account.updateAccount(data, id); res.status(200).json(...account); } catch (error) { - res.status(500).json({ message: 'Failed to update account.', error }); + res.status(500).json({ message: `Failed to update account with id ${id}.`, error }); } }); @@ -33,7 +33,7 @@ router.delete('/:id', async (req, res) => { const account = await Account.deleteAccount(id); res.status(200).json({message: `Account with id ${id} successfully deleted.`}); } catch (error) { - res.status(500).json({ message: 'Failed to delete account.', error }); + res.status(500).json({ message: `Failed to delete account with id ${id}.`, error }); } }); diff --git a/api/routes/meetingRoute.js b/api/routes/meetingRoute.js index f13a6e0..6baf50c 100644 --- a/api/routes/meetingRoute.js +++ b/api/routes/meetingRoute.js @@ -25,7 +25,7 @@ router.put('/:id', async (req, res) => { const meeting = await Meeting.updateMeeting(data, id); res.status(200).json(meeting); } catch (error) { - res.status(500).json({ message: 'Failed to update meeting.', error }); + res.status(500).json({ message: `Failed to update meeting with id ${id}.`, error }); } }); @@ -36,7 +36,7 @@ router.delete('/:id', async (req, res) => { const meeting = await Meeting.deleteMeeting(id); res.status(200).json({message: `Meeting with id ${id} successfully deleted.`}); } catch (error) { - res.status(500).json({ message: 'Failed to delete meeting.', error }); + res.status(500).json({ message: `Failed to delete meeting with id ${id}.`, error }); } }); diff --git a/api/routes/participantRoute.js b/api/routes/participantRoute.js index 69d5b50..4e8922b 100644 --- a/api/routes/participantRoute.js +++ b/api/routes/participantRoute.js @@ -65,7 +65,10 @@ router.get('/:account_id-:meeting_id', async (req, res) => { ); res.status(200).json(participant); } catch (error) { - res.status(500).json({ message: "Participant doesn't exist.", error }); + res.status(500).json({ + message: `Participant with id ${account_id}-${meeting_id} doesn't exist.`, + error, + }); } }); diff --git a/api/routes/possibleDateRoute.js b/api/routes/possibleDateRoute.js new file mode 100644 index 0000000..229916d --- /dev/null +++ b/api/routes/possibleDateRoute.js @@ -0,0 +1,36 @@ +let express = require('express'); +let router = express.Router(); + +let PossibleDate = require('../models/possibleDateModel'); + +router.post('/', async (req, res) => { + const data = { ...req.body }; + + try { + const [possibleDate] = await PossibleDate.addPossibleDate(data); + res.status(201).json(possibleDate); + } catch (error) { + res.status(500).json({ + message: 'Failed to add a new possible date.', + error, + }); + } +}); + +router.delete('/:possible_date_id', async (req, res) => { + const id = req.params.id; + + try { + const possibleDate = await PossibleDate.deletePossibleDate(id); + res.status(200).json({ + message: `Possible date with id ${id} successfully deleted.`, + }); + } catch (error) { + res.status(500).json({ + message: `Failed to delete possible date with id ${id}.`, + error, + }); + } +}); + +module.exports = router; diff --git a/api/server.js b/api/server.js index 4d850b3..800c981 100644 --- a/api/server.js +++ b/api/server.js @@ -6,6 +6,7 @@ require('dotenv').config(); const accountsRoute = require('./routes/accountRoute'); const meetingsRoute = require('./routes/meetingRoute'); const participantsRoute = require('./routes/participantRoute'); +const possibleDatesRoute = require('./routes/possibleDateRoute'); const server = express(); @@ -16,6 +17,7 @@ server.use(helmet()); server.use('/api/accounts', accountsRoute); server.use('/api/meetings', meetingsRoute); server.use('/api/participants', participantsRoute); +server.use('/api/possible-dates', possibleDatesRoute); server.get('/', (req, res) => res