# sagspot/simple-ecommerce-api architecture

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

- Interactive diagram: https://gitdiagram.com/sagspot/simple-ecommerce-api
- Repository: https://github.com/sagspot/simple-ecommerce-api

## Overview

##### Main Components

a. Client Layer:
- External clients (mobile/web applications) that consume the API
- As the topmost layer

b. API Gateway Layer:
- Express.js server (index.js)
- Middleware components (auth.js, validation.js)
- Major endpoints groupings (users, products, orders)

c. Business Logic Layer:
- Controllers (userController.js, productController.js, orderController.js)
- Models (userModel.js, productModel.js, orderModel.js)

d. Database Layer:
- MongoDB database

##### Specific Implementation Details

**Client Applications**
- ↓
**[API Gateway (Express.js)]**

- [Middleware Layer]
- Authentication (auth.js)
- Validation (validation.js)

- [Routes Layer]
- /users
- /products
- /orders

- [Controllers Layer]
- UserController
- ProductController
- OrderController

- [Models Layer]
- UserModel
- ProductModel
- OrderModel

**[MongoDB Database]**

##### Additional Elements

a. Authentication Flow:
- JWT token flow
- Bcrypt password hashing

b. Technology Stack Annotations:
- Express.js
- Mongoose
- JWT
- Joi validation
- Morgan logging

##### Important Notes

- Notes about validation middleware using Joi
- Authentication flow with JWT
- Where Morgan logging occurs
- Routes that require authentication

## Diagram (Mermaid)

```mermaid
flowchart TB
    %% Title
    title[E-commerce API System Architecture v1.0.0]
    
    %% External Clients
    Client(("External Clients"))
    
    %% API Gateway
    Gateway["API Gateway (Express.js)"]:::gateway
    click Gateway "https://github.com/sagspot/simple-ecommerce-api/blob/master/index.js"
    
    %% Middleware Layer
    subgraph ML["Middleware Layer"]
        Auth["Authentication"]:::middleware
        Valid["Validation"]:::middleware
        Logger["Morgan Logger"]:::middleware
    end
    click Auth "https://github.com/sagspot/simple-ecommerce-api/blob/master/components/middlewares/auth.js"
    click Valid "https://github.com/sagspot/simple-ecommerce-api/blob/master/components/middlewares/validation.js"
    
    %% Routes Layer
    subgraph RL["Routes Layer"]
        UserRoutes["/users"]:::route
        ProductRoutes["/products"]:::route
        OrderRoutes["/orders"]:::route
    end
    click UserRoutes "https://github.com/sagspot/simple-ecommerce-api/blob/master/components/users/users.js"
    click ProductRoutes "https://github.com/sagspot/simple-ecommerce-api/blob/master/components/products/products.js"
    click OrderRoutes "https://github.com/sagspot/simple-ecommerce-api/blob/master/components/orders/orders.js"
    
    %% Controllers Layer
    subgraph CL["Controllers Layer"]
        UserController["User Controller"]:::controller
        ProductController["Product Controller"]:::controller
        OrderController["Order Controller"]:::controller
    end
    click UserController "https://github.com/sagspot/simple-ecommerce-api/blob/master/components/users/userController.js"
    click ProductController "https://github.com/sagspot/simple-ecommerce-api/blob/master/components/products/productController.js"
    click OrderController "https://github.com/sagspot/simple-ecommerce-api/blob/master/components/orders/orderController.js"
    
    %% Models Layer
    subgraph ML2["Models Layer"]
        UserModel["User Model"]:::model
        ProductModel["Product Model"]:::model
        OrderModel["Order Model"]:::model
    end
    click UserModel "https://github.com/sagspot/simple-ecommerce-api/blob/master/components/users/userModel.js"
    click ProductModel "https://github.com/sagspot/simple-ecommerce-api/blob/master/components/products/productModel.js"
    click OrderModel "https://github.com/sagspot/simple-ecommerce-api/blob/master/components/orders/orderModel.js"
    
    %% Database
    DB[(MongoDB)]:::database
    
    %% Connections
    Client --> Gateway
    Gateway --> ML
    Auth --> RL
    Valid --> RL
    Logger --> RL
    
    UserRoutes --> UserController
    ProductRoutes --> ProductController
    OrderRoutes --> OrderController
    
    UserController --> UserModel
    ProductController --> ProductModel
    OrderController --> OrderModel
    
    UserModel --> DB
    ProductModel --> DB
    OrderModel --> DB
    
    %% Styles
    classDef gateway fill:#3498db,stroke:#2980b9,color:white
    classDef middleware fill:#f1c40f,stroke:#f39c12,color:black
    classDef route fill:#3498db,stroke:#2980b9,color:white
    classDef controller fill:#2ecc71,stroke:#27ae60,color:white
    classDef model fill:#e67e22,stroke:#d35400,color:white
    classDef database fill:#9b59b6,stroke:#8e44ad,color:white
    
    %% Legend
    subgraph Legend
        L1["API Gateway"]:::gateway
        L2["Middleware"]:::middleware
        L3["Routes"]:::route
        L4["Controllers"]:::controller
        L5["Models"]:::model
        L6[(Database)]:::database
    end
```
