# william-le1004/dtp-backend architecture

> Architecture diagram of the GitHub repository william-le1004/dtp-backend, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-05-23). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/william-le1004/dtp-backend
- Repository: https://github.com/william-le1004/dtp-backend

## Overview

##### Project type and purpose
- This is a RESTful, backend-only API built in ASP.NET Core 8 using Clean (Onion/Hexagonal) Architecture.
- It provides user authentication, role-based authorization, user management, ordering and payment features, caching via Redis, background jobs via Hangfire, and integration events via a message bus.

##### File structure
- Top-level layers:
– API (Presentation)
– Application (Business Logic & CQRS/MediatR)
– Domain (Entities, Value Objects, Domain Events)
– Infrastructure (EF Core DbContexts, Repositories, External Services)
- Key infra services: RedisCache, Cloudinary, Firebase (FCM), JWT token service, Hangfire job services, EventBus
- Persistence: MySQL via DtpDbContext, EF Core Migrations
- Middleware pipeline: ErrorHandling, JWTBlacklist, Transaction

##### README for insights
- Confirms Clean Architecture, CQRS (MediatR), EF Core, Redis, Docker Compose orchestrating API + Redis + MySQL
- Mentions Swagger, role-based access control, Docker setup

a. Main components
– API Layer
- HTTP Clients → API Gateway (ASP.NET Core WebHost)
- Middleware chain → Authentication, Authorization, ErrorHandling, Transaction
- Controllers (Authentication, User, Tour, Order, Payment, Wallet, etc.)
– Application Layer
- MediatR pipeline → Commands & Queries → Domain Events → Integration Events
- DTOs, Validation, Mapping
– Domain Layer
- Entities, ValueObjects, DomainEvents
– Infrastructure Layer
- Persistence
· EF Core DbContext → MySQL database
· Repositories implementing I*Repository interfaces
- Caching
· Redis cache service → Redis server
- External Services
· Cloudinary (media uploads)
· Firebase FCM (notifications)
· EventBus (message broker, e.g., RabbitMQ or similar)
· Hangfire (job scheduling & background workers)
– External Data Stores & Services
- MySQL DB container
- Redis container
- Docker Engine hosting all containers

b. Relationships & data flows
1. Client HTTP → API Gateway
2. Middleware validates token, enforces roles, handles errors
3. Controller receives request → delegates to Application via MediatR
4. Command/Query Handler loads/stores Domain Entities through Repository → DbContext → MySQL
5. Handler may raise DomainEvent → Infrastructure’s EventBus publishes IntegrationEvent
6. InfraServices call Redis, Cloudinary, Firebase as needed
7. Hangfire jobs run in background (triggered by EventBus or scheduled)
8. Response travels back up chain → client

c. Architectural patterns & principles
– Clean Architecture / Onion / Hexagonal: Dependency Inversion between layers
– CQRS via MediatR for separation of reads (Queries) and writes (Commands)
– Repository Pattern and Unit of Work via EF Core DbContext
– Domain Events with integration to a messaging bus
– Middleware Pipeline for cross-cutting concerns (error handling, auth, transactions)
– Docker Compose for containerized dependencies

d. Key technologies
– ASP.NET Core Web API, Controllers, Middleware
– MediatR
– Entity Framework Core → MySQL
– Redis (IDistributedCache)
– Hangfire for background processing
– Cloudinary, Firebase FCM
– Docker & Docker Compose
– Swagger (OpenAPI)

## Diagram (Mermaid)

