Skip to content

Latest commit

Β 

History

37 Commits

Folders and files

NameName
Last commit message
Last commit date
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

Library Management System API

A comprehensive RESTful API for managing library operations with authentication, role-based access control, and a complete book borrowing system.

πŸ“š Documentation

  • SETUP.md β€” getting a dev environment running (Docker or manual)
  • API.md β€” full endpoint reference (generated β€” see below)
  • Interactive docs β€” GET /api/docs on a running server (Swagger UI)
  • ARCHITECTURE.md β€” system design, DB schema, auth model
  • SECURITY_TESTING.md β€” security posture + manual test procedures
  • DOCKER.md β€” Docker-specific details
  • TODO.md β€” prioritized punch list / completion record
  • CONTRIBUTING.md β€” how to contribute
  • CHANGELOG.md β€” release notes

πŸš€ Features

  • πŸ” JWT Authentication - Secure user authentication with JSON Web Tokens
  • πŸ‘₯ Role-Based Access Control - Three user roles (Admin, Librarian, User)
  • πŸ“š Book Borrowing System - Complete borrowing and return workflow with due date tracking
  • ⏰ Overdue Management - Automatic overdue detection and late fee calculation
  • πŸ” Search & Pagination - Advanced search with pagination on all list endpoints
  • βœ… Input Validation - Comprehensive validation and sanitization
  • πŸ›‘οΈ Rate Limiting - Role-based rate limiting for API security
  • πŸ”’ Security Headers - Multiple security layers including CORS, XSS protection
  • πŸ“ Request Logging - Detailed logging of all API requests
  • ❌ Error Handling - Centralized error handling with proper HTTP status codes

πŸ› οΈ Tech Stack

  • Runtime: Node.js
  • Framework: Express.js
  • Database: MySQL
  • Authentication: JWT (jsonwebtoken)
  • Password Hashing: bcrypt
  • Environment Variables: dotenv

πŸ“¦ Installation

# Clone the repository
git clone <repository-url>
cd LMS-API

# Install dependencies
npm install

# Set up environment variables (dotenv loads this from src/, not the repo root)
cp src/.env.example src/.env
# Edit src/.env with your configuration β€” generate a JWT_SECRET with:
node -e "console.log(require('crypto').randomBytes(32).toString('hex'))"

# Set up database
mysql -u root -p < src/Database/library.database.sql

# Start the server
npm run dev

See SETUP.md for the full walkthrough (including the Docker path) and DOCKER.md for Docker-specific details.

βš™οΈ Environment Variables

Create a src/.env file with the following variables:

# Server

PORT=5080
NODE_ENV=development

# Database

DB_HOST=localhost
DB_USER=root
DB_PASSWORD=your_password
DB_NAME=library_db
DB_PORT=3306

# JWT

JWT_SECRET=your_super_secret_key_here
JWT_EXPIRE=7d

# Security

CORS_ORIGINS=<http://localhost:3000,http://localhost:3001>
BCRYPT_ROUNDS=12
INITIAL_SETUP_KEY=your_secure_setup_key_here

πŸ—„οΈ Database Schema

The system uses 4 main tables:

  • users - User accounts with roles (Admin, Librarian, User)
  • authors - Book authors information
  • books - Book inventory and availability
  • borrow_records - Track book borrowing and returns

πŸ”Œ API Endpoints

Authentication

Method Endpoint Description Access
POST /api/auth/register Register new user Public
POST /api/auth/login Login user Public
GET /api/auth/me Get current user profile Protected
POST /api/auth/logout Logout user Protected
POST /api/auth/refresh Refresh JWT token Protected
POST /api/auth/change-password Change password Protected
POST /api/auth/setup-admin Initial admin setup Public (with setup key)

Authors

Method Endpoint Description Access
GET /api/authors Get all authors Public
GET /api/authors/:id Get author by ID Public
POST /api/authors Create author Admin/Librarian
PUT /api/authors/:id Update author Admin/Librarian
DELETE /api/authors/:id Delete author Admin/Librarian

Books

Method Endpoint Description Access
GET /api/books Get all books with filters Public
GET /api/books/:id Get book by ID Public
POST /api/books Create book Admin/Librarian
PUT /api/books/:id Update book Admin/Librarian
DELETE /api/books/:id Delete book Admin/Librarian
POST /api/books/:id/borrow Borrow a book Authenticated
POST /api/books/:id/return Return a book Authenticated

Users

Method Endpoint Description Access
GET /api/users Get all users Admin
GET /api/users/public Get public users list Public
GET /api/users/profile Get current user profile Protected
GET /api/users/:id Get user by ID Admin/Librarian/Own
GET /api/users/:id/borrow-records Get user borrow records Protected
POST /api/users Create user Admin
PUT /api/users/:id Update user Admin/Own
DELETE /api/users/:id Delete user Admin

Borrow Records

col1 Endpoint Description Access
GET /api/borrow-records Get all borrow records Admin/Librarian
GET /api/borrow-records/overdue Get overdue records Admin/Librarian
GET /api/borrow-records/statistics Get borrowing stats Admin/Librarian
POST /api/borrow-records/:id/extend Extend due date User/Admin/Librarian

πŸ” Authentication

All protected endpoints require a JWT token in the Authorization header:

Authorization: Bearer <your_jwt_token>

Example Login Request

curl -X POST http://localhost:5080/api/auth/login \
  -H "Content-Type: application/json" \
  -d '{
    "emailOrUsername": "user@example.com",
    "password": "Password123"
  }'

πŸ‘₯ Role-Based Access

Admin

  • Full system access
  • User management (CRUD operations)
  • Book and author management
  • View all borrow records and statistics
  • System configuration

Librarian

  • Book and author management
  • View and update user profiles
  • Manage borrow records
  • View statistics
  • Cannot delete users or modify system settings

User

  • Browse books and authors
  • Borrow and return books
  • View own borrow history
  • Update own profile
  • Cannot access admin features

πŸ” Query Parameters

Pagination

  • page - Page number (default: 1)
  • limit - Items per page (default: 10, max: 100)

Search

  • search - Search query string across relevant fields

Filters

  • status - Filter by status (Available, Borrowed, Overdue, etc.)
  • author_id - Filter books by author
  • genre - Filter books by genre
  • role - Filter users by role
  • is_active - Filter users by active status
  • overdue_only - Filter for overdue records only

Example

GET /api/books?page=1&limit=20&search=harry&genre=Fantasy&status=Available

πŸ“‹ Business Rules

Borrowing System

  • Maximum 5 books per user simultaneously
  • Default loan period: 14 days (configurable)
  • Cannot borrow same book twice simultaneously
  • Book must be available (available_copies > 0)
  • Users must have active account status

Returns & Overdue

  • Late fee: $1 per day after due date
  • Book becomes available immediately after return
  • Overdue status automatically updated
  • Due dates can be extended (once per borrow)

Validation Rules

  • Email must be unique and valid format
  • ISBN must be unique
  • Password minimum 8 characters with complexity requirements
  • Phone numbers validated for format
  • Required fields enforced on all models

πŸ›‘οΈ Security Features

Rate Limiting

  • Guest: 20 requests/15 minutes
  • User: 60 requests/15 minutes
  • Librarian: 120 requests/15 minutes
  • Admin: 300 requests/15 minutes

Input Sanitization

  • XSS protection
  • SQL injection prevention
  • HTML tag removal
  • Parameter validation

Security Headers

  • X-Frame-Options: DENY
  • X-Content-Type-Options: nosniff
  • X-XSS-Protection: 1; mode=block
  • Strict-Transport-Security
  • Content Security Policy

❌ Error Responses

All error responses follow this format:

{
  "success": false,
  "message": "Error description",
  "error": "Detailed error message",
  "errors": [] // Optional validation errors
}

HTTP Status Codes

  • 200 - Success
  • 201 - Created
  • 400 - Bad Request / Validation Error
  • 401 - Unauthorized
  • 403 - Forbidden
  • 404 - Not Found
  • 409 - Conflict
  • 429 - Too Many Requests
  • 500 - Internal Server Error

πŸš€ Quick Start

1. Initial Setup

See Installation above (or SETUP.md for the full walkthrough, including the Docker path).

2. Create Admin User

# Use the setup key from your src/.env file
curl -X POST http://localhost:5080/api/auth/setup-admin \
  -H "Content-Type: application/json" \
  -d '{
    "admin_email": "admin@yourlibrary.com",
    "admin_password": "SecurePass123!",
    "setup_key": "YOUR_SETUP_KEY_FROM_ENV"
  }'

3. Test the API

# Login as admin
curl -X POST <http://localhost:5080/api/auth/login> \
  -H "Content-Type: application/json" \
  -d '{
    "emailOrUsername": "<admin@yourlibrary.com>",
    "password": "SecurePass123!"
  }'

