From ad7ca3136a21ec016eeec85f4e6918b369098f9d Mon Sep 17 00:00:00 2001 From: rui hildt Date: Wed, 6 May 2020 12:30:32 +0200 Subject: [PATCH] Complete account endpoints --- api/models/accountModel.js | 30 +++++++++++++++--------------- api/routes/accountRoute.js | 37 +++++++++++++++++++++++-------------- 2 files changed, 38 insertions(+), 29 deletions(-) diff --git a/api/models/accountModel.js b/api/models/accountModel.js index a8b6f50..e05afef 100644 --- a/api/models/accountModel.js +++ b/api/models/accountModel.js @@ -5,7 +5,7 @@ module.exports = { getAccountById, updateAccount, deleteAccount, - // getMeetingsByAccountId, + getMeetingsByAccountId, }; function addAccount(data) { @@ -39,20 +39,20 @@ function deleteAccount(id) { return db('account').where({ id }).del(); } -// function getMeetingsByAccountId(id) { -// // Get all meetings to which an account is participating -// // select id, title, description, start_time, timezone, duration, status where -// return db('participant') -// .where({ id }) -// .select( -// 'id', -// 'username', -// 'email', -// 'timezone', -// 'earliest_time', -// 'latest_time', -// ); -// } +function getMeetingsByAccountId(account_id) { + return db('participant as p') + .select( + 'm.id', + 'm.title', + 'm.description', + 'm.start_time', + 'm.timezone', + 'm.duration', + 'm.status', + ) + .join('meeting as m', { 'm.id': 'p.meeting_id' }) + .where({ account_id }); +} function getAccountById(id) { return db('account') diff --git a/api/routes/accountRoute.js b/api/routes/accountRoute.js index 7211976..3a16395 100644 --- a/api/routes/accountRoute.js +++ b/api/routes/accountRoute.js @@ -22,7 +22,10 @@ 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 with id ${id}.`, error }); + res.status(500).json({ + message: `Failed to update account with id ${id}.`, + error, + }); } }); @@ -31,24 +34,30 @@ router.delete('/:id', async (req, res) => { try { const account = await Account.deleteAccount(id); - res.status(200).json({message: `Account with id ${id} successfully deleted.`}); + res.status(200).json({ + message: `Account with id ${id} successfully deleted.`, + }); } catch (error) { - res.status(500).json({ message: `Failed to delete account with id ${id}.`, error }); + res.status(500).json({ + message: `Failed to delete account with id ${id}.`, + error, + }); } }); +router.get('/:id/meetings', async (req, res) => { + const { id } = req.params; -// // Get list of meetings by id -// router.get('/', async (req, res) => { -// const { id } = req.body; - -// try { -// const meetings = await Account.getMeetingsByAccountId(id); -// res.status(200).json(meetings); -// } catch (error) { -// res.status(500).json({ message: "Couldn't get meetings for account with id ${id}.", error }); -// } -// }); + try { + const meetings = await Account.getMeetingsByAccountId(id); + res.status(200).json(meetings); + } catch (error) { + res.status(500).json({ + message: `Couldn't get meetings for account with id ${id}.`, + error, + }); + } +}); router.get('/:id', async (req, res) => { const id = req.params.id;