Closed stephencweiss closed 5 years ago
Basic GET is working.
app.get('/budget-item', (request, response) => {
db.BudgetItem.findAll({})
.then(message => response.status(200).send(message))
.catch(err => response.status(404).send(errorMessage, err));
});
Specific User's GET
app.get('/myBudget/:budgetId', (request, response) => {
db.BudgetItem.findAll({ where: { budget_id: request.params.budgetId } })
.then(message => response.status(200).send(message))
.catch(err => response.status(404).send(errorMessage, err))
})
POST a budgetItem
app.use(bodyParser.json());
app.post('/newBudgetItem', (request, response) => {
// Assume that the budgetId is known and part of the request (body?)
// the budgetId is defined earlier in the process because that's the page we're on.
db.BudgetItem.create({
category: request.body.category,
hours_allocated: request.body.hoursAllocated,
budget_id: request.body.budgetId
})
.then(result => response.status(201).send(result))
.catch(err => response.status(500).send(errorMessage, err));
});
Needed to add the bodyParser to be able to use the request.body
values
PUT a budgetItem
app.put('/updateBudgetItem/:budgetItemId', (request, response) => {
db.BudgetItem.update({
category: request.body.category,
hours_allocated: request.body.hoursAllocated
},
{ where:
{ budget_item_id: request.params.budgetItemId }
})
.then(message => response.status(200).send(message))
.catch(err => response.status(404).send(errorMessage, err))
})
DELETE a budgetItem
app.delete('/deleteBudgetItem/:budgetItemId', (request, response) => {
console.log(chalk.green(`Budget Id to delete -->`),request.params.budgetItemId)
db.BudgetItem.destroy({
where: { budget_item_id: request.params.budgetItemId },
limit: 1
})
.then(() => response.sendStatus(204))
.catch(err => response.status(404).send(errorMessage, err))
})
Addressed with the PR #4