# Use the returned token for protected endpoints
curl -X GET <http://localhost:5080/api/auth/me> \
  -H "Authorization: Bearer YOUR_JWT_TOKEN" \
  -H "Content-Type: application/json"

πŸ“ Project Structure

LMS-API/
β”œβ”€β”€ src/
β”‚   β”œβ”€β”€ config/           # Database + auth/JWT/password/rate-limit config
β”‚   β”œβ”€β”€ controllers/      # Request handling, one file per resource
β”‚   β”œβ”€β”€ middlewares/      # Auth, validation, centralized error handling
β”‚   β”œβ”€β”€ models/           # All SQL lives here; row β†’ API-shape formatting
β”‚   β”œβ”€β”€ routes/           # Express routers, one per resource
β”‚   β”œβ”€β”€ jobs/              # Scheduled jobs (overdue borrow-record sweep)
β”‚   β”œβ”€β”€ utils/             # Logger, cache, mailer, token/store helpers
β”‚   β”œβ”€β”€ validation/        # Zod request-body schemas
β”‚   β”œβ”€β”€ Database/          # library.database.sql (schema quick-start)
β”‚   β”œβ”€β”€ .env               # Real env vars (gitignored) β€” loaded via __dirname, not CWD
β”‚   β”œβ”€β”€ .env.example        # Template for src/.env
β”‚   └── server.js          # Entry point β€” middleware wiring, graceful shutdown
β”œβ”€β”€ migrations/            # Forward-only SQL migrations + schema_migrations tracking
β”œβ”€β”€ scripts/               # migrate.js (migration runner)
β”œβ”€β”€ tests/                 # Jest + Supertest β€” api/, unit/, helpers/; .env.test lives here too
β”œβ”€β”€ docs/                  # All documentation (this file included)
β”‚   β”œβ”€β”€ README.md          # You are here
β”‚   β”œβ”€β”€ SETUP.md           # Getting a dev environment running
β”‚   β”œβ”€β”€ API.md              # Full endpoint reference
β”‚   β”œβ”€β”€ ARCHITECTURE.md     # System design, DB schema, auth model
β”‚   β”œβ”€β”€ SECURITY_TESTING.md # Security posture + manual test procedures
β”‚   β”œβ”€β”€ TODO.md              # Prioritized punch list / completion record
β”‚   β”œβ”€β”€ CONTRIBUTING.md      # How to contribute
β”‚   β”œβ”€β”€ CHANGELOG.md         # Release notes
β”‚   └── DOCKER.md            # Docker-specific details
β”œβ”€β”€ docker/                # Dockerfile, docker-compose.yml, .env.example
β”œβ”€β”€ .github/workflows/     # CI (lint β†’ test β†’ docker build)
└── package.json

πŸ§ͺ Development

# Run in development mode with auto-reload
npm run dev

# Run in production mode
npm start

# Generate new secrets
node generate-secrets.js

πŸ“ Example Requests

User Registration

curl -X POST http://localhost:5080/api/auth/register \
  -H "Content-Type: application/json" \
  -d '{
    "first_name": "John",
    "last_name": "Doe",
    "user_name": "johndoe",
    "email": "john@example.com",
    "password": "Password123"
  }'

Create Book

curl -X POST http://localhost:5080/api/books \
  -H "Authorization: Bearer YOUR_TOKEN" \
  -H "Content-Type: application/json" \
  -d '{
    "isbn": "978-0451524935",
    "title": "1984",
    "author_id": 1,
    "published_date": "1949-06-08",
    "genre": "Dystopian Fiction",
    "total_copies": 5
  }'

Borrow Book

curl -X POST http://localhost:5080/api/books/1/borrow \
  -H "Authorization: Bearer YOUR_TOKEN" \
  -H "Content-Type: application/json" \
  -d '{
    "due_days": 21
  }'

🀝 Contributing

  • Fork the repository
  • Create your feature branch (git checkout -b feature/AmazingFeature)
  • Commit your changes (git commit -m 'Add some AmazingFeature')
  • Push to the branch (git push origin feature/AmazingFeature)
  • Open a Pull Request

About

REST API for a Library Management System. My first Backend Engineering Project, building a RESTful API for a library management system that manages books, authors, and users. It also handles borrowing and returning of books, with additional features like role-based access control, search, and caching.

Resources

Contributing

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages