82 lines
1.8 KiB
JavaScript
82 lines
1.8 KiB
JavaScript
const express = require('express');
|
|
const router = express.Router();
|
|
const bcrypt = require('bcryptjs');
|
|
|
|
const Account = require('../models/accountModel');
|
|
|
|
router.post('/', async (req, res) => {
|
|
const data = { ...req.body };
|
|
const hash = bcrypt.hashSync(data.password, 14);
|
|
data.password = hash;
|
|
|
|
try {
|
|
const [account] = await Account.addAccount(data);
|
|
res.status(201).json(account);
|
|
} catch (error) {
|
|
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;
|
|
|
|
if (data.password) {
|
|
const hash = bcrypt.hashSync(data.password, 14);
|
|
data.password = hash;
|
|
}
|
|
|
|
try {
|
|
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,
|
|
});
|
|
}
|
|
});
|
|
|
|
router.delete('/:id', async (req, res) => {
|
|
const id = req.params.id;
|
|
|
|
try {
|
|
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 with id ${id}.`,
|
|
error,
|
|
});
|
|
}
|
|
});
|
|
|
|
router.get('/:id/meetings', async (req, res) => {
|
|
const { id } = req.params;
|
|
|
|
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;
|
|
|
|
try {
|
|
const account = await Account.getAccountById(id);
|
|
res.status(200).json(account);
|
|
} catch (error) {
|
|
res.status(500).json({ message: "Account doesn't exist.", error });
|
|
}
|
|
});
|
|
|
|
module.exports = router;
|