# yonexdu0ra/qltv architecture

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

- Interactive diagram: https://gitdiagram.com/yonexdu0ra/qltv
- Repository: https://github.com/yonexdu0ra/qltv

## Overview

##### Project Type and Purpose
- This is a full-stack Node.js/Express monolith for “qltv” (quản lý thư viện) – a library management system.
- It follows an MVC/service/repository layered architecture, rendering EJS views on the server and serving static assets.

##### Top-Level Components
- Client Layer
– Web browser accessing EJS-rendered HTML and public JS/CSS.
- Presentation Layer (Express App)
– Static asset serving (src/public, src/css, tailwind.config.js)
– View rendering (src/views/*.ejs)
- API Layer
– Routing (src/routes/public & src/routes/private)
– Middleware (authentication, authorization)
- Controller Layer (src/controllers)
– Orchestrates HTTP requests → business logic.
- Service Layer (src/services)
– Core business rules and transaction management.
- Repository Layer (src/repositories)
– Data access via ORM (Sequelize models in src/models).
- Data Layer
– Relational Database (configured in src/config/database.js)
– Redis cache (src/config/redisClient.js)
– File storage (Cloudinary via src/config/cloudinary.js; multer for uploads)
- Utilities
– JWT, slug generator, base64, formatting (src/utils).
- Seeders (src/seeders)
– Initial data population.

##### Interactions and Data Flow
1. Client → HTTP GET/POST to Express server
2. Express static middleware serves /public and /css assets directly
3. Dynamic routes hit src/routes → pass through authenticationMiddleware → authorizationMiddleware
4. Route handler invokes corresponding controller
5. Controller calls service layer for business logic
6. Service uses repositories to CRUD via Sequelize models ↔ Database
7. For caching reads/writes, repositories use Redis client
8. For file uploads, controllers use multer → cloudinary upload
9. Controller returns data or renders EJS view back to client

##### Architectural Patterns and Principles
- Layered Architecture (Routing → Middleware → Controller → Service → Repository → Model)
- Separation of Concerns (controllers do orchestration, services contain logic, repositories handle data)
- Single Responsibility per module
- Config modules isolate infrastructure details (DB, Redis, Cloudinary)
- Seeders for idempotent data setup

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Client
    Client["Web Browser"]:::ui

    %% Presentation Layer
    subgraph "Presentation Layer"
        StaticAssets["Static Assets"]:::ui
        CSS["CSS Stylesheet"]:::ui
        JS["Frontend Scripts"]:::ui
        Views["EJS Views"]:::ui
        Tailwind["Tailwind Config"]:::ui
    end

    %% API Layer
    subgraph "API Layer"
        PublicRoutes["Public Routes"]:::api
        PrivateRoutes["Private Routes"]:::api
        AuthMW["Authentication Middleware"]:::middleware
        AuthzMW["Authorization Middleware"]:::middleware
    end

    %% Controller Layer
    subgraph "Controller Layer"
        Controllers["Controllers"]:::controller
    end

    %% Service Layer
    subgraph "Service Layer"
        Services["Services"]:::service
    end

    %% Repository Layer
    subgraph "Repository Layer"
        Repositories["Repositories"]:::repository
    end

    %% Models
    Models["Sequelize Models"]:::repository

    %% Data Layer
    subgraph "Persistence Layer"
        DB["Relational Database"]:::db
        Redis["Redis Cache"]:::cache
        Cloudinary["Cloudinary"]:::external
        Multer["Multer Upload Config"]:::external
    end

    %% Utilities and Seeders
    Utils["Utilities"]:::utility
    Seeders["Seeders"]:::utility
    Entry["App Entry Point"]:::utility

    %% Connections
    Client -->|HTTP GET/POST| StaticAssets
    Client -->|HTTP GET/POST| Views
    Client -->|HTTP GET/POST| PublicRoutes
    Client -->|HTTP GET/POST| PrivateRoutes

    StaticAssets -->|served by| Entry
    Views -->|rendered by| Entry
    PublicRoutes -->|routes through| AuthMW
    AuthMW -->|then| AuthzMW
    AuthzMW -->|then| Controllers
    PrivateRoutes -->|routes through| AuthMW

    Controllers -->|calls| Services
    Services -->|uses| Repositories
    Repositories -->|CRUD via| Models
    Models -->|SQL| DB
    Repositories -.->|cache read/write| Redis
    Controllers -.->|upload via| Multer
    Controllers -.->|upload to| Cloudinary

    Entry --> PublicRoutes
    Entry --> PrivateRoutes
    Entry --> StaticAssets
    Entry --> Views

    Services --> Utils
    Entry --> Seeders

    %% Click Events
    click StaticAssets "https://github.com/yonexdu0ra/qltv/tree/main/src/public"
    click CSS "https://github.com/yonexdu0ra/qltv/blob/main/src/css/styles.css"
    click JS "https://github.com/yonexdu0ra/qltv/tree/main/src/public/js"
    click Views "https://github.com/yonexdu0ra/qltv/tree/main/src/views"
    click Tailwind "https://github.com/yonexdu0ra/qltv/blob/main/tailwind.config.js"
    click PublicRoutes "https://github.com/yonexdu0ra/qltv/tree/main/src/routes/public"
    click PrivateRoutes "https://github.com/yonexdu0ra/qltv/tree/main/src/routes/private"
    click AuthMW "https://github.com/yonexdu0ra/qltv/blob/main/src/middleware/authenticationMiddleware.js"
    click AuthzMW "https://github.com/yonexdu0ra/qltv/blob/main/src/middleware/authorizationMiddleware.js"
    click Controllers "https://github.com/yonexdu0ra/qltv/tree/main/src/controllers"
    click Services "https://github.com/yonexdu0ra/qltv/tree/main/src/services"
    click Repositories "https://github.com/yonexdu0ra/qltv/tree/main/src/repositories"
    click Models "https://github.com/yonexdu0ra/qltv/tree/main/src/models"
    click DB "https://github.com/yonexdu0ra/qltv/blob/main/src/config/database.js"
    click Redis "https://github.com/yonexdu0ra/qltv/blob/main/src/config/redisClient.js"
    click Cloudinary "https://github.com/yonexdu0ra/qltv/blob/main/src/config/cloudinary.js"
    click Multer "https://github.com/yonexdu0ra/qltv/blob/main/src/config/multer.js"
    click Utils "https://github.com/yonexdu0ra/qltv/tree/main/src/utils"
    click Seeders "https://github.com/yonexdu0ra/qltv/tree/main/src/seeders"
    click Entry "https://github.com/yonexdu0ra/qltv/blob/main/src/index.js"

    %% Styles
    classDef ui fill:#d0e7ff,stroke:#0366d6,stroke-width:2px
    classDef api fill:#cffaf4,stroke:#17a2b8,stroke-width:2px
    classDef middleware fill:#e0f7fa,stroke:#00838f,stroke-width:2px
    classDef controller fill:#d4edda,stroke:#28a745,stroke-width:2px
    classDef service fill:#d4edda,stroke:#218838,stroke-width:2px
    classDef repository fill:#fff3cd,stroke:#fd7e14,stroke-width:2px
    classDef db fill:#e2d6f3,stroke:#6f42c1,stroke-width:2px
    classDef cache fill:#e2d6f3,stroke:#6610f2,stroke-width:2px
    classDef external fill:#f0f0f0,stroke:#6c757d,stroke-width:2px,stroke-dasharray: 5 5
    classDef utility fill:#f8f9fa,stroke:#343a40,stroke-width:2px
```
