Implement Book Management CRUD Operations
Overview
This pull request introduces the core CRUD functionalities for Book management, including creating, retrieving, updating, patching, and deleting books. The implementation spans across controller, service, route, and model layers, ensuring a modular and maintainable codebase.
Changes by File
/controllers/bookController.js
- Added controller methods for:
-
createBook: Handles book creation requests. -
getAllBooks: Retrieves all books. -
getBookById: Fetches a book by its ID. -
updateBook: Updates a book entirely. -
patchBook: Partially updates a book. -
deleteBook: Deletes a book by ID.
-
- Improved error handling for not found and validation errors.
/services/bookService.js
- Implemented service logic for:
- Validating book data (e.g., published year cannot be in the future).
- Creating, retrieving, updating, patching, and deleting books.
- Ensured proper error handling and data validation for each operation.
/routes/bookRoutes.js
- Defined RESTful routes for book operations:
-
POST /booksfor creation. -
GET /booksfor listing all books. -
GET /books/:idfor retrieving a specific book. -
PUT /books/:idfor full updates. -
DELETE /books/:idfor deletion.
-
- Added route for searching books (
GET /books/search).
/models/Book.js
- Defined the Book schema with fields:
-
title,author,publishedYear,genre, andavailable.
-
- Set up required validations and default values.
Summary
This PR establishes the foundational CRUD operations for Book management, ensuring robust validation, error handling, and a clean separation of concerns across the application layers.