# alejandro11102003/bibliotecapy architecture

> Architecture diagram of the GitHub repository alejandro11102003/bibliotecapy, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-07-19). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/alejandro11102003/bibliotecapy
- Repository: https://github.com/alejandro11102003/bibliotecapy

## Overview

##### Project Type and Purpose
- This is a full-stack web application (“Sistema de Gestión de Biblioteca”) built with Flask. Its goal is to manage book inventories, user registrations, loan transactions, and reporting for a library.

##### File Structure Analysis
- Top-level files:
- run.py – application entry point
- init_db.py – database initialization script
- config.py – environment & Flask configuration
- requirements.txt – dependency list
- app/ module:
- __init__.py – Flask app & SQLAlchemy setup
- models.py – ORM entities (Book, User, Loan)
- forms.py – WTForms definitions
- routes.py – HTTP controllers (endpoints)
- repositories/ – data access layer (one repository per entity + report)
- factories/ – ModelFactory to convert forms into domain models
- services/ – auxiliary services (e.g., FileService for image uploads)
- static/ & templates/ – frontend assets (CSS, JS, HTML with Bootstrap)
- grafico_numpy.py, pandas.py – data-analysis utilities for charts
- Data folder: app/data holds JSON snapshots but primary storage is MySQL via SQLAlchemy.

##### Architectural Patterns & Principles
- Repository Pattern: Separates data access logic.
- Factory Pattern: Centralizes object creation from forms.
- Service Layer: Encapsulates cross-cutting concerns (file handling).
- MVC-like: routes.py act as controllers, templates are views, models.py are domain models.
- SOLID principles: SRP, OCP, ISP, DIP are applied across layers.

##### Main Components
a. Frontend Layer
- Browser – user interface rendered via templates + Bootstrap + JS
- Static server – serves CSS/JS/images from app/static
b. Backend Layer
- Flask Application – handles HTTP requests in routes.py
- Controller Layer – routes call Services and Repositories
- Service Layer – FileService, potential other business services
- Repository Layer – LibroRepository, UsuarioRepository, PrestamoRepository, ReportRepository
- Factory Layer – ModelFactory converting form data to SQLAlchemy models
c. Database
- MySQL (configured via SQLAlchemy) – persistent storage for books, users, loans
d. Data-Analysis & Reporting
- Utility modules (grafico_numpy.py, pandas.py) + Matplotlib/NumPy for chart generation
e. External/Environment
- .env file – environment variables (SECRET_KEY, DATABASE_URL)
- SMTP or other services (if added later for notifications—optional)

##### Relationships & Data Flow
- User → Browser → HTTP Request → Flask routes
- routes → validate forms → ModelFactory → domain model
- routes → calls Repository to CRUD → DB
- Repository → SQLAlchemy → MySQL
- routes → call reporting Repository → uses pandas/grafico_numpy → Matplotlib → chart rendered in template
- Browser ← rendered HTML templates + chart images
- Static assets served in parallel

