Enhancing the API with Filters and Specific Modifiers

Welcome to this lesson on enhancing your Todo REST API with filters and specific modifiers using NestJS. So far, we've covered the basics of setting up GET, POST, PUT, and DELETE requests which allows you to Create, Retrieve, Update, and Delete (CRUD) Todo items.

In this lesson, we'll build on those foundations by adding filters and specific modifiers to our API. You'll learn how to implement a filter to show only incomplete tasks and a modifier to mark tasks as complete. These features will make your API more flexible and powerful.

Adding Filters to the Todo API

Filters allow users to retrieve specific subsets of data, making the API more versatile. Imagine a case where the client only wants to see the incomplete Todo items. In this section, we'll add a filter to show only incomplete Todo items using query parameters.

Introduction to Query Parameters

Query parameters are used to pass optional information to a web API. They are appended to the URL and can be accessed in the request handler. In practice, they are added to the URL and may look familiar to you. Let's say you want to list all of the Todos that are incomplete, we can support a filter in the GET requiest such as GET /todos?showIncomplete=1 via the query parameter.

We'll add a showIncomplete filter to our findAll method in the TodoController and TodoService.

Code Example: `findAll` Method in `TodoController`
Code Example: `findAll` Method in `TodoService`
Adding Specific Modifiers to the Todo API

Specific modifiers allow users to make targeted updates to individual items. In this section, we'll add a modifier to mark tasks as complete using path parameters.

Understanding Path Parameters

Path parameters are used to identify specific resources in an API. They are part of the URL and can be accessed in the request handler.

We'll add a complete method to our TodoController and TodoService to mark tasks as complete. This will be a PUT request because we are modifying existing data (similar to updating). The URL for this modification will be PUT /todos/:id/complete.

Code Example: `complete` Method in `TodoController`
Code Example: `complete` Method in `TodoService`
Summary and Next Steps

In this lesson, you learned how to enhance your To-Do REST API with filters and specific modifiers. We covered adding a showIncomplete filter and a complete modifier. These additions make your API more powerful and user-friendly.

Key Takeaways
  • Query parameters enable filtering data.
  • Path parameters allow targeted updates.
  • Separation of concerns: controllers handle requests, services manage business logic.
Prepare for Practice

Next, you'll get hands-on practice with these enhancements in the upcoming exercises. Try implementing more filters and modifiers to deepen your understanding.

Sign up
Join the 1M+ learners on CodeSignal
Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal