# codergogoi/go-ecommerce-series architecture

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

- Interactive diagram: https://gitdiagram.com/codergogoi/go-ecommerce-series
- Repository: https://github.com/codergogoi/go-ecommerce-series

## Overview

##### Main Components
- Entry Point & Configuration: The main.go file acts as the entry point, with additional configuration support provided in the config/ directory and docker-compose.yml for container orchestration.
- API Layer (HTTP & REST): Under internal/api/rest and its handlers (catalogHandler.go, transactionHandler.go, userHandler.go), this layer handles incoming HTTP requests. Include the httpHandler.go and response.go for request routing and response formulation.
- Domain Models: Found in internal/domain, these files (Address.go, BankAccount.go, Cart.go, Category.go, Order.go, OrderItem.go, Payment.go, Product.go, User.go) represent core business entities.
- Data Transfer Objects (DTOs): Located in internal/dto, this layer abstracts request and response data (for example, ShoppingRequestDto.go, categoryRequestDto.go, produstRequestDto.go, etc.).
- Repositories: Under internal/repository, these files (catalogRepository.go, transactionRepository.go, userRepository.go) handle data persistence and retrieval; they abstract the interactions with the database or any storage.
- Services: In internal/service, business logic is implemented (e.g., catalogService.go, transactionService.go, userService.go) to process the business use cases.
- Helper/Utility Functions: The internal/helper directory (including auth.go and utility.go) provides support functions such as authentication and common utilities.
- External Integration: The pkg/notification directory (sendSms.go) indicates integration with external services, such as sending SMS notifications (e.g., via Twilio as referenced in the README).

##### Establish Relationships and Data Flow
- The API server (internal/api/server.go along with rest/httpHandler.go) listens for incoming HTTP requests. Each request is directed to a specific REST handler (catalogHandler, transactionHandler, userHandler).
- Each handler delegates work to the respective Service layer (catalogService, transactionService, userService), which contains the business logic.
- The Service layer calls upon the Repository layer to manage data persistence and retrieval.
- Domain models are used both within the Repository and Service layers to represent business entities. Data sent over the API is encapsulated in DTOs.
- Auxiliary helper functions (internal/helper) support these processes—such as handling authentication.
- On the notifications front, the pkg/notification component suggests integration points for external communication (e.g., sending SMS after certain actions).

##### Architectural Patterns and Design Principles to Emphasize
- Clean Separation of Concerns: There’s a clear separation between the API (HTTP), business logic (Service), domain models, data access (Repository), and auxiliary functionality.
- Layered Architecture: The structure follows a layered approach where high-level components (API) rely on mid-tier services and lower-level data repositories.
- Modularity & Extensibility: The use of DTOs, modular handlers, and service components allows for clean extensions (e.g., additional endpoints, services, or integrations).

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Client Layer
    Client("Client (Postman/UI)"):::client

    %% Entry Layer
    subgraph "Entry Layer"
        main("main.go"):::entry
        config("Configuration (config/)"):::entry
        docker("docker-compose.yml"):::entry
    end

    %% API Layer
    subgraph "API Layer"
        server("API Server (internal/api/server.go)"):::api
        httpHandler("HTTP Handler (internal/api/rest/httpHandler.go)"):::api
        response("Response (internal/api/rest/response.go)"):::api
        subgraph "HTTP Handlers"
            catalogH("Catalog Handler (catalogHandler.go)"):::api
            transactionH("Transaction Handler (transactionHandler.go)"):::api
            userH("User Handler (userHandler.go)"):::api
        end
    end

    %% Service Layer
    subgraph "Service Layer"
        catalogS("Catalog Service (catalogService.go)"):::service
        transactionS("Transaction Service (transactionService.go)"):::service
        userS("User Service (userService.go)"):::service
    end

    %% Repository Layer
    subgraph "Repository Layer"
        catalogR("Catalog Repository (catalogRepository.go)"):::repository
        transactionR("Transaction Repository (transactionRepository.go)"):::repository
        userR("User Repository (userRepository.go)"):::repository
    end

    %% Domain Models
    subgraph "Domain Models"
        domain("Domain Models (internal/domain)"):::domain
    end

    %% DTOs
    subgraph "Data Transfer Objects"
        dto("DTOs (internal/dto)"):::dto
    end

    %% Helper/Utility Functions
    subgraph "Helper/Utility"
        helperInternal("Helper Functions (internal/helper)"):::helper
        helperRoot("Project Helper (helper.go)"):::helper
    end

    %% External Integration
    subgraph "External Integration"
        notification("Notification (pkg/notification)"):::external
    end

    %% Connections
    Client -->|"sends_request"| server
    main -->|"initializes"| config
    main -->|"starts"| server
    server -->|"routes_to"| httpHandler
    httpHandler -->|"formulates"| response
    httpHandler -->|"delegates_to"| catalogH
    httpHandler -->|"delegates_to"| transactionH
    httpHandler -->|"delegates_to"| userH

    catalogH -->|"calls"| catalogS
    transactionH -->|"calls"| transactionS
    userH -->|"calls"| userS

    catalogS -->|"uses"| catalogR
    transactionS -->|"uses"| transactionR
    userS -->|"uses"| userR

    catalogS -->|"maps_with"| domain
    transactionS -->|"maps_with"| domain
    userS -->|"maps_with"| domain

    catalogS -->|"transfers_data_with"| dto
    transactionS -->|"transfers_data_with"| dto
    userS -->|"transfers_data_with"| dto

    catalogS -->|"utilizes"| helperInternal
    transactionS -->|"utilizes"| helperInternal
    userS -->|"utilizes"| helperInternal

    helperInternal -->|"supports"| helperRoot

    transactionS -->|"triggers"| notification

    docker -->|"deploys"| main

    %% Click Events
    click main "https://github.com/codergogoi/go-ecommerce-series/blob/main/main.go"
    click config "https://github.com/codergogoi/go-ecommerce-series/tree/main/config/"
    click docker "https://github.com/codergogoi/go-ecommerce-series/blob/main/docker-compose.yml"
    click server "https://github.com/codergogoi/go-ecommerce-series/blob/main/internal/api/server.go"
    click httpHandler "https://github.com/codergogoi/go-ecommerce-series/blob/main/internal/api/rest/httpHandler.go"
    click response "https://github.com/codergogoi/go-ecommerce-series/blob/main/internal/api/rest/response.go"
    click catalogH "https://github.com/codergogoi/go-ecommerce-series/blob/main/internal/api/rest/handlers/catalogHandler.go"
    click transactionH "https://github.com/codergogoi/go-ecommerce-series/blob/main/internal/api/rest/handlers/transactionHandler.go"
    click userH "https://github.com/codergogoi/go-ecommerce-series/blob/main/internal/api/rest/handlers/userHandler.go"
    click catalogS "https://github.com/codergogoi/go-ecommerce-series/blob/main/internal/service/catalogService.go"
    click transactionS "https://github.com/codergogoi/go-ecommerce-series/blob/main/internal/service/transactionService.go"
    click userS "https://github.com/codergogoi/go-ecommerce-series/blob/main/internal/service/userService.go"
    click catalogR "https://github.com/codergogoi/go-ecommerce-series/blob/main/internal/repository/catalogRepository.go"
    click transactionR "https://github.com/codergogoi/go-ecommerce-series/blob/main/internal/repository/transactionRepository.go"
    click userR "https://github.com/codergogoi/go-ecommerce-series/blob/main/internal/repository/userRepository.go"
    click domain "https://github.com/codergogoi/go-ecommerce-series/tree/main/internal/domain"
    click dto "https://github.com/codergogoi/go-ecommerce-series/tree/main/internal/dto"
    click helperInternal "https://github.com/codergogoi/go-ecommerce-series/tree/main/internal/helper"
    click helperRoot "https://github.com/codergogoi/go-ecommerce-series/blob/main/helper.go"
    click notification "https://github.com/codergogoi/go-ecommerce-series/blob/main/pkg/notification/sendSms.go"

    %% Styles
    classDef entry fill:#FFCC00,stroke:#000,stroke-width:2px;
    classDef api fill:#CCFFCC,stroke:#000,stroke-width:2px;
    classDef service fill:#99CCFF,stroke:#000,stroke-width:2px;
    classDef repository fill:#FF99CC,stroke:#000,stroke-width:2px;
    classDef domain fill:#FFCCCC,stroke:#000,stroke-width:2px;
    classDef dto fill:#CCCCFF,stroke:#000,stroke-width:2px;
    classDef helper fill:#FFFFCC,stroke:#000,stroke-width:2px;
    classDef external fill:#CCFFFF,stroke:#000,stroke-width:2px;
    classDef client fill:#D9EAD3,stroke:#000,stroke-width:2px;
```