##### Architectural Patterns Highlight
- Clear layering: Presentation (templates/static) → Controllers (routes) → Services/Factories → Data Access (repositories) → Database

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Frontend Layer
    subgraph "Frontend Layer"
        direction TB
        Browser["Browser"]:::external
        StaticAssets["Static Assets"]:::static
        Templates["Templates"]:::static
    end

    %% Backend Layer
    subgraph "Backend Layer"
        direction TB
        RunPy["run.py"]:::code
        InitDBScript["init_db.py"]:::code
        Config["config.py"]:::code
        AppInit["app/__init__.py"]:::code
        Routes["app/routes.py"]:::code
        Forms["app/forms.py"]:::code
        Models["app/models.py"]:::code
        ModelFactory["ModelFactory"]:::code
        FileService["FileService"]:::code
        Utils["app/utils.py"]:::code
        subgraph "Repositories"
            direction TB
            LibroRepo["LibroRepository"]:::code
            UsuarioRepo["UsuarioRepository"]:::code
            PrestamoRepo["PrestamoRepository"]:::code
            ReportRepo["ReportRepository"]:::code
        end
        subgraph "Data Snapshots"
            direction TB
            JSONData["app/data JSON Snapshots"]:::static
        end
        subgraph "Data-Analysis Utilities"
            direction TB
            Grafico["grafico_numpy.py"]:::util
            Pandas["pandas.py"]:::util
        end
        Requirements["requirements.txt"]:::code
    end

    %% Database
    DB["MySQL Database"]:::db

    %% Environment
    Env[".env / Environment Variables"]:::code

    %% Data Flow
    Browser -->|"requests"| Routes
    Browser --> Templates
    StaticAssets --> Browser
    Routes -->|validates via| Forms
    Forms --> ModelFactory
    Routes --> ModelFactory
    ModelFactory --> Models
    Routes --> LibroRepo
    Routes --> UsuarioRepo
    Routes --> PrestamoRepo
    Routes --> ReportRepo
    FileService --> Routes
    Routes --> Utils
    Routes --> Grafico
    Routes --> Pandas
    ReportRepo --> Grafico
    ReportRepo --> Pandas
    LibroRepo -->|CRUD| DB
    UsuarioRepo -->|CRUD| DB
    PrestamoRepo -->|CRUD| DB
    ReportRepo -->|queries| DB
    InitDBScript --> DB
    Config --> AppInit
    Env --> Config
    RunPy --> AppInit
    AppInit -->|sets up| Routes

    %% Click Events
    click StaticAssets "https://github.com/alejandro11102003/bibliotecapy/tree/main/app/static"
    click Templates "https://github.com/alejandro11102003/bibliotecapy/tree/main/app/templates"
    click RunPy "https://github.com/alejandro11102003/bibliotecapy/blob/main/run.py"
    click InitDBScript "https://github.com/alejandro11102003/bibliotecapy/blob/main/init_db.py"
    click Config "https://github.com/alejandro11102003/bibliotecapy/blob/main/config.py"
    click AppInit "https://github.com/alejandro11102003/bibliotecapy/blob/main/app/__init__.py"
    click Models "https://github.com/alejandro11102003/bibliotecapy/blob/main/app/models.py"
    click Forms "https://github.com/alejandro11102003/bibliotecapy/blob/main/app/forms.py"
    click Routes "https://github.com/alejandro11102003/bibliotecapy/blob/main/app/routes.py"
    click LibroRepo "https://github.com/alejandro11102003/bibliotecapy/blob/main/app/repositories/libro_repository.py"
    click UsuarioRepo "https://github.com/alejandro11102003/bibliotecapy/blob/main/app/repositories/usuario_repository.py"
    click PrestamoRepo "https://github.com/alejandro11102003/bibliotecapy/blob/main/app/repositories/prestamo_repository.py"
    click ReportRepo "https://github.com/alejandro11102003/bibliotecapy/blob/main/app/repositories/report_repository.py"
    click ModelFactory "https://github.com/alejandro11102003/bibliotecapy/blob/main/app/factories/model_factory.py"
    click FileService "https://github.com/alejandro11102003/bibliotecapy/blob/main/app/services/file_service.py"
    click Grafico "https://github.com/alejandro11102003/bibliotecapy/blob/main/app/grafico_numpy.py"
    click Pandas "https://github.com/alejandro11102003/bibliotecapy/blob/main/app/pandas.py"
    click JSONData "https://github.com/alejandro11102003/bibliotecapy/tree/main/app/data"
    click Utils "https://github.com/alejandro11102003/bibliotecapy/blob/main/app/utils.py"
    click Requirements "https://github.com/alejandro11102003/bibliotecapy/blob/main/requirements.txt"

    %% Styles
    classDef code fill:#ADD8E6,stroke:#000,stroke-width:1px
    classDef static fill:#FFA500,stroke:#000,stroke-width:1px
    classDef db fill:#90EE90,stroke:#000,stroke-width:1px
    classDef util stroke-dasharray: 5 5,fill:#fff,stroke:#000,stroke-width:1px
    classDef external fill:#fff,stroke:#000,stroke-width:1px,font-style:italic
```
