Improve error messages with id

This commit is contained in:
rui hildt 2020-05-05 11:34:30 +02:00
parent b7b7dbbc73
commit cfedf47568
6 changed files with 62 additions and 5 deletions

View File

@ -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();
}

View File

@ -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 });
}
});

View File

@ -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 });
}
});

View File

@ -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,
});
}
});

View File

@ -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;

View File

@ -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