# nguyenthanhcong03/e-commerce architecture

> Architecture diagram of the GitHub repository nguyenthanhcong03/e-commerce, 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/nguyenthanhcong03/e-commerce
- Repository: https://github.com/nguyenthanhcong03/e-commerce

## Overview

##### Main Components
- Frontend (Client):
- Built using ReactJS with Vite, Tailwind CSS, and Redux for state management.
- Contains components for user interface elements (e.g., Auth, Breadcrumb, Button, Layout components like Header, Footer, SideBar, etc.).
- Organized into pages for both customer and admin functionalities (e.g., HomePage, DetailProduct, Admin Dashboard, ManageUser, etc.).
- Uses hooks and utility functions for custom behaviors (e.g., useScrollHandling, useTranslateXImage).
- Routing layer (client/src/routes) to manage navigation between pages.

- Backend (Server):
- Implements a RESTful API using Node.js.
- Follows an MVC-like pattern with separate folders for controllers, models, routes, services, middleware, and configuration.
- Controllers manage business logic for different aspects (authentication, products, customers, users).
- Models define the data schemas (e.g., product.js, user.js, customer.js) used by the database.
- Middleware handles authentication (JWT, verifyToken), error handling, validation, etc.
- Service layer for additional business logic and integrations (e.g., authService, customerService, emailService).
- Config folder contains settings for database connections (dbconnect) and mailing (mailer).
- Public folder for static assets like image uploads.

- External Services and Dependencies:
- Database: Although not explicitly named in the files, the presence of dbconnect.js suggests a connection to a database (likely MongoDB or SQL).
- Email Service: Configurations for mailing indicate integration with an email service.
- File Upload and Storage: Handled through specific routes for uploading files.

##### Relationships and Interactions
- Middleware processes within the server (e.g., authentication, validation, error handling) which intercept incoming requests and outgoing responses.
- Integration points such as the Email Service when a specific action (e.g., user registration or order confirmation) triggers an outgoing email.

##### Architectural Patterns and Principles
- Client-Side: Uses a component-based structure with separation between presentation components (UI components, pages) and state management (Redux store, slices for account, cart, etc.).
- Server-Side: Exhibits an MVC-like pattern (routes delegate to controllers, which then interact with models), with a layered approach (middleware → controllers → services/models).
- Separation of concerns, where each layer handles its specific role ensuring modularity and maintainability.

## Diagram (Mermaid)

