๐Ÿง‘๐Ÿพโ€๐Ÿ’ป prep

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:

  1. 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.

๐Ÿ’ช๐Ÿพ 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

๐Ÿ“ฎ ๐Ÿงช 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.

๐Ÿ“ฎ Interactive Postman Workspace

Test your movies API

  1. 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.

  2. 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.

  3. Given a user wants to add a movie, when they make a POST request to /movies, then they should receive a success message.

  4. Given a user wants to update a movie, when they make a PUT request to /movies/:movieId, then they should receive a success message.