CRUD Again
Learning Objectives
We are building a CRUD API. CRUD stands for Create, Retrieve, Update, Delete. If you think about it, this is what most applications do:
โจ Create some “resources” ๐ Retrieve them (GET them) ๐จ Update them ๐๏ธ Delete them
๐ฏ Goal
Our API will manage movie data. It will:
โจ Create a new movie, ๐ Retrieve a list of movies or a single movie ๐จ Update a movie
We will build this endpoint:
PUT /movies/:movieId should update a movie (that matches the passed movieId)
๐ You should already have a project using the Node-Starter-Kit. If not, do last week’s prep before you go on.
๐จ PUT
Learning Objectives
PUT /movies/:movieId should update a movie (that matches the passed movieId)
This means that PUT /movies/2
should update an movie with the id 2
and return 200
with JSON { success: true }
to the user.
The code should look something like this:
app.put("/movies/:movieID", (req, res) => {
console.log("PUT /movies route");
});
Remember, you have got to update the movie, not add it to the list.
Test that your API works by updating one of the movies.
PUT Codealong with Mitch ๐
๐ช๐พ CRUD Challenges
CHALLENGE 1:
Return the old version of the object you updated as well as the new value in the response
CHALLENGE 2:
Validate the request body to make sure the ID can’t be updated and that users can’t add additional fields
CHALLENGE 3:
Persist your changes to file so that you are able to return your updated values even after you restart the server
Reading from the filesystem ๐
Writing tests in Postman ๐
๐ฎ ๐งช Test Examples in Postman
Learning Objectives
You will need to create an account with Postman to complete this task. Follow along with the video Writing Tests in Postman with Examples.
Test your movies API
Given a user wants to see all the movies, when they make a GET request to
/movies
, then they should receive a list of all the movies.Given a user wants to see a single movie, when they make a GET request to
/movies/:movieId
, then they should receive a single movie.Given a user wants to add a movie, when they make a POST request to
/movies
, then they should receive a success message.Given a user wants to update a movie, when they make a PUT request to
/movies/:movieId
, then they should receive a success message.