CRUDing Controllers

Menu
Menu

Adding Create, Read, Update and Delete Endpoints

So far the application allows users to read data, the 'R' of CRUDing (Creating, Reading, Updating and Deleting). We will now extend the project with endpoints to add this functionality. In the context of the films API project, an endpoint refers to a specific URL or URI that represents a unique film or an action related to films. Endpoints define the operations that can be performed on film resources.

Each operation in the films API can be associated with specific endpoints:

  1. Create: This operation allows the creation of a new film resource. The corresponding HTTP method is typically POST, and the endpoint represents the collection or resource where the new film will be added. For example, /api/ can be used to create a new film.

  2. Read: This operation is used to retrieve film resources. The corresponding HTTP method is typically GET, and the endpoint represents the specific film or collection of films to be retrieved. For example, /api/ can retrieve all films, and /api/films/:id can retrieve a specific film by its ID.

  3. Update: This operation is used to modify an existing film resource. The corresponding HTTP method is usually PUT or PATCH, and the endpoint represents the specific film to be updated. For example, /api/:id can be used to update a film with a specific ID.

  4. Delete: This operation is used to remove an existing film resource. The corresponding HTTP method is typically DELETE, and the endpoint represents the specific film to be deleted. For example, /api/:id can delete a film with a specific ID.

In a films API, various endpoints are defined to handle different film-related actions and resources. Each endpoint follows a consistent pattern, where the URL structure reflects the purpose of the action and the specific film or collection of films involved.

For example, in a films API, the following endpoints could be defined:

By mapping CRUD operations to specific endpoints in the films API, clients can interact with the API in a standardized way, allowing them to create, retrieve, update, and delete film resources easily.

Before we proceed with the CRUD functionality, let's briefly explain what REST calls are. REST stands for Representational State Transfer and is an architectural style for designing networked applications. In REST, resources are identified by unique URLs, and different HTTP methods are used to perform operations on those resources. The four main HTTP methods used in REST are:

Adding an Endpoint to Create a new Document

In the controller.js create an an asynchronous function createData that will be responsible for creating a new document in the MongoDB collection.

SNIPPET: controllers/controllers.js

Here's an explanation of the code:

Ensure the new function is exported by amending the export:

SNIPPET: controllers/controllers.js

With the controller set up we now need a router by adding a POST route as follows:

SNIPPET: routes/routes.js

As the data is been set in the http headers through req.body we need to edit index.js by adding:

SNIPPET: index.js

Test the pages by making a POST http call in POSTMAN with raw JSON data as a body payload.

Postman POSTing data
TEST DATA

Adding an Endpoint to Update Data

Next we'll add an asynchronous function updateData that is responsible for updating a document in the MongoDB collection based on its ID. We will need to convert the provided ID into an ObjectId that is the format that MongoDB can work with.

SNIPPET: controllers/controllers.js

Again ensure the new function is exported by amending the export:

SNIPPET: controllers/controllers.js

Add a route in the routes/routes.js to call this new endpoint. This will be a PUT.

SNIPPET: routes/routes.js

Save and the test the page. As we need an ObjectId to update we need to send this as a URL parameter, as we did with the getDataById() example. This should also be made through a PUT call from POSTMAN, with new JSON to update the document.

Postman PUTTing data

Adding the Delete Endpoint

Hopefully, adding the delete functionality will be straight-forward given the previous two examples.

We will add a deleteData to remove a document. Like the update function this will require and objectID to ensure the correct document is removed.

SNIPPET controllers/controllers.js

Again the exports need updating to:

SNIPPET controllers/controllers.js

Update the router file with a delete endpoint:

SNIPPET: routes/routes.js

Again test with Postman but this time creating a DELETE http call with the objectID as a URL parameter of the document you wish to remove.

Notes on Postman

Postman is a popular tool used for testing and interacting with APIs. It provides a user-friendly interface that allows you to send HTTP requests to API endpoints and inspect the responses.

  1. Start your Node application.
  2. Open Postman.
  3. Create a new request and set the request URL to the appropriate endpoint. For example, to test the getAllData function, set the URL to http://localhost:your_port/api/ (replace your_port with the actual port number you're running your application on).
  4. Select the appropriate HTTP method for the request (GET for getAllData, POST for createData, PUT for updateData, DELETE for deleteData).
  5. Set the request body if required. For POST and PUT requests, you can provide the data to be created or updated in the body.
  6. Send the request and observe the response from the API.

Repeat these steps for each endpoint you want to test, using the appropriate HTTP method and providing the required data in the request body when necessary.

Tip: This Repo will have the code from the above.

Next: Mongoose