```mermaid
flowchart TB
    %% Presentation Layer
    subgraph "Presentation Layer"
        direction TB
        AuthenticationController["AuthenticationController"]:::presentation
        OrderController["OrderController"]:::presentation
        PaymentController["PaymentController"]:::presentation
        ErrorHandlingMiddleware["ErrorHandlingMiddleware"]:::presentation
        JwtBlacklistMiddleware["JwtBlacklistMiddleware"]:::presentation
        TransactionMiddleware["TransactionMiddleware"]:::presentation
    end

    %% Application Layer
    subgraph "Application Layer"
        direction TB
        PlaceOrderCommand["PlaceOrder Command"]:::application
        OrderCompletedHandler["OrderCompletedHandler"]:::application
        OrderPaidIntegrationEvent["OrderPaidIntegrationEvent"]:::application
        LoginCommand["Login Command"]:::application
        IEventBus["IEventBus"]:::application
        IRedisCacheService["IRedisCacheService"]:::application
    end

    %% Domain Layer
    subgraph "Domain Layer"
        direction TB
        EntityBase["Entity (Base)"]:::domain
        OrderPaidDomainEvent["OrderPaid DomainEvent"]:::domain
    end

    %% Infrastructure Layer
    subgraph "Infrastructure Layer"
        direction TB
        DtpDbContext["DtpDbContext"]:::infrastructure
        Migrations["EF Core Migrations"]:::infrastructure
        CompanyRepository["CompanyRepository"]:::infrastructure
        EventBusImpl["EventBus"]:::infrastructure
        RedisService["RedisCacheService"]:::infrastructure
        CloudinaryService["CloudinaryService"]:::infrastructure
        FcmService["FcmService"]:::infrastructure
        JwtTokenService["JwtTokenService"]:::infrastructure
        HangfireJobService["HangfireJobService"]:::infrastructure
        HangfireStorageService["HangfireStorageService"]:::infrastructure
        OrderJobService["OrderJobService"]:::infrastructure
    end

    %% External Services
    subgraph "External Services"
        direction TB
        MySQL["MySQL Database"]:::external
        Redis["Redis"]:::external
        MessageBroker["Message Broker"]:::external
        DockerCompose["Docker Compose"]:::external
        CloudinaryExt["Cloudinary"]:::external
        Firebase["Firebase FCM"]:::external
        HangfireServer["Hangfire Server"]:::external
    end

    %% Data Flows and Relationships
    AuthenticationController -->|calls| PlaceOrderCommand
    OrderController -->|calls| PlaceOrderCommand
    PaymentController -->|calls| PlaceOrderCommand
    AuthenticationController -->|calls| LoginCommand

    PlaceOrderCommand -->|publishes| OrderCompletedHandler
    PlaceOrderCommand -->|raises| OrderPaidIntegrationEvent
    PlaceOrderCommand -->|invokes| IEventBus
    PlaceOrderCommand -->|uses| IRedisCacheService

    LoginCommand -->|uses| IEventBus
    LoginCommand -->|uses| IRedisCacheService

    IEventBus -->|implemented by| EventBusImpl
    EventBusImpl -->|publishes to| MessageBroker

    IRedisCacheService -->|implemented by| RedisService
    RedisService -->|connects to| Redis

    PlaceOrderCommand -->|accesses| DtpDbContext
    DtpDbContext -->|migrates via| Migrations
    DtpDbContext -->|reads/writes| MySQL
    CompanyRepository -->|uses| DtpDbContext

    PlacedOrder -->|base entity| EntityBase
    OrderCompletedHandler -->|handles| OrderPaidDomainEvent

    CloudinaryService -->|uploads to| CloudinaryExt
    FcmService -->|sends to| Firebase
    JwtTokenService -->|issues tokens| AuthenticationController
    HangfireJobService -->|schedules on| HangfireServer
    OrderJobService -->|triggers jobs| HangfireServer
    HangfireStorageService -->|stores jobs| MySQL

    DockerCompose -->|orchestrates| AuthenticationController
    DockerCompose -->|orchestrates| OrderController
    DockerCompose -->|orchestrates| PaymentController
    DockerCompose -->|orchestrates| Redis
    DockerCompose -->|orchestrates| MySQL
    DockerCompose -->|orchestrates| MessageBroker
    DockerCompose -->|orchestrates| HangfireServer

    %% Click Events
    click AuthenticationController "https://github.com/william-le1004/dtp-backend/blob/master/Api/Controllers/AuthenticationController.cs"
    click OrderController "https://github.com/william-le1004/dtp-backend/blob/master/Api/Controllers/OrderController.cs"
    click PaymentController "https://github.com/william-le1004/dtp-backend/blob/master/Api/Controllers/PaymentController.cs"
    click ErrorHandlingMiddleware "https://github.com/william-le1004/dtp-backend/blob/master/Api/Middlewares/ErrorHandlingMiddleware.cs"
    click JwtBlacklistMiddleware "https://github.com/william-le1004/dtp-backend/blob/master/Api/Middlewares/JwtBlacklistMiddleware.cs"
    click TransactionMiddleware "https://github.com/william-le1004/dtp-backend/blob/master/Api/Middlewares/TransactionMiddleware.cs"
    click PlaceOrderCommand "https://github.com/william-le1004/dtp-backend/blob/master/Application/Features/Order/Commands/PlaceOrder.cs"
    click OrderCompletedHandler "https://github.com/william-le1004/dtp-backend/blob/master/Application/Features/Order/DomainEvents/OrderCompletedHandler.cs"
    click OrderPaidIntegrationEvent "https://github.com/william-le1004/dtp-backend/blob/master/Application/Messaging/Order/OrderPaidIntegrationEvent.cs"
    click LoginCommand "https://github.com/william-le1004/dtp-backend/blob/master/Application/Features/Users/Commands/Login.cs"
    click IEventBus "https://github.com/william-le1004/dtp-backend/blob/master/Application/Contracts/EventBus/IEventBus.cs"
    click IRedisCacheService "https://github.com/william-le1004/dtp-backend/blob/master/Application/Contracts/Caching/IRedisCacheService.cs"
    click EntityBase "https://github.com/william-le1004/dtp-backend/blob/master/Domain/Common/Entity.cs"
    click OrderPaidDomainEvent "https://github.com/william-le1004/dtp-backend/blob/master/Domain/Events/OrderPaid.cs"
    click DtpDbContext "https://github.com/william-le1004/dtp-backend/blob/master/Infrastructure/Contexts/DtpDbContext.cs"
    click Migrations "https://github.com/william-le1004/dtp-backend/blob/master/Infrastructure/Migrations/20250508142753_Initial.cs"
    click CompanyRepository "https://github.com/william-le1004/dtp-backend/blob/master/Infrastructure/Repositories/Persistence/CompanyRepository.cs"
    click EventBusImpl "https://github.com/william-le1004/dtp-backend/blob/master/Infrastructure/Repositories/MessageBroker/EventBus.cs"
    click RedisService "https://github.com/william-le1004/dtp-backend/blob/master/Infrastructure/Services/RedisCacheService.cs"
    click CloudinaryService "https://github.com/william-le1004/dtp-backend/blob/master/Infrastructure/Services/CloudinaryService.cs"
    click FcmService "https://github.com/william-le1004/dtp-backend/blob/master/Infrastructure/Services/FcmService.cs"
    click JwtTokenService "https://github.com/william-le1004/dtp-backend/blob/master/Infrastructure/Services/JwtTokenService.cs"
    click HangfireJobService "https://github.com/william-le1004/dtp-backend/blob/master/Infrastructure/Services/HangfireJobService.cs"
    click OrderJobService "https://github.com/william-le1004/dtp-backend/blob/master/Infrastructure/Services/OrderJobService.cs"
    click DockerCompose "https://github.com/william-le1004/dtp-backend/blob/master/compose.yaml"

    %% Styles
    classDef presentation fill:#DCEEFB,stroke:#0366D6,color:#000
    classDef application fill:#E6F4EA,stroke:#22863A,color:#000
    classDef domain fill:#FFF5B1,stroke:#D29922,color:#000
    classDef infrastructure fill:#FFEFEF,stroke:#C32A1F,color:#000
    classDef external fill:#F1F8FF,stroke:#586069,color:#000
```
