Updating teams

To update our teams, first we will need to look for an existing team in our database using the ID provided in the path. If a team is found, we apply the changes to the team object. So, let's start adding the following code:

api
.route('/teams/:id')
.get((req, res, next) => {
...
})
.put((req, res, next) => {
let id = req.params.id

Team.findById(id)
.then(data => {
if (data == null) {
throw new Error("Team not found")
}
return data
})
.then(team => {
// We found the team.
// Code to update goes here!
})
.catch(err => next(err))

})
.delete((req, res) => {
    // TODO
})
...

First, we extract the id passed in the endpoint. Then, we call the findById method to look for an existing team. If a valid team is found, we will have a team object as a parameter. Otherwise, an error will be thrown.

As you can see, there are more lines of code to add to our logic. Now it's time to extract the values from the req.body object and modify the team found:

...
.put((req, res, next) => {
let id = req.params.id
let teamBody = req.body

Team.findById(id)
.then(data => {
if (data == null) {
throw new Error("Team not found")
}
return data
})
.then(team => {
team.code = teamBody.code || team.code
team.name = teamBody.name || team.name
team.ranking = teamBody.ranking || team.ranking
team.captain = teamBody.captain || team.captain
team.trainer = teamBody.trainer || team.trainer
team.confederation = teamBody.confederation || team.confederation

})
.catch(err => {
next(err)
})

})
...

We have created a teamBody variable to host the req.body data. Then, we change the values into the team object. We are using the || operator; this operator will assign teamBody.code if this value is sent in the body object. Otherwise, it will assign the same value to the team object. With this, we are able to change the values only if they are sent.

Now, to save it to the database, we will call the save method as we did when we coded the logic to create a new Team:

... 
.put((req, res, next) => {
let id = req.params.id
let teamBody = req.body

Team.findById(id)
.then(data => {
if (data == null) {
throw new Error("Team not found")
}
return data
})
.then(team => {
team.code = teamBody.code || team.code
team.name = teamBody.name || team.name
team.ranking = teamBody.ranking || team.ranking
team.captain = teamBody.captain || team.captain
team.trainer = teamBody.trainer || team.trainer
team.confederation = teamBody.confederation || team.confederation

return team.save()
})
.then(result => res.json(result))
.catch(err => next(err))

})
...

Cool! Now we have implemented the logic to update our Team entities. Let's test things out. Execute the following command:

$ curl -X PUT -H 'Content-type: application/json' -d '{"ranking": 1}' http://localhost:3000/teams/5a662fbf728726072c6298fc

{"_id":"5a662fbf728726072c6298fc","name":"Peru","ranking":1,"captain":"Paolo Guerreo","Trainer":"Ricardo Gareca","confederation":"CONMEBOL","__v":0}

Excellent! Now we are able to update Team entities. However, have you noticed that our code is a little difficult to organize? We are using multiple Promise entities to find and save a product. What will happen if we need to perform more asynchronous operations? Sooner or later, we will end up having code that contains a lot of then instructions that might be difficult to maintain and understand. However, don't worry! The async/await comes to save the day! Keep reading.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset