[JITERA] Implement Create Book API
Created by: chi-jitera
Overview
This pull request implements the "Create Book" API as specified. It includes the definition of a Book model, a controller for handling the creation of books, and the necessary routes to expose the API endpoint.
Changes
-
Model:
/models/Book.js- Created a Mongoose schema for the Book model with the following fields:
-
title: string, required -
author: string, required -
publishedYear: number, required -
genre: string, required -
available: boolean, defaults to true
-
- Added timestamps to track creation and update times.
- Created a Mongoose schema for the Book model with the following fields:
-
Controller:
/controllers/bookController.js- Implemented the
createBookfunction to handle POST requests to/books. - Validates the input fields to ensure they meet the required types and presence.
- Creates a new book record and saves it to the database.
- Returns a 201 status code on success and a 400 status code for validation errors.
- Implemented the
-
Route:
/routes/bookRoutes.js- Defined a POST route for
/booksthat maps to thecreateBookcontroller function.
- Defined a POST route for
-
App Configuration:
/app.js- Updated the main application file to include the book routes and ensure the app can parse JSON request bodies.
This implementation adheres to the requirements of no authentication/authorization and provides appropriate response codes for success and validation errors.