2020-05-01 15:32:46 +00:00
|
|
|
let express = require('express');
|
|
|
|
let router = express.Router();
|
|
|
|
|
|
|
|
let Account = require('../models/accountModel');
|
|
|
|
|
|
|
|
router.post('/', async (req, res) => {
|
2020-05-04 14:02:31 +00:00
|
|
|
const data = { ...req.body };
|
2020-05-01 15:32:46 +00:00
|
|
|
|
|
|
|
try {
|
2020-05-04 14:02:31 +00:00
|
|
|
const [account] = await Account.addAccount(data);
|
|
|
|
res.status(201).json(account);
|
2020-05-01 16:24:59 +00:00
|
|
|
} catch (error) {
|
2020-05-04 14:02:31 +00:00
|
|
|
res.status(500).json({ message: 'Failed to add new account.', error });
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
router.put('/:id', async (req, res) => {
|
|
|
|
const data = { ...req.body };
|
|
|
|
const id = req.params.id;
|
|
|
|
|
|
|
|
try {
|
|
|
|
const account = await Account.updateAccount(data, id);
|
|
|
|
res.status(200).json(...account);
|
|
|
|
} catch (error) {
|
2020-05-06 10:30:32 +00:00
|
|
|
res.status(500).json({
|
|
|
|
message: `Failed to update account with id ${id}.`,
|
|
|
|
error,
|
|
|
|
});
|
2020-05-04 14:02:31 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
router.delete('/:id', async (req, res) => {
|
|
|
|
const id = req.params.id;
|
|
|
|
|
|
|
|
try {
|
|
|
|
const account = await Account.deleteAccount(id);
|
2020-05-06 10:30:32 +00:00
|
|
|
res.status(200).json({
|
|
|
|
message: `Account with id ${id} successfully deleted.`,
|
|
|
|
});
|
2020-05-04 14:02:31 +00:00
|
|
|
} catch (error) {
|
2020-05-06 10:30:32 +00:00
|
|
|
res.status(500).json({
|
|
|
|
message: `Failed to delete account with id ${id}.`,
|
|
|
|
error,
|
|
|
|
});
|
2020-05-04 14:02:31 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2020-05-06 10:30:32 +00:00
|
|
|
router.get('/:id/meetings', async (req, res) => {
|
|
|
|
const { id } = req.params;
|
2020-05-04 14:02:31 +00:00
|
|
|
|
2020-05-06 10:30:32 +00:00
|
|
|
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,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
2020-05-04 14:02:31 +00:00
|
|
|
|
|
|
|
router.get('/:id', async (req, res) => {
|
|
|
|
const id = req.params.id;
|
|
|
|
|
|
|
|
try {
|
|
|
|
const account = await Account.getAccountById(id);
|
|
|
|
res.status(200).json(account);
|
|
|
|
} catch (error) {
|
|
|
|
res.status(500).json({ message: "Account doesn't exist.", error });
|
2020-05-01 15:32:46 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
module.exports = router;
|