# leonardogit99/e-commerce_server architecture

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

- Interactive diagram: https://gitdiagram.com/leonardogit99/e-commerce_server
- Repository: https://github.com/leonardogit99/e-commerce_server

## Overview

##### Project type and purpose
- This is a TypeScript-based RESTful API server (“e-commerce_server”) designed to power an e-commerce application’s backend.
- It exposes HTTP endpoints for managing products, categories, subcategories, and dashboard metrics, persists data in an SQL database, and handles image uploads via Cloudinary.

##### File structure
- Top-level:
- package.json / tsconfig.json – project metadata and TS compiler settings
- database/ – SQL schema and initialization scripts
- src/ – main application code
- src/controllers – HTTP request handlers (one per resource)
- src/routes – Express routing definitions
- src/services – business logic and orchestrations (e.g., discounted price, image uploads)
- src/models & src/schemas – data models (ORM or query builders) and validation schemas
- src/utils – shared helpers (DB connection, Cloudinary client, parsers)
- src/index.ts – application bootstrap (Express app + middleware)

##### README
- Only a placeholder title (“e-commerce_server”) is present, confirming it as the backend component of an e-commerce system.

a. Main components
- Client / API Consumer (e.g., frontend SPA or mobile app)
- API Gateway / HTTP Server (Express in src/index.ts)
- Routing Layer (src/routes)
- Controller Layer (src/controllers)
- Service Layer (src/services)
- Data Access Layer (src/models + src/utils/db)
- Database (SQL server initialized via database/*.sql)
- External Service: Cloudinary (for image uploads)
b. Relationships & data flow
1. Client sends HTTP request → API Gateway
2. Gateway delegates to Routing Layer → matches a route file
3. Route invokes corresponding Controller method
4. Controller calls Service methods to execute business logic
5. Service uses Models / DB utils to read/write the SQL database
6. For images: Service calls Cloudinary client in utils/cloudinary.ts
7. Service returns result → Controller formats response → Gateway sends JSON back to Client
c. Architectural patterns & principles
– Layered architecture (Presentation → Application → Domain/Business → Data Access)
– Single-Responsibility per module (controllers vs. services vs. models)
– Dependency injection by passing database client or Cloudinary instance into services
– Validation via separate schema definitions (src/schemas)
d. Key technologies & libraries
– Node.js + Express (HTTP server, middleware)
– TypeScript (static typing, tsconfig.json)
– SQL database (scripts in database/)
– Cloudinary SDK (image service)
– JSON schema / custom validators (src/schemas)

##### Detailed capture of essential elements
- Do not oversimplify: break out each controller (ProductsController, CategoriesController, SubCategoriesController, DashboardController) if detail is needed.
- Separate route files mapping to controllers under the Routing layer.
- Utility functions like discountedPrice and parseToFormattedDate as side-notes in the Service layer.
- DB util module (src/utils/db.ts) as the bridge between the Service layer and the physical database.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Clients
    Client["Client (Frontend/Mobile)"]:::external

    %% API Gateway
    API_Gateway["Express HTTP Server<br/>(src/index.ts)"]:::gateway

    %% Routing Layer
    subgraph "Routing Layer"
        direction TB
        Routes_Index["index.ts"]:::router
        Products_Route["products.routes.ts"]:::router
        Categories_Route["categories.routes.ts"]:::router
        SubCategories_Route["subcategories.routes.ts"]:::router
        Dashboard_Route["dashboard.routes.ts"]:::router
    end

    %% Controller Layer
    subgraph "Controller Layer"
        direction TB
        Categories_Controller["CategoriesController"]:::controller
        Products_Controller["ProductsController"]:::controller
        SubCategories_Controller["SubCategoriesController"]:::controller
        Dashboard_Controller["DashboardController"]:::controller
    end

    %% Service Layer
    subgraph "Service Layer"
        direction TB
        Category_Service["category.service.ts"]:::service
        Product_Service["product.service.ts"]:::service
        SubCategory_Service["subCategory.service.ts"]:::service
        Image_Service["image.service.ts"]:::service
        Discount_Util["discountedPrice.ts"]:::util
        Date_Util["parseToFormattedDate.ts"]:::util
    end

    %% Data Access Layer
    subgraph "Data Access Layer"
        direction TB
        Category_Model["category.model.ts"]:::data
        Product_Model["product.model.ts"]:::data
        SubCategory_Model["subCategory.model.ts"]:::data
        DB_Util["db.ts"]:::data
    end

    %% Validation Schemas
    subgraph "Validation Schemas"
        direction TB
        Category_Schema["category.schema.ts"]:::schema
        Product_Schema["product.schema.ts"]:::schema
        SubCategory_Schema["subCategory.schema.ts"]:::schema
    end

    %% External Services & DB
    Cloudinary["Cloudinary Service"]:::external
    Database[(SQL Database)]:::database
    SQL_Scripts["database scripts<br/>(db.sql, init.sql)"]:::database

    %% Flow connections
    Client -->|HTTP Request| API_Gateway
    API_Gateway -->|uses routes| Routes_Index
    Routes_Index -->|imports| Products_Route
    Routes_Index -->|imports| Categories_Route
    Routes_Index -->|imports| SubCategories_Route
    Routes_Index -->|imports| Dashboard_Route

    Products_Route -->|calls| Products_Controller
    Categories_Route -->|calls| Categories_Controller
    SubCategories_Route -->|calls| SubCategories_Controller
    Dashboard_Route -->|calls| Dashboard_Controller

    Products_Controller -->|calls| Product_Service
    Categories_Controller -->|calls| Category_Service
    SubCategories_Controller -->|calls| SubCategory_Service
    Dashboard_Controller -->|calls| Product_Service

    Product_Service -->|CRUD via| Product_Model
    Category_Service -->|CRUD via| Category_Model
    SubCategory_Service -->|CRUD via| SubCategory_Model

    Image_Service -->|uploads via| Cloudinary
    Product_Service -->|invokes| Image_Service

    Product_Service -->|utilizes| Discount_Util
    Dashboard_Controller -->|formats| Date_Util

    Product_Model -->|query via| DB_Util
    Category_Model -->|query via| DB_Util
    SubCategory_Model -->|query via| DB_Util
    DB_Util -->|executes SQL| Database

    SQL_Scripts -->|initialize| Database

    API_Gateway -->|returns JSON| Client

    %% Click Events
    click API_Gateway "https://github.com/leonardogit99/e-commerce_server/blob/main/src/index.ts"
    click Routes_Index "https://github.com/leonardogit99/e-commerce_server/blob/main/src/routes/index.ts"
    click Products_Route "https://github.com/leonardogit99/e-commerce_server/blob/main/src/routes/products.routes.ts"
    click Categories_Route "https://github.com/leonardogit99/e-commerce_server/blob/main/src/routes/categories.routes.ts"
    click SubCategories_Route "https://github.com/leonardogit99/e-commerce_server/blob/main/src/routes/subcategories.routes.ts"
    click Dashboard_Route "https://github.com/leonardogit99/e-commerce_server/blob/main/src/routes/dashboard.routes.ts"
    click Categories_Controller "https://github.com/leonardogit99/e-commerce_server/blob/main/src/controllers/categories.controller.ts"
    click Products_Controller "https://github.com/leonardogit99/e-commerce_server/blob/main/src/controllers/products.controller.ts"
    click SubCategories_Controller "https://github.com/leonardogit99/e-commerce_server/blob/main/src/controllers/subCategories.controller.ts"
    click Dashboard_Controller "https://github.com/leonardogit99/e-commerce_server/blob/main/src/controllers/dashboard.controller.ts"
    click Category_Service "https://github.com/leonardogit99/e-commerce_server/blob/main/src/services/category.service.ts"
    click Product_Service "https://github.com/leonardogit99/e-commerce_server/blob/main/src/services/product.service.ts"
    click SubCategory_Service "https://github.com/leonardogit99/e-commerce_server/blob/main/src/services/subCategory.service.ts"
    click Image_Service "https://github.com/leonardogit99/e-commerce_server/blob/main/src/services/image.service.ts"
    click Category_Model "https://github.com/leonardogit99/e-commerce_server/blob/main/src/models/category.model.ts"
    click Product_Model "https://github.com/leonardogit99/e-commerce_server/blob/main/src/models/product.model.ts"
    click SubCategory_Model "https://github.com/leonardogit99/e-commerce_server/blob/main/src/models/subCategory.model.ts"
    click DB_Util "https://github.com/leonardogit99/e-commerce_server/blob/main/src/utils/db.ts"
    click Cloudinary "https://github.com/leonardogit99/e-commerce_server/blob/main/src/utils/cloudinary.ts"
    click Discount_Util "https://github.com/leonardogit99/e-commerce_server/blob/main/src/utils/discountedPrice.ts"
    click Date_Util "https://github.com/leonardogit99/e-commerce_server/blob/main/src/utils/parseToFormattedDate.ts"

    %% Styles
    classDef external fill:#f9f,stroke:#333,stroke-width:1px
    classDef gateway fill:#bbf,stroke:#333,stroke-width:1px
    classDef router fill:#ddf,stroke:#333,stroke-width:1px
    classDef controller fill:#bfb,stroke:#333,stroke-width:1px
    classDef service fill:#bdf,stroke:#333,stroke-width:1px
    classDef util fill:#ffeb99,stroke:#333,stroke-width:1px
    classDef data fill:#fbc,stroke:#333,stroke-width:1px
    classDef schema fill:#fc9,stroke:#333,stroke-width:1px
    classDef database fill:#ccc,stroke:#333,stroke-width:1px,stroke-dasharray: 5 5
```
