# hoangphuc101202/commerce-be architecture

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

## Overview

##### Main Components
- Client/API Consumers: Represent external clients (web, mobile, admin panels, etc.) that interact with the API.
- API Layer (Controllers): Includes controllers (CategoriesController, InvoiceController, PaymentController, ProductsController, SupplierController, UsersController) that expose endpoints to the clients.
- Business Logic (Services): Represents the service layer where implementations like CategoriesService, InvoiceService, PaymentService, ProductsService, SupplierService, and UserService encapsulate business logic. Interfaces for contracts in the Services/Interface folder highlight abstraction.
- Data Access Layer (Repositories): Contains repository implementations (CategoryRepository, InvoiceRepository, etc.) and their corresponding interfaces. This layer abstracts the database interactions using Entity Framework Core.
- Database and ORM: SQL Server backed by Entity Framework Core. Migrations in the Migrations folder indicate changes managed by EF migrations.
- Infrastructure: Includes various cross-cutting concerns such as Dependency Injection (ServiceCollectionExtension), Authentication (TokenProvider, MailService), Mapping (MappingProfile), and Password security (PasswordHasher).
- Middleware: Custom middleware components (API Response formatting, Exception Handling, Unauthorized/Forbidden handling) that manage cross-cutting API concerns.
- Exception Handling: The custom HTTP exceptions and handling classes ensure that errors are managed consistently across the system.
- External Services & Caching: Although not extensively detailed in the file tree, the README notes Redis for caching and Docker for deployment. Also, JWT Authentication is used for securing the APIs.

##### Relationships and Interactions
- Client requests hit the API controllers where they are first processed by the middleware pipeline (error handling, authentication/authorization).
- Controllers then delegate requests to the corresponding service implementation for business logic.
- Services interact with repositories (data access layer) to perform CRUD operations or complex interactions with the database.
- The database (SQL Server) is managed via Entity Framework Core along with migration files.
- External integrations such as email service (via MailService) and token generation (TokenProvider) facilitate authentication and notification functionalities.
- Docker and docker-compose are used for containerizing the application and possibly orchestrating multiple containers (like the API, SQL Server, and Redis caching).

##### Architectural Patterns and Design Principles
- Layered Architecture: The project is clearly divided into Controllers, Services, and Repositories.
- Dependency Injection: Facilitated through extension methods under DependencyInjection and used across the application for loose coupling.
- Separation of Concerns: Each directory isolates specific responsibilities – controllers handle HTTP, services encapsulate business logic, and repositories abstract data persistence.
- Custom Middleware: Enhances modular error handling, logging, and authorization mechanisms.
- DTOs and Auto-mapping: The Model/DTO folder indicates the use of Data Transfer Objects and mapping profiles to decouple the domain models from the API contracts.
- Use of Docker Containerization: Indicates a focus on repeatable deployments and scalability.

