stephencweiss / matilda

See your time in a whole new way. Small, lightweight, magical.
MIT License
1 stars 0 forks source link

Need a basic server #1

Closed stephencweiss closed 5 years ago

stephencweiss commented 5 years ago
stephencweiss commented 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));
});

image

stephencweiss commented 5 years ago

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))
})

image

stephencweiss commented 5 years ago

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));
});

image

Needed to add the bodyParser to be able to use the request.body values

stephencweiss commented 5 years ago

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))
})

image

stephencweiss commented 5 years ago

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))
})

image

stephencweiss commented 5 years ago

Addressed with the PR #4