Welcome back! In the previous lesson, you learned how to create new resources in your API using DTOs and validation. Now, we will complete the core features for managing both books and users by implementing all CRUD operations — Create, Read, Update, and Delete. These operations are the foundation of almost every web application, allowing you to add, view, change, and remove data as needed.
CRUD stands for:
- Create – Add new data (via
POST) - Read – Retrieve existing data (
GETone or all) - Update – Modify data (
PATCHorPUT) - Delete – Remove data (
DELETE)
These actions mirror how real apps behave — whether you're building a task manager, social platform, or e-commerce dashboard. In NestJS, each of these actions maps to a method in a controller, which delegates logic to a service, ensuring separation of concerns.
In this lesson, you will see how these operations work together in the Reading Tracker API. By the end, you will understand how to manage both books and users through the API, setting you up for hands-on practice and real-world projects.
Before we dive in, let’s quickly remind ourselves how the project is organized. You already have a NestJS app with modules for books and users, each with its own controller, service, and DTOs. The data is stored in a simple in-memory database service.
Here’s a quick summary of the setup:
This structure helps keep your code organized and makes it easy to add new features.
Controllers are responsible for handling incoming HTTP requests and sending responses. Each controller method matches a specific API endpoint and HTTP method.
Let’s look at some examples from the BooksController and UsersController:
Each method does the following:
@Post()handles creating a new book.@Get()returns all books.@Patch(':id')updates a book by its ID.@Delete(':id')removes a book by its ID.
The
@Patch(':id')route is used for partial updates. For example, you might only want to change the book's title without updating the author.
Services contain the business logic for your application. They interact with the data and handle all the actual work behind each operation.
Here’s a look at some key methods from the BooksService:
Explanation:
createadds a new book to the list.findAllreturns all books.updatefinds a book by ID and updates its properties.Object.assign(book, updateBookDto);merges the new data into the existing book object. If the user only sends{ "title": "Updated Title" }, only the title is changed. The author field stays untouched. This mirrors the behavior of — partial update without replacing the full object.
DTOs (Data Transfer Objects) help you control and validate the data that comes into your API. You have already used them for creating new resources. For updates, you can use a special helper called PartialType to make all fields optional.
Here’s how the update DTO is defined:
Explanation:
PartialType(CreateBookDto)creates a new DTO where all fields fromCreateBookDtoare optional.- This is useful for
PATCHrequests, where you might only want to update one or two fields.
Let’s say the original CreateBookDto required both title and author. But when you update a book, you might only want to change the author. With PartialType, all fields become optional and still inherit the original validation rules if present.
The same pattern is used for updating users.
Let’s walk through what happens when a user updates a book:
- Frontend sends a
PATCHrequest to/books/1with a JSON body:
In this lesson, you learned how to complete the core CRUD operations for both books and users in your Reading Tracker API. You saw how controllers handle requests, services perform the logic, and DTOs help with data validation and updates.
You have now reached the end of this course! Well done for making it through all the lessons. You are ready to put your knowledge into practice with the exercises that follow. Keep experimenting and building — these skills are the foundation for many real-world applications. Congratulations on your progress!
