Complete crud for accounts and meetings endpoints
This commit is contained in:
@@ -3,15 +3,61 @@ let router = express.Router();
|
||||
|
||||
let Account = require('../models/accountModel');
|
||||
|
||||
// Add a user
|
||||
router.post('/', async (req, res) => {
|
||||
const userData = { ...req.body };
|
||||
const data = { ...req.body };
|
||||
|
||||
try {
|
||||
const user = await Account.addUser(userData);
|
||||
res.status(201).json(user);
|
||||
const [account] = await Account.addAccount(data);
|
||||
res.status(201).json(account);
|
||||
} catch (error) {
|
||||
res.status(500).json({ message: 'Failed to add new user', 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;
|
||||
|
||||
try {
|
||||
const account = await Account.updateAccount(data, id);
|
||||
res.status(200).json(...account);
|
||||
} catch (error) {
|
||||
res.status(500).json({ message: 'Failed to update account.', 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.', error });
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
// // 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 });
|
||||
// }
|
||||
// });
|
||||
|
||||
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 });
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
54
api/routes/meetingRoute.js
Normal file
54
api/routes/meetingRoute.js
Normal file
@@ -0,0 +1,54 @@
|
||||
let express = require('express');
|
||||
let router = express.Router();
|
||||
const { v4: uuidv4 } = require('uuid');
|
||||
|
||||
let Meeting = require('../models/meetingModel');
|
||||
|
||||
router.post('/', async (req, res) => {
|
||||
id = uuidv4();
|
||||
data = {id, ...req.body}
|
||||
console.log(data);
|
||||
|
||||
try {
|
||||
const [meeting] = await Meeting.addMeeting(data);
|
||||
res.status(201).json(meeting);
|
||||
} catch (error) {
|
||||
res.status(500).json({ message: 'Failed to add meeting.', error });
|
||||
}
|
||||
});
|
||||
|
||||
router.put('/:id', async (req, res) => {
|
||||
const data = { ...req.body };
|
||||
const id = req.params.id;
|
||||
|
||||
try {
|
||||
const meeting = await Meeting.updateMeeting(data, id);
|
||||
res.status(200).json(meeting);
|
||||
} catch (error) {
|
||||
res.status(500).json({ message: 'Failed to update meeting.', error });
|
||||
}
|
||||
});
|
||||
|
||||
router.delete('/:id', async (req, res) => {
|
||||
const id = req.params.id;
|
||||
|
||||
try {
|
||||
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 });
|
||||
}
|
||||
});
|
||||
|
||||
router.get('/', async (req, res) => {
|
||||
const id = req.params.id;
|
||||
|
||||
try {
|
||||
const meeting = await Meeting.getMeetingById(id);
|
||||
res.status(200).json(meeting);
|
||||
} catch (error) {
|
||||
res.status(500).json({ message: "Meeting doesn't exist.", error });
|
||||
}
|
||||
});
|
||||
|
||||
module.exports = router;
|
||||
Reference in New Issue
Block a user