Implementing Update and Delete Handlers in NestJS
Introduction
Welcome to the lesson on implementing the Update and Delete handlers in a Todo REST API with NestJS. In this lesson, we'll build upon what we've learned so far to complete the CRUD functionality in our application.
Previously, you set up GET queries to fetch all Todo items or a specific item by its ID and implemented the POST handler to create new Todo items. Now, we'll focus on how to update and delete Todo items using PUT and DELETE requests, respectively.
Understanding PUT and DELETE Verbs in REST
In RESTful web services, HTTP methods (also known as verbs) play a crucial role in defining the actions that can be performed on resources. We've learned about GET and POST. Two key methods we will focus on in this lesson are PUT and DELETE.
- PUT: The
PUTmethod is used to update the state of a resource. If the resource does not exist,PUTcan also create a new resource with the specified data, although this behavior can vary depending on implementation. In our implementation, we will not create a record viaPUTif it does not already exist. - DELETE: The
DELETEmethod is used to remove a resource. After a successfulDELETErequest, the resource should no longer exist on the server.
Adding PUT and DELETE to the TodoController
In NestJS, controllers handle incoming HTTP requests and send responses to the client. They typically delegate the business logic to services. For this lesson, we'll extend our TodoController to include methods for updating and deleting Todo items.
Here's the basic structure of our updated TodoController:
