# pardeshibabu/loan_service architecture

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

- Interactive diagram: https://gitdiagram.com/pardeshibabu/loan_service
- Repository: https://github.com/pardeshibabu/loan_service

## Overview

##### Main Components
- API Layer / HTTP Handlers: Represented by the "actions" directory. These include endpoints like creating loans, approving or disbursing loans, adding investments, and document handling.
- Business Logic / Controllers: Although intertwined with actions, this layer includes the core logic processing validated inputs and transitioning loan states.
- Data Models: Represented in the "models" directory containing the core entities (Loan, Investment, Document, Notification, etc.) as well as database validation and state logic.
- Database / Persistence Layer: The "migrations" directory and "database.yml" describe the schema and configuration for the MySQL database (using Buffalo’s POP ORM).
- View / Template Layer: Located in the "templates" directory (for server-side HTML rendering) and the "public" directory (for static assets like CSS, JS, and images).
- Notification Service: The "services/notification" directory encapsulates the logic to send agreement and other emails.
- Additional Layers:
- Middleware (in "middleware") for aspects like authentication.
- Configuration and deployment support from files like Dockerfile, docker-compose.yml, and various config files.
- Command entry point under "cmd/app/main.go" for starting the application.
- Asset pipeline configuration (Webpack, Babel, PostCSS, etc.) for front-end assets.

##### Relationships and Interactions
- Frontend Requests: Browser calls hit the HTTP endpoints defined in the actions.
- Business Logic Processing: Each endpoint calls application services (controllers) that handle business rules (e.g., validating loan state transitions) and invoke model operations.
- Data Persistence: Models interact with the MySQL database for CRUD operations. Migrations ensure the database schema is consistently managed.
- Template Rendering: For the HTML views served, the controllers use the template engine (using Buffalo templates in the “templates” directory).
- API Documentation: The README indicates a Swagger file (in public/swagger) to document the API, which should be represented as an external documentation component.
- Notification Triggers: After events like a fully invested loan, a notification service (email) is triggered, interacting with external mail servers.
- Docker & Deployment: Docker-compose orchestrates the containerized deployment, connecting the application, database, and other environmental configurations.

##### Architectural Patterns and Design Principles
- MVC Pattern: Clearly separate Models, Views, and Controllers.
- Containerization: Use Docker for consistent development, testing, and production environments.
- RESTful API Design: The endpoints follow REST principles.
- Modular/Service-based: The system modularly separates concerns (loan management, investment management, document handling, notifications).

##### Deployment Elements
- Optionally, connect the container to the external database host (as described by environment variables in docker-compose.yml).

## Diagram (Mermaid)

```mermaid
flowchart TD

    %% User Interface Layer
    subgraph "User Interface"
        CLIENT["Client/Browser"]:::client
        SWAGGER["Swagger API Docs"]:::client
    end

    %% Application Layer
    subgraph "Application Layer"
        MIDDLEWARE["Middleware"]:::middleware
        API_HANDLERS["API Handlers (HTTP)"]:::api
        BUSINESS_LOGIC["Business Logic (Controllers)"]:::business
        NOTIFICATION["Notification Service"]:::notification
    end

    %% Presentation Layer
    subgraph "Presentation Layer"
        TEMPLATES["Templates (Views)"]:::view
        STATIC["Static Assets"]:::view
    end

    %% Persistence Layer
    subgraph "Persistence Layer"
        MODELS["Models (ORM)"]:::persistence
        MIGRATIONS["Migrations"]:::persistence
        DB_CONFIG["Database Config"]:::persistence
        MYSQL["MySQL Database"]:::persistence
    end

    %% Deployment & Configuration
    subgraph "Deployment & Configuration"
        ENTRY["App Entry Point"]:::deployment
        DOCKER["Docker & Deployment"]:::deployment
        ASSET_PIPELINE["Asset Pipeline"]:::asset
    end

    %% Connections
    CLIENT -->|"HTTP_Request"| MIDDLEWARE
    MIDDLEWARE -->|"Process_Request"| API_HANDLERS
    API_HANDLERS -->|"Delegate"| BUSINESS_LOGIC
    BUSINESS_LOGIC -->|"CRUD_Operations"| MODELS
    MODELS -->|"Query/Update"| MYSQL
    MIGRATIONS -->|"Manage_Schema"| MYSQL
    DB_CONFIG -->|"DB_Settings"| MYSQL
    BUSINESS_LOGIC -->|"Render_HTML"| TEMPLATES
    BUSINESS_LOGIC -->|"Serve_Assets"| STATIC
    BUSINESS_LOGIC -->|"Trigger_Email"| NOTIFICATION
    CLIENT ---|"View_API_Docs"| SWAGGER
    ENTRY -->|"Initialize_App"| MIDDLEWARE
    DOCKER -->|"Deploy_Environment"| ENTRY
    ASSET_PIPELINE -->|"Build_Assets"| STATIC

    %% Styling of subgraphs to remain vertical
    linkStyle default stroke:#333,stroke-width:1.5px

    %% Click Events
    click API_HANDLERS "https://github.com/pardeshibabu/loan_service/tree/main/actions"
    click BUSINESS_LOGIC "https://github.com/pardeshibabu/loan_service/blob/main/actions/app.go"
    click MODELS "https://github.com/pardeshibabu/loan_service/tree/main/models"
    click MIGRATIONS "https://github.com/pardeshibabu/loan_service/tree/main/migrations"
    click DB_CONFIG "https://github.com/pardeshibabu/loan_service/blob/main/database.yml"
    click TEMPLATES "https://github.com/pardeshibabu/loan_service/tree/main/templates"
    click STATIC "https://github.com/pardeshibabu/loan_service/tree/main/public"
    click NOTIFICATION "https://github.com/pardeshibabu/loan_service/tree/main/services/notification"
    click MIDDLEWARE "https://github.com/pardeshibabu/loan_service/blob/main/middleware/auth.go"
    click ENTRY "https://github.com/pardeshibabu/loan_service/blob/main/cmd/app/main.go"
    click DOCKER "https://github.com/pardeshibabu/loan_service/blob/main/docker-compose.yml"
    click ASSET_PIPELINE "https://github.com/pardeshibabu/loan_service/blob/main/webpack.config.js"
    click SWAGGER "https://github.com/pardeshibabu/loan_service/blob/main/public/swagger/swagger.yaml"

    %% Class Definitions for Colors
    classDef client fill:#F39C12,stroke:#333,stroke-width:2px;
    classDef middleware fill:#3498DB,stroke:#333,stroke-width:2px;
    classDef api fill:#1ABC9C,stroke:#333,stroke-width:2px;
    classDef business fill:#2ECC71,stroke:#333,stroke-width:2px;
    classDef persistence fill:#F1C40F,stroke:#333,stroke-width:2px;
    classDef view fill:#9B59B6,stroke:#333,stroke-width:2px;
    classDef deployment fill:#E67E22,stroke:#333,stroke-width:2px;
    classDef notification fill:#E74C3C,stroke:#333,stroke-width:2px;
    classDef asset fill:#A569BD,stroke:#333,stroke-width:2px;
```