```mermaid
flowchart TD
    subgraph "Client (Frontend)"
        CReact["React App"]:::client
        subgraph "Presentation Layer"
            Auth["Auth Component"]:::client
            Breadcrumb["Breadcrumb Component"]:::client
            Button["Button Component"]:::client
            Header["Header Layout"]:::client
            Footer["Footer Layout"]:::client
            SideBar["SideBar Layout"]:::client
        end
        subgraph "Routing and State Management"
            Routes["Routing Layer"]:::client
            Redux["Redux Store"]:::client
        end
        subgraph "Utilities and Hooks"
            Hooks["Custom Hooks"]:::client
        end
    end

    subgraph "Server (Backend)"
        subgraph "API Routes"
            AuthRoutes["Auth Routes"]:::backend
            CustomerRoutes["Customer Routes"]:::backend
            ProductRoutes["Product Routes"]:::backend
            UserRoutes["User Routes"]:::backend
            UploadRoutes["File Upload Routes"]:::backend
        end
        subgraph "Middleware"
            Middleware["Auth&JWT/Validator/ErrorHandler"]:::middleware
        end
        subgraph "Controllers"
            AuthCtrl["Auth Controller"]:::backend
            CustomerCtrl["Customer Controller"]:::backend
            ProductCtrl["Product Controller"]:::backend
            UserCtrl["User Controller"]:::backend
            APICtrl["Central API Controller"]:::backend
        end
        subgraph "Services"
            AuthService["Auth Service"]:::backend
            CustomerService["Customer Service"]:::backend
            UserService["User Service"]:::backend
            EmailService["Email Service"]:::backend
            FileService["File Service"]:::backend
        end
        subgraph "Models"
            CustomerModel["Customer Model"]:::backend
            ProductModel["Product Model"]:::backend
            UserModel["User Model"]:::backend
        end
        subgraph "Configurations"
            DBConfig["Database Connection"]:::backend
            MailConfig["Mailing Configuration"]:::backend
        end
    end

    subgraph "External Services"
        Database["Database"]:::external
        EmailExt["Email Service"]:::external
        FileStorage["File Storage"]:::external
    end

    %% Data Flow: Client to Server
    CReact -->|"API_call"| AuthRoutes
    CReact -->|"API_call"| CustomerRoutes
    CReact -->|"API_call"| ProductRoutes
    CReact -->|"API_call"| UserRoutes
    CReact -->|"File_upload"| UploadRoutes

    %% Client internal structure
    Auth -->|"part_of"| CReact
    Breadcrumb -->|"part_of"| CReact
    Button -->|"part_of"| CReact
    Header -->|"part_of"| CReact
    Footer -->|"part_of"| CReact
    SideBar -->|"part_of"| CReact
    Routes -->|"used_by"| CReact
    Redux -->|"state_management"| CReact
    Hooks -->|"utility_for"| CReact

    %% Server internal flow: Routes -> Middleware -> Controllers
    AuthRoutes --> Middleware
    CustomerRoutes --> Middleware
    ProductRoutes --> Middleware
    UserRoutes --> Middleware
    UploadRoutes --> Middleware
    Middleware --> AuthCtrl
    Middleware --> CustomerCtrl
    Middleware --> ProductCtrl
    Middleware --> UserCtrl
    Middleware --> APICtrl

    %% Controllers delegating to Services and Models
    AuthCtrl --> AuthService
    CustomerCtrl --> CustomerService
    ProductCtrl --> FileService
    UserCtrl --> UserService
    APICtrl --> AuthService

    AuthService --> CustomerModel
    CustomerService --> CustomerModel
    UserService --> UserModel
    ProductCtrl --> ProductModel

    %% Service interactions with external systems
    EmailService --> EmailExt
    FileService --> FileStorage

    %% Models connecting to Database
    CustomerModel --> Database
    ProductModel --> Database
    UserModel --> Database

    %% Configurations providing settings to external services
    DBConfig --> Database
    MailConfig --> EmailExt

    %% Click Events for Frontend Components
    click CReact "https://github.com/nguyenthanhcong03/e-commerce/tree/main/client/"
    click Auth "https://github.com/nguyenthanhcong03/e-commerce/blob/main/client/src/components/Auth/Auth.jsx"
    click Breadcrumb "https://github.com/nguyenthanhcong03/e-commerce/blob/main/client/src/components/Breadcrumb/Breadcrumb.jsx"
    click Button "https://github.com/nguyenthanhcong03/e-commerce/blob/main/client/src/components/Button/Button.jsx"
    click Header "https://github.com/nguyenthanhcong03/e-commerce/blob/main/client/src/components/Layout/Header/Header.jsx"
    click Footer "https://github.com/nguyenthanhcong03/e-commerce/blob/main/client/src/components/Layout/Footer/Footer.jsx"
    click SideBar "https://github.com/nguyenthanhcong03/e-commerce/blob/main/client/src/components/Layout/SideBar/SideBar.jsx"
    click Routes "https://github.com/nguyenthanhcong03/e-commerce/blob/main/client/src/routes/routes.js"
    click Hooks "https://github.com/nguyenthanhcong03/e-commerce/tree/main/client/src/hooks"
    click Redux "https://github.com/nguyenthanhcong03/e-commerce/tree/main/client/src/redux/"

    %% Click Events for Server Controllers
    click AuthCtrl "https://github.com/nguyenthanhcong03/e-commerce/blob/main/server/controllers/authController.js"
    click CustomerCtrl "https://github.com/nguyenthanhcong03/e-commerce/blob/main/server/controllers/customerController.js"
    click ProductCtrl "https://github.com/nguyenthanhcong03/e-commerce/blob/main/server/controllers/productController.js"
    click UserCtrl "https://github.com/nguyenthanhcong03/e-commerce/blob/main/server/controllers/userController.js"
    click APICtrl "https://github.com/nguyenthanhcong03/e-commerce/blob/main/server/controllers/apiController.js"

    %% Click Events for Data Models
    click CustomerModel "https://github.com/nguyenthanhcong03/e-commerce/blob/main/server/models/customer.js"
    click ProductModel "https://github.com/nguyenthanhcong03/e-commerce/blob/main/server/models/product.js"
    click UserModel "https://github.com/nguyenthanhcong03/e-commerce/blob/main/server/models/user.js"

    %% Click Events for Middleware and API Routes
    click Middleware "https://github.com/nguyenthanhcong03/e-commerce/blob/main/server/middlewares/auth.js"
    click AuthRoutes "https://github.com/nguyenthanhcong03/e-commerce/blob/main/server/routes/authRoutes.js"
    click CustomerRoutes "https://github.com/nguyenthanhcong03/e-commerce/blob/main/server/routes/customerRoutes.js"
    click ProductRoutes "https://github.com/nguyenthanhcong03/e-commerce/blob/main/server/routes/productRoutes.js"
    click UserRoutes "https://github.com/nguyenthanhcong03/e-commerce/blob/main/server/routes/userRoutes.js"
    click UploadRoutes "https://github.com/nguyenthanhcong03/e-commerce/blob/main/server/routes/uploadFileRoutes.js"

    %% Click Events for Services
    click AuthService "https://github.com/nguyenthanhcong03/e-commerce/blob/main/server/services/authService.js"
    click CustomerService "https://github.com/nguyenthanhcong03/e-commerce/blob/main/server/services/customerService.js"
    click UserService "https://github.com/nguyenthanhcong03/e-commerce/blob/main/server/services/userService.js"
    click EmailService "https://github.com/nguyenthanhcong03/e-commerce/blob/main/server/services/emailService.js"
    click FileService "https://github.com/nguyenthanhcong03/e-commerce/blob/main/server/services/fileService.js"

    %% Click Events for Configuration Settings
    click DBConfig "https://github.com/nguyenthanhcong03/e-commerce/blob/main/server/config/dbconnect.js"
    click MailConfig "https://github.com/nguyenthanhcong03/e-commerce/blob/main/server/config/mailer.js"

    %% Click Event for File Upload Storage
    click FileStorage "https://github.com/nguyenthanhcong03/e-commerce/tree/main/server/public/images/upload"

    %% Styling Classes
    classDef client fill:#9ecae1,stroke:#2c3e50,stroke-width:2px;
    classDef backend fill:#a1d99b,stroke:#2c3e50,stroke-width:2px;
    classDef middleware fill:#fdd49e,stroke:#d95f02,stroke-width:2px;
    classDef external fill:#d9d9d9,stroke:#636363,stroke-width:2px;
```
