Skip to content

[JITERA] Implement Create Book API

chi le requested to merge feat/create-book-api-1759156504 into main

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

  1. 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.
  2. Controller: /controllers/bookController.js

    • Implemented the createBook function 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.
  3. Route: /routes/bookRoutes.js

    • Defined a POST route for /books that maps to the createBook controller function.
  4. 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.

Merge request reports