|
|
|
# como fazer a função update:
|
|
|
|
|
|
|
|
```javascript
|
|
|
|
exports.update = function(req, res) {
|
|
|
|
|
|
|
|
// Validate input, in our case we will recive a json if
|
|
|
|
// all changeable informations, in activity's
|
|
|
|
// perspective we to recive the ID of activity and the
|
|
|
|
// description, so, we check if its not blank
|
|
|
|
if(!req.params.activityId) {
|
|
|
|
return res.status(400).send({
|
|
|
|
message: "Acitivity's ID cannot be empty"
|
|
|
|
});
|
|
|
|
}
|
|
|
|
if(!req.body.description) {
|
|
|
|
return res.status(400).send({
|
|
|
|
message: "Activity's description cannot be empty"
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
// Normally on our project we are using await/async
|
|
|
|
// but in that case is pretty simple to use the standart promise
|
|
|
|
// so, without await/async but with just then at the and of a function
|
|
|
|
AcitivityModel.update(
|
|
|
|
{ description: req.body.description },
|
|
|
|
{ where: { id: req.params.activityId } }
|
|
|
|
).then(function(updateStatus) {
|
|
|
|
// First of all, what happens above:
|
|
|
|
// update is a function that recives two parameters and returns a update status
|
|
|
|
// the first of those two parameters is: a map of column: newValue that should be updated
|
|
|
|
// the second one is: a map with your where clause
|
|
|
|
// its update status return is a array if just one position, where 0 extends for failure
|
|
|
|
// and 1 for success
|
|
|
|
|
|
|
|
// Now, about the promises, when we finish a function we can add a .then at its end
|
|
|
|
// making the "then" function just happen after our main function, we also can take
|
|
|
|
// whatever the main function send back to the caller, so, we have a new function
|
|
|
|
// reciving the 'updateStatus' that was the result from the update
|
|
|
|
|
|
|
|
if(updateStatus[0] === 0) {
|
|
|
|
// 0 means failure, so throw a error
|
|
|
|
return res.status(404).send({
|
|
|
|
message: "Acitivity not found with ID " + req.params.activityId + "."
|
|
|
|
});
|
|
|
|
} else if(updateStatus[0] === 1) {
|
|
|
|
// 1 means success, so throw a success message
|
|
|
|
return res.status(200).send({
|
|
|
|
message: "Acitivity with ID equals to " + req.params.activityId + " updated successfully."
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
// Just to be sure that non special situation might pass
|
|
|
|
return res.status(500).send({
|
|
|
|
message: "Error updating activity with ID " + req.params.activityId + "."
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}).catch(function(err) {
|
|
|
|
// Checking anykind of exceptions
|
|
|
|
if(err.kind === 'ObjectId') {
|
|
|
|
return res.status(404).send({
|
|
|
|
message: "Acitivity not found with ID " + req.params.activityId + "."
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return res.status(500).send({
|
|
|
|
message: "Error updating activity with ID " + req.params.activityId + "."
|
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|
|
|
|
``` |
|
|
|
\ No newline at end of file |