##### Relevant Technologies and Frameworks
- .NET 8.0 and C# are used for application building.
- Entity Framework Core for ORM and database migrations.
- SQL Server as the primary database.
- Swagger for API documentation.
- JWT for authentication, along with custom token providers.
- Redis for caching.
- Docker for containerization.
- Third-party frameworks/libraries like Serilog for logging and possibly AutoMapper (inferred by MappingProfile).

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% External Clients
    Clients["External Clients"]:::external

    %% Middleware Layer
    subgraph "Middleware"
        M1["ApiResponse"]:::middleware
        M2["ForbidenMiddleware"]:::middleware
        M3["HttpExceptionHandlingMiddleware"]:::middleware
        M4["UnauthorizeMiddleware"]:::middleware
    end

    %% API Layer (Controllers)
    subgraph "API Layer (Controllers)"
        C1["CategoriesController"]:::controller
        C2["InvoiceController"]:::controller
        C3["PaymentController"]:::controller
        C4["ProductsController"]:::controller
        C5["SupplierController"]:::controller
        C6["UsersController"]:::controller
    end

    %% Business Logic (Services)
    subgraph "Business Logic (Services)"
        S1["CategoriesService"]:::service
        S2["InvoiceService"]:::service
        S3["PaymentService"]:::service
        S4["ProductsService"]:::service
        S5["SupplierService"]:::service
        S6["UserService"]:::service
    end

    %% Data Access Layer (Repositories)
    subgraph "Data Access Layer (Repositories)"
        R1["CategoryRepository"]:::repository
        R2["InvoiceRepository"]:::repository
        R3["PaymentRepository"]:::repository
        R4["ProductRepository"]:::repository
        R5["SupplierRepository"]:::repository
        R6["UserRepository"]:::repository
    end

    %% Database and ORM
    subgraph "Database and ORM"
        DB1["ApplicationDBContext"]:::db
        DB2["Migrations"]:::db
    end

    %% Infrastructure Components
    subgraph "Infrastructure Components"
        I1["TokenProvider"]:::infrastructure
        I2["MailService"]:::infrastructure
        I3["ServiceCollectionExtension"]:::infrastructure
        I4["MappingProfile"]:::infrastructure
        I5["PasswordHasher"]:::infrastructure
    end

    %% Exception Handling
    subgraph "Exception Handling"
        E1["BadRequestHttpException"]:::exception
        E2["InternalServerErrorHttpException"]:::exception
        E3["NotFoundHttpException"]:::exception
        E4["HttpException"]:::exception
    end

    %% External Services
    subgraph "External Services"
        EX1["Redis Caching"]:::external
        EX2["Docker Container"]:::external
    end

    %% Connections: Request Flow
    Clients -->|"request"| M3
    M3 -->|"processes"| C1
    M3 --> C2
    M3 --> C3
    M3 --> C4
    M3 --> C5
    M3 --> C6

    %% Controllers delegate to Services
    C1 -->|"calls"| S1
    C2 -->|"calls"| S2
    C3 -->|"calls"| S3
    C4 -->|"calls"| S4
    C5 -->|"calls"| S5
    C6 -->|"calls"| S6

    %% Services interact with Repositories
    S1 -->|"data"| R1
    S2 -->|"data"| R2
    S3 -->|"data"| R3
    S4 -->|"data"| R4
    S5 -->|"data"| R5
    S6 -->|"data"| R6

    %% Repositories interact with Database
    R1 -->|"CRUD"| DB1
    R2 --> DB1
    R3 --> DB1
    R4 --> DB1
    R5 --> DB1
    R6 --> DB1

    %% Database includes migrations
    DB1 -->|"managedBy"| DB2

    %% Cross-cutting Infrastructure interactions (dotted lines)
    S6 -.->|"auth"| I1
    S6 -.->|"notify"| I2
    S1 -.->|"mapping"| I4
    S2 -.->|"DI"| I3
    S6 -.->|"security"| I5

    %% Middleware handles exceptions
    M3 -.->|"handles"| E2
    M3 -.-> E1

    %% External integrations
    S3 -.->|"cache"| EX1
    Clients -.->|"containerized"| EX2

    %% Click Events for API Layer (Controllers)
    click C1 "https://github.com/hoangphuc101202/commerce-be/blob/develop/Restapi-net8/Controllers/CategoriesController.cs"
    click C2 "https://github.com/hoangphuc101202/commerce-be/blob/develop/Restapi-net8/Controllers/InvoiceController.cs"
    click C3 "https://github.com/hoangphuc101202/commerce-be/blob/develop/Restapi-net8/Controllers/PaymentController.cs"
    click C4 "https://github.com/hoangphuc101202/commerce-be/blob/develop/Restapi-net8/Controllers/ProductsController.cs"
    click C5 "https://github.com/hoangphuc101202/commerce-be/blob/develop/Restapi-net8/Controllers/SupplierController.cs"
    click C6 "https://github.com/hoangphuc101202/commerce-be/blob/develop/Restapi-net8/Controllers/UsersController.cs"

    %% Click Events for Business Logic (Services)
    click S1 "https://github.com/hoangphuc101202/commerce-be/blob/develop/Restapi-net8/Services/Implementation/CategoriesService.cs"
    click S2 "https://github.com/hoangphuc101202/commerce-be/blob/develop/Restapi-net8/Services/Implementation/InvoiceService.cs"
    click S3 "https://github.com/hoangphuc101202/commerce-be/blob/develop/Restapi-net8/Services/Implementation/PaymentService.cs"
    click S4 "https://github.com/hoangphuc101202/commerce-be/blob/develop/Restapi-net8/Services/Implementation/ProductsService.cs"
    click S5 "https://github.com/hoangphuc101202/commerce-be/blob/develop/Restapi-net8/Services/Implementation/SupplierService.cs"
    click S6 "https://github.com/hoangphuc101202/commerce-be/blob/develop/Restapi-net8/Services/Implementation/UserService.cs"

    %% Click Events for Data Access Layer (Repositories)
    click R1 "https://github.com/hoangphuc101202/commerce-be/blob/develop/Restapi-net8/Repository/Implementation/CategoryRepository.cs"
    click R2 "https://github.com/hoangphuc101202/commerce-be/blob/develop/Restapi-net8/Repository/Implementation/InvoiceRepository.cs"
    click R3 "https://github.com/hoangphuc101202/commerce-be/blob/develop/Restapi-net8/Repository/Implementation/PaymentRepository.cs"
    click R4 "https://github.com/hoangphuc101202/commerce-be/blob/develop/Restapi-net8/Repository/Implementation/ProductRepository.cs"
    click R5 "https://github.com/hoangphuc101202/commerce-be/blob/develop/Restapi-net8/Repository/Implementation/SupplierRepository.cs"
    click R6 "https://github.com/hoangphuc101202/commerce-be/blob/develop/Restapi-net8/Repository/Implementation/UserRepository.cs"

    %% Click Events for Database and ORM
    click DB1 "https://github.com/hoangphuc101202/commerce-be/tree/develop/Restapi-net8/Data"
    click DB2 "https://github.com/hoangphuc101202/commerce-be/tree/develop/Restapi-net8/Migrations"

    %% Click Events for Infrastructure Components
    click I1 "https://github.com/hoangphuc101202/commerce-be/blob/develop/Restapi-net8/Infrastructure/Authentication/TokenProvider.cs"
    click I2 "https://github.com/hoangphuc101202/commerce-be/blob/develop/Restapi-net8/Infrastructure/Authentication/MailService.cs"
    click I3 "https://github.com/hoangphuc101202/commerce-be/blob/develop/Restapi-net8/Infrastructure/DependencyInjection/ServiceCollectionExtension.cs"
    click I4 "https://github.com/hoangphuc101202/commerce-be/blob/develop/Restapi-net8/Infrastructure/Mapping/MappingProfile.cs"
    click I5 "https://github.com/hoangphuc101202/commerce-be/blob/develop/Restapi-net8/Infrastructure/Password/PasswordHasher.cs"

    %% Click Events for Middleware
    click M1 "https://github.com/hoangphuc101202/commerce-be/blob/develop/Restapi-net8/Middlewares/ApiResponse.cs"
    click M2 "https://github.com/hoangphuc101202/commerce-be/blob/develop/Restapi-net8/Middlewares/ForbidenMiddleware.cs"
    click M3 "https://github.com/hoangphuc101202/commerce-be/blob/develop/Restapi-net8/Middlewares/HttpExceptionHandlingMiddleware.cs"
    click M4 "https://github.com/hoangphuc101202/commerce-be/blob/develop/Restapi-net8/Middlewares/UnauthorizeMiddleware.cs"

    %% Click Events for Exception Handling
    click E1 "https://github.com/hoangphuc101202/commerce-be/blob/develop/Restapi-net8/Exceptions/Http/BadRequestHttpException.cs"
    click E2 "https://github.com/hoangphuc101202/commerce-be/blob/develop/Restapi-net8/Exceptions/Http/InternalServerErrorHttpException.cs"
    click E3 "https://github.com/hoangphuc101202/commerce-be/blob/develop/Restapi-net8/Exceptions/Http/NotFoundHttpException.cs"
    click E4 "https://github.com/hoangphuc101202/commerce-be/blob/develop/Restapi-net8/Exceptions/Http/HttpException.cs"

    %% Styles
    classDef external fill:#FEE,stroke:#900,stroke-width:2px;
    classDef controller fill:#BBF,stroke:#333,stroke-width:2px;
    classDef service fill:#BFB,stroke:#333,stroke-width:2px;
    classDef repository fill:#FFB,stroke:#333,stroke-width:2px;
    classDef db fill:#FBB,stroke:#333,stroke-width:2px;
    classDef middleware fill:#FBF,stroke:#333,stroke-width:2px;
    classDef infrastructure fill:#BFF,stroke:#333,stroke-width:2px;
    classDef exception fill:#DDD,stroke:#333,stroke-width:2px;
```
