Welcome back! So far, you've learned how to create and view ToDo items. Now, it's time to add more interactivity by allowing users to update and delete ToDo items.
These functionalities will make your application more dynamic and provide users with full control over their list of tasks. By the end of this lesson, your ToDo app will support all core CRUD operations.
In this lesson, you’ll learn how to:
- Update existing ToDo items.
- Delete ToDo items from the list.
These essential features will make your ToDo application more interactive and practical for users.
The first step is to extend the TodoController to handle update and delete requests. We'll use the PUT and DELETE HTTP methods and keep all the routes under the /todos base path for a more RESTful design.
Here’s a breakdown of what each method does:
- The
@Put(':id')method allows updating an existing ToDo item. It takes theidfrom the URL parameters and the updatedtitle,dueDate, anddescriptionvalues from the request body. Theupdatemethod in theTodoServiceis then called to apply these changes. After the update, the updated list of ToDos is returned to render the page. - The
@Delete(':id')method removes a ToDo item by calling thedeletemethod in the service. It uses the HTTP method, and the item's ID is passed to the service to remove the corresponding entry from the list.
The TodoService is responsible for handling the logic behind updating and deleting ToDo items. Here’s how we implement these functionalities:
- The
updatemethod searches for a ToDo item by itsid. If found, it modifies thetitle,dueDate, anddescriptionproperties and returns the updated item. This method provides flexibility, as it can handle cases where only one of the properties is updated while leaving the others unchanged. - The
deletemethod filters out the item with the specified ID from thetodosarray, effectively removing it from the list.
To enable users to update and delete ToDo items, we need to modify the view (index.hbs) and use JavaScript to handle the PUT and DELETE requests:
updateTodofunction: This function gathers the updated data from the form and sends aPUTrequest to the/todos/:idendpoint. If successful, the page reloads to show the updated list.deleteTodofunction: This function sends aDELETErequest to the/todos/:idendpoint. Upon success, the page reloads to reflect the changes.
Since HTML forms don't directly support PUT and DELETE methods, we'll use JavaScript in the practices to handle these requests.
By implementing PUT and DELETE operations in your ToDo app, you:
- Enhance User Control: Users can now modify or remove tasks, providing flexibility in managing their to-do lists.
- Follow RESTful Design: Using
PUTfor updates andDELETEfor removals aligns with RESTful API conventions, making your application more standardized. - Improve Interactivity: With these operations, your app becomes more dynamic, allowing real-time interactions with the data.
By supporting these key operations, your application will feel more polished and provide a richer user experience. Ready to see these features in action? Let's move on to the exercises and put this into practice!
