[JITERA] Implement Reader Module for Book Borrowing Management
Created by: chi-jitera
Overview
This pull request introduces a new reader module that allows for the management of readers who borrow books. The module includes a model, controller, routes, and updates to the server configuration.
Changes Made
-
Create Reader Model
- A new model file
Reader.jswas created in the/modelsdirectory. This model defines the schema for a reader, including their name, contact information, and a list of borrowed books.
const mongoose = require('../config/db'); const readerSchema = new mongoose.Schema({ name: { type: String, required: true }, contactInfo: { type: String, required: true }, borrowedBooks: [{ type: mongoose.Schema.Types.ObjectId, ref: 'Book' }] }); module.exports = mongoose.model('Reader', readerSchema); - A new model file
-
Create Reader Controller
- A new controller file
readerController.jswas created in the/controllersdirectory. This controller includes methods for adding a reader, retrieving all readers, and allowing a reader to borrow a book.
const Reader = require('../models/Reader'); const Book = require('../models/Book'); exports.addReader = async (req, res) => { /* implementation */ }; exports.getReaders = async (req, res) => { /* implementation */ }; exports.borrowBook = async (req, res) => { /* implementation */ }; - A new controller file
-
Create Reader Routes
- A new routes file
readerRoutes.jswas created in the/routesdirectory. This file defines the API endpoints for managing readers, including routes for adding a reader, getting all readers, and borrowing a book.
const express = require('express'); const readerController = require('../controllers/readerController'); const router = express.Router(); router.post('/readers', readerController.addReader); router.get('/readers', readerController.getReaders); router.post('/readers/:readerId/borrow/:bookId', readerController.borrowBook); module.exports = router; - A new routes file
-
Update Server to Include Reader Module
- The
server.jsfile was updated to include the new reader routes. This allows the application to handle requests related to readers.
const readerRoutes = require('./routes/readerRoutes'); app.use('/api', readerRoutes); - The
This implementation provides a complete solution for managing readers and their book borrowing activities.