Skip to content

folg-code/Library-Service

Repository files navigation

📚 Library Service API

A production-style REST API for managing a modern digital library system.

The application replaces a fully manual, paper-based process for tracking books, borrowings, users, and payments with a structured, scalable backend system.

The system exposes a fully documented API and integrates external services for payments and notifications.

🚀 Overview

The Library Service API provides functionality for:

  • Managing book inventory
  • Handling user registration and authentication
  • Creating and tracking borrowings
  • Processing online payments
  • Calculating overdue fines
  • Sending asynchronous notifications

The project is designed as a modular backend system with clearly separated responsibilities between services. There is no frontend layer. The system is fully operable via REST API and Swagger UI.

🏗 Architecture

The system follows a modular Django app architecture:

  • books/
  • users/
  • borrowings/
  • payments/
  • notifications/

Core principles:

  • Separation of concerns
  • Clear domain boundaries
  • Explicit business logic
  • Transaction-safe operations
  • External integrations abstracted behind service layers
  • Asynchronous event handling

🧩 Domain Model

erDiagram
    USER {
        int id PK
        string email
        string first_name
        string last_name
        string password
        bool is_staff
        bool is_active
        datetime date_joined
    }

    BOOK {
        int id PK
        string title
        string author
        string cover
        int inventory
        decimal daily_fee
    }

    BORROWING {
        int id PK
        date borrow_date
        date expected_return_date
        date actual_return_date
        int user_id FK
        int book_id FK
    }

    PAYMENT {
        int id PK
        string status
        string type
        decimal money_to_pay
        string session_id
        string session_url
        datetime created_at
        int borrowing_id FK
    }

    USER ||--o{ BORROWING : "creates"
    BOOK ||--o{ BORROWING : "borrowed in"
    BORROWING ||--o{ PAYMENT : "generates"
Loading

🔐 Authentication & Authorization

  • JWT authentication via djangorestframework-simplejwt
  • Email-based login
  • Access & refresh tokens
  • Role-based access control (admin vs regular user)

Security model:

  • Users can access only their own borrowings and payments
  • Admin users can access all records
  • Public endpoints limited to registration and token generation

💳 Stripe Integration

Payments are processed using Stripe Checkout.

Flow:

  • Borrowing created
  • Stripe Checkout session generated
  • Payment record created (PENDING)
  • User completes payment
  • Stripe webhook updates status to PAID
  • Success endpoint confirms payment state
  • Fine payments are generated automatically for overdue returns:
 fine = overdue_days * daily_fee * FINE_MULTIPLIER

📖 API Endpoints

Endpoint Method Description
/api/user/register/ POST Register a new user
/api/user/token/ POST Get access and refresh tokens
/api/user/token/refresh/ POST Refresh access token
/api/user/me/ GET, PUT, PATCH Retrieve or update authenticated user's profile
/api/books/ GET List all books
/api/books/ POST Create a new book (admin only)
/api/books/{id}/ GET Retrieve book details
/api/books/{id}/ PUT, PATCH Update book (admin only)
/api/books/{id}/ DELETE Delete book (admin only)
/api/borrowings/ GET List borrowings (filtered by user / active status)
/api/borrowings/ POST Create a new borrowing
/api/borrowings/{id}/ GET Retrieve borrowing details
/api/borrowings/{id}/return/ POST Return a borrowed book
/api/payments/ GET List payments
/api/payments/{id}/ GET Retrieve payment details
/api/payments/success/ GET Check payment status after Stripe redirect
/api/payments/cancel/ GET Stripe cancel callback
/api/payments/webhooks/stripe/ POST Stripe webhook for payment confirmation

Testing

  • Unit tests for models and business logic
  • API endpoint tests
  • Stripe session mocked in tests
  • Celery tasks mocked

Tests coverage: 96%

  • Borrowings: 100%
  • Payments (incl. webhook): 100%
  • Notifications tasks: 92%
  • Overall branch-safe transactional logic covered

⚙️ Technology Stack

  • Python 3.11
  • Django
  • Django REST Framework
  • drf-spectacular
  • Celery
  • Redis
  • PostgreSQL
  • Stripe SDK

🚀 Running the Project

  1. Build containers
docker-compose build
  1. Start services
docker-compose up
  1. Run migrations
docker-compose exec web python manage.py migrate
  1. Create superuser
docker-compose exec web python manage.py createsuperuser

🌐 Access the Application

Service URL
API http://localhost:8000
Swagger http://localhost:8000/api/docs/
ReDoc http://localhost:8000/api/redoc/

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

Packages

Contributors

Languages