# kaushikrajghimire/book-management-system architecture

> Architecture diagram of the GitHub repository kaushikrajghimire/book-management-system, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-04-10). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/kaushikrajghimire/book-management-system
- Repository: https://github.com/kaushikrajghimire/book-management-system

## Overview

##### Project Type & Purpose
- This is a backend API service built with FastAPI.
- Its purpose is to provide endpoints for user registration, authentication via JWT tokens, and CRUD operations for books.
- The system uses MongoDB as its database, storing user credentials (with hashed passwords) and book records.
- Authentication (login, token generation) and data validation (using Pydantic via schemas/models) are core features indicated by both the file structure and the README description.

##### File Structure Analysis
- Top-Level:
- The project contains a main FastAPI application entry point (app/main.py).
- The “app” directory is subdivided into:
▪ api – Contains files for the API endpoints that handle authentication (app/api/auth.py) and book operations (app/api/books.py).
▪ core – Houses the key security and JWT authentication functions located in app/core/auth.py and app/core/security.py.
▪ db – Contains the database connection logic (app/db/database.py) which sets up the MongoDB connection.
▪ schemas – Provides Pydantic models that handle data validation for books (app/schemas/book.py) and users (app/schemas/user.py). (Note: the README mentions a “models” directory, but the file tree shows “schemas”. This is likely just a naming difference.)
- There are configuration files like app/config.py and pyproject.toml for dependency and project management.
- Configuration and Build Scripts:
- .gitignore for version control management.
- The use of uvicorn for running the server and uv dependency management is highlighted in the README.

##### Key Components to Highlight
a. The Client / API Consumer:
- Represents the user or application making HTTP requests (e.g., via web browsers or tools like Postman).
b. FastAPI Application:
- Entry point of the application (app/main.py).
- Routes are included from sub-modules.
c. API Layer:
- Authentication API (app/api/auth.py): Exposes endpoints such as POST /register and POST /token.
- Book CRUD API (app/api/books.py): Exposes endpoints for adding, updating, getting, and deleting books.
d. Core Security/Authentication Module:
- JWT & password handling located in app/core/auth.py and app/core/security.py.
- Functions for creating hashed passwords, verifying passwords, and generating JWT tokens are critical.
e. Database Layer:
- MongoDB access is encapsulated in app/db/database.py.
- This layer interacts with the “users_collection” and “books_collection”.
f. Data Validation:
- Pydantic models found in app/schemas (representing users and books) ensure input meets defined constraints.

##### Relationships & Data Flow
- Client → FastAPI Application: HTTP requests are directed to the FastAPI endpoint.
- FastAPI Application: Routes requests to the appropriate API modules.
- For user registration/login, the API calls the authentication module (core/auth.py) to handle hashing and JWT token creation.
- For any operation on books, after ensuring the user is authenticated (through JWT tokens), the API calls the book endpoint methods.
- API Modules ↔ Database Layer: Both user and book operations interact with MongoDB through the database module.
- Data validation occurs at the API layer using Pydantic models from the schemas folder, ensuring errors are caught early.

##### Architectural Patterns & Design Principles
- Separation of Concerns is evident:
- API endpoints, core authentication/authorization logic, and database access are separated into distinct directories/modules.
- Modular Design:
- Each directory is tasked with a specific responsibility (e.g., security in core, routes in api, database interactions in db, data validation in schemas).
- Use of JWT for secure, stateless session management.
- Emphasis on secure password handling (hashing and verification) and role-based endpoint access.

## Diagram (Mermaid)

```mermaid
graph TD
    A["Client / API Consumer"]:::client
    B["FastAPI Application (main.py)"]:::backend
    C["Authentication API (auth.py)"]:::api
    D["Book CRUD API (books.py)"]:::api
    E["Core Security & JWT (auth.py, security.py)"]:::core
    F["Data Validation (User & Book Schemas)"]:::schema
    G["Database (MongoDB)"]:::database

    A -->|"HTTP_Request"| B
    B -->|"Routes_to"| C
    B -->|"Routes_to"| D
    C -->|"HandlesAuth"| E
    C -->|"DB_Operations"| G
    D -->|"ValidatesData"| F
    D -->|"CRUD_Operations"| G
    F -->|"EnsuresIntegrity"| G
    E -->|"AccessesDB"| G

    click B "https://github.com/kaushikrajghimire/book-management-system/blob/dev/app/main.py"
    click C "https://github.com/kaushikrajghimire/book-management-system/blob/dev/app/api/auth.py"
    click D "https://github.com/kaushikrajghimire/book-management-system/blob/dev/app/api/books.py"
    click E "https://github.com/kaushikrajghimire/book-management-system/blob/dev/app/core/auth.py"
    click E "https://github.com/kaushikrajghimire/book-management-system/blob/dev/app/core/security.py"
    click G "https://github.com/kaushikrajghimire/book-management-system/blob/dev/app/db/database.py"
    click F "https://github.com/kaushikrajghimire/book-management-system/blob/dev/app/schemas/user.py"
    click F "https://github.com/kaushikrajghimire/book-management-system/blob/dev/app/schemas/book.py"

    classDef client fill:#b3e5fc,stroke:#039be5,stroke-width:2px;
    classDef backend fill:#c8e6c9,stroke:#2e7d32,stroke-width:2px;
    classDef api fill:#ffccbc,stroke:#d84315,stroke-width:2px;
    classDef core fill:#ffe082,stroke:#ff8f00,stroke-width:2px;
    classDef schema fill:#f8bbd0,stroke:#ad1457,stroke-width:2px;
    classDef database fill:#d1c4e9,stroke:#4527a0,stroke-width:2px;
```
