# tuandangvan/learn-math-api architecture

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

- Interactive diagram: https://gitdiagram.com/tuandangvan/learn-math-api
- Repository: https://github.com/tuandangvan/learn-math-api

## Overview

##### Main Components
- API Routes & Endpoints – Look under the src/routes/v1 folder. This is where all the endpoint definitions for accounts, chapters, classes, documents, exams, tests, uploads, and users reside.
- Controllers – Found under src/controllers. They process incoming requests and are the entry point for business logic.
- Services – In the src/services directory. Each service (e.g., accountService.js, examService.js) encapsulates the main business logic.
- Models – Under src/models. They represent database schemas (likely using MongoDB, as suggested by src/config/mongodb.js).
- Middlewares – Located in src/middlewares. These include authentication, authorization, and error handling that intercept requests before they reach controllers.
- Configurations – In src/config (e.g., cors.js, environment.js, mongodb.js), define core settings for the application.
- External Integrations – The src/sendEmail directory for handling email services and src/upload for cloud integration (e.g., uploadCloudinary.js).

##### Relationships and Data Flow
- Routes forward requests to the Controllers.
- Controllers then invoke appropriate Services for processing business logic.
- Services interact with Models which handle direct communication with the MongoDB database.
- Middlewares (authentication, authorization, error handling) wrap or sit in between these flows to enforce security and manage errors.

##### Architectural Patterns and Design Principles
- Layered/MVC Architecture – Separate concerns into routes (API layer), controllers (presentation logic), services (business logic), and models (data logic).
- Middleware Pattern – A series of functions (authentication, authorization, error handling) that intercept and process requests.
- External Services Integration – Clear points where external systems (email and file storage/cloud services) are integrated.
- Configuration Management – A dedicated configuration layer to manage settings like CORS, environment variables, and database connections.
- Utility Modules – Common utilities in src/utils are a supporting layer for various functionalities (e.g., token handling, constants, enums).

a. A “Client” block representing external consumers.
b. An “API Gateway/Routes” block showing all the endpoints (accounts, chapters, classes, etc.).
e. From the Service Layer, connect to a “Database Layer” block (representing MongoDB integration via models).
g. Integration points for “External Services” (Email and Cloudinary) connected to Controllers/Services.
h. A “Configuration” block feeding into multiple components (Controllers, Services, DB connection).

##### Final Touches
- Components are clearly labeled and explained.

## Diagram (Mermaid)

```mermaid
graph TD
    subgraph "Client Layer"
        client["Client"]:::client
    end

    subgraph "API Layer"
        apiRoutes["API Routes & Endpoints"]:::apiRoutes
        middlewares["Middlewares"]:::middlewares
    end

    subgraph "Application Layer"
        controllers["Controllers"]:::controllers
        services["Services"]:::services
    end

    subgraph "Data Layer"
        models["Models (MongoDB)"]:::models
    end

    subgraph "Infrastructure & Utilities"
        configurations["Configurations"]:::configurations
        utilities["Utility Modules"]:::utilities
        emailService["Email Service"]:::external
        cloudUpload["Cloud Upload"]:::external
    end

    %% Data Flow Connections
    client -->|"sendsRequest"| apiRoutes
    apiRoutes -->|"appliesMiddleware"| middlewares
    middlewares -->|"forwardsRequest"| controllers
    controllers -->|"callsBusinessLogic"| services
    services -->|"queriesData"| models

    %% External Integrations Connections
    controllers -->|"invokes"| emailService
    controllers -->|"invokes"| cloudUpload
    services -->|"invokes"| emailService
    services -->|"invokes"| cloudUpload

    %% Configuration Influence Connections
    configurations -->|"configures"| apiRoutes
    configurations -->|"configures"| controllers
    configurations -->|"configures"| services
    configurations -->|"configures"| models

    %% Utility Support
    utilities -->|"supports"| controllers
    utilities -->|"supports"| services

    %% Click Events
    click apiRoutes "https://github.com/tuandangvan/learn-math-api/tree/main/src/routes/v1"
    click middlewares "https://github.com/tuandangvan/learn-math-api/tree/main/src/middlewares"
    click controllers "https://github.com/tuandangvan/learn-math-api/tree/main/src/controllers"
    click services "https://github.com/tuandangvan/learn-math-api/tree/main/src/services"
    click models "https://github.com/tuandangvan/learn-math-api/tree/main/src/models"
    click configurations "https://github.com/tuandangvan/learn-math-api/tree/main/src/config"
    click emailService "https://github.com/tuandangvan/learn-math-api/blob/main/src/sendEmail/emailService.js"
    click cloudUpload "https://github.com/tuandangvan/learn-math-api/blob/main/src/upload/uploadCloudinary.js"
    click utilities "https://github.com/tuandangvan/learn-math-api/tree/main/src/utils"

    %% Style Definitions
    classDef client fill:#b3f0ff,stroke:#000,stroke-width:2px;
    classDef apiRoutes fill:#cce5ff,stroke:#333,stroke-width:2px;
    classDef controllers fill:#d1e7dd,stroke:#333,stroke-width:2px;
    classDef services fill:#fce5cd,stroke:#333,stroke-width:2px;
    classDef models fill:#f8d7da,stroke:#333,stroke-width:2px;
    classDef middlewares fill:#fff3cd,stroke:#333,stroke-width:2px;
    classDef configurations fill:#e2e3e5,stroke:#333,stroke-width:2px;
    classDef external fill:#ffd1dc,stroke:#333,stroke-width:2px;
    classDef utilities fill:#d1c4e9,stroke:#333,stroke-width:2px;
```
