Skip to content

sofiengwin/book-management-system

Repository files navigation

Book Management API

Overview

The Book Management API is a library system built using Ruby on Rails. It allows users to borrow and return books while keeping track of transactions, account balances The system ensures that users can only borrow books if they have sufficient balance.

Features

  1. User Management
  • Users have an account with an initial balance.
  • Users can query their account balance and borrowing history.
  1. Book Borrowing & Returning
  • Users can borrow books if they have sufficient balance
  • A fee is deducted from the user's balance upon borrowing.
  • Users must return borrowed books before borrowing again.
  1. Reporting & Queries
  • Query a user's account and borrowed books.
  • Query book loan counts
  • Generate user reports for monthly and annual borrowing activity.
  • Calculate book income over a time period.

API Endpoints

User Endpoints

  • POST /api/v1/users - Create a new user with an initial balance.
  • GET /api/v1/users/:id/account - Retrieve a user's account details.
  • GET /api/v1/users/:id/report?type=monthly|annual - Get user borrowing report for a given period.

Transaction Endpoints

  • POST /api/v1/transactions/return - Return a book (requires user_id and book_id).
  • POST /api/v1/transactions/borrow - Borrow a book (requires user_id and book_id).

Book Endpoints

  • GET /api/v1/books/:id/income?start_at=YYYY-MM-DD&end_at=YYYY-MM-DD - Get total income for a book within a date range.

Design Principles

1. Service-Oriented Architecture

  • Business logic is encapsulated in service objects (BorrowBook, ReturnBook, BookIncome).

2. Active Record Models

  • User: Manages user data and balance constraints.
  • Book: Manages book details.
  • Transaction: Tracks borrow and return transactions.

3. API Documentation with Swagger (Rswag)

  • Rswag is used to generate API documentation.
  • Request examples are automatically included.

How to use Swagger documentation

After creating a user, log in to retrieve your authentication token. Then, click the Authorize button and enter the token obtained from the login response to secure your session.

Installation & Setup

  1. Clone the repository:
    git clone https://github.com/your-repo/book-management-api.git
    cd book-management-api
  2. Install dependencies:
    bundle install
  3. Set up the database:
    rails db:create db:migrate db:seed
  4. Run the server:
    rails s

Running Tests

  • Run RSpec tests:
    rspec
  • Run API documentation tests:
    rake rswag:specs:swaggerize

Future Improvements

  • Book availability management.
  • Implementing transaction status.
  • Add background jobs for overdue book notifications.
  • Improvements to authentication system
  • Implement validation to prevent users from borrowing a book that they have already checked out.

Swagger API Documentation

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published