# log795-pfe020/stockai-backend architecture

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

- Interactive diagram: https://gitdiagram.com/log795-pfe020/stockai-backend
- Repository: https://github.com/log795-pfe020/stockai-backend

## Overview

##### Main Components
- Presentation Layer:
- This includes the Presentation.Api (API controllers and middlewares), Presentation.Consumers (for event consumption via messaging), and Presentation.Jobs (background tasks such as TickJob, AddDefaultDbRecords, etc.).
- Application Layer:
- Separated into two major parts: Application.Commands and Application.Queries, which follow the CQRS pattern. Identify the command handlers (e.g., ResetAdminPasswordHandler, CreateClockHandler, etc.) and query handlers (e.g., GetStockPriceHandler, GetJwtForCredentialsHandler, etc.) in these directories.
- Domain Layer:
- Consists of Domain.Common, Domain.News, Domain.Portfolio, Domain.Stock, Domain.Time, and Domain.User. Here you have domain models (like Article, Wallet, Share, Clock, etc.), value objects, and domain events. This layer encapsulates core business logic and rules.
- Infrastructure Layer:
- Includes Infrastructure.RabbitMQ (for messaging, using MassTransit, with services and registration code that sets up message publishing) and Infrastructure.Repositories (various repository implementations, migrations, and context setups for both relational databases and NoSQL such as MongoArticleRepository and InMemoryStore).
- Configuration/Bootstrap Layer:
- The Configuration directory contains startup code (Program.cs, Startup.cs) and dispatchers (CommandDispatcher, QueryDispatcher), which wire up dependency injection, the message publisher, encryption services, and other cross-cutting concerns like certificate management.

##### Relationships and Data Flow
- The Presentation Layer (API controllers and job runners) interacts with the Application Layer by issuing commands or queries.
- In the Application Layer, the command and query dispatchers (from the “Seedwork” sections) route operations to their respective handlers.
- The command handlers in Application.Commands perform business operations by delegating to the Domain Layer (for rules and state changes) and then interact with the Infrastructure Layer for persistence (e.g., repositories) and messaging (e.g., RabbitMQ for event distribution).
- The query handlers in Application.Queries retrieve data from the underlying data stores via repositories.
- RabbitMQ handled in Infrastructure.RabbitMQ facilitates asynchronous communication between components, with consumers in Presentation.Consumers picking up events such as News or StockQuote.
- Integration tests (AuthServer.IntegrationTests) reference these layers and test the interactions by setting up dockerized containers for external dependencies (e.g., Azurite for blob storage, MongoDB, Postgres, RabbitMQ).

##### Architectural Patterns and Key Technologies
- CQRS Pattern: Clear separation between write (commands) and read (queries) sides.
- Domain-Driven Design (DDD): The Domain projects and Seedwork classes indicate a strong emphasis on business logic encapsulated in aggregates, entities, value objects, and domain events.
- Messaging: The use of RabbitMQ (via MassTransit configuration) for event-driven communication is a core architectural element.
- Microservices-ish or Modular Monolithic: Even though it might run as a combined solution, the neat separation into layers and projects indicates a modular design that could be split later.
- Infrastructure orchestration: Docker compose is used to build and run dependencies, as indicated by the docker-compose.yml, which ties the system together during development and testing.

##### Final Reminders
- Be detailed – include all essential components (presentation, application commands/queries, domain, infrastructure, configuration).
- Optionally, annotate technologies (e.g., .NET 8, RabbitMQ, MassTransit, Docker) alongside each section.
- Remember to note integration points such as the docker-compose network and testing/integration components.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% External Client
    EC("External Client"):::external

    %% Presentation Layer
    subgraph "Presentation Layer"
        API("API Endpoints"):::presentation
        CON("Event Consumers"):::presentation
        JOBS("Background Jobs"):::presentation
    end

    %% Application Layer
    subgraph "Application Layer"
        CMD("Application Commands"):::application
        QRY("Application Queries"):::application
    end

    %% Domain Layer
    subgraph "Domain Layer"
        DOMC("Domain: Common"):::domain
        DOMN("Domain: News"):::domain
        DOMP("Domain: Portfolio"):::domain
        DOMS("Domain: Stock"):::domain
        DOMT("Domain: Time"):::domain
        DOMU("Domain: User"):::domain
    end

    %% Infrastructure Layer
    subgraph "Infrastructure Layer"
        RAB("Messaging (RabbitMQ)"):::infrastructure
        REPO("Repositories"):::infrastructure
    end

    %% Configuration/Bootstrap Layer
    subgraph "Configuration/Bootstrap Layer"
        CONF("Startup & Dispatchers"):::configuration
    end

    %% Data Flow Connections
    EC -->|"APIRequest"| API
    API -->|"ExecuteCommands"| CMD
    API -->|"ExecuteQueries"| QRY
    JOBS -->|"TriggerTasks"| CMD
    JOBS -->|"TriggerTasks"| QRY
    CMD -->|"BusinessLogic"| DOMC
    CMD -->|"PublishEvents"| RAB
    QRY -->|"FetchData"| REPO
    CONF -->|"WireDependencies"| CMD
    CONF -->|"WireDependencies"| QRY
    RAB -->|"ConsumedBy"| CON

    %% Additional Domain Connections (for representation)
    CMD -->|"ApplyRules"| DOMN
    CMD -->|"ApplyRules"| DOMP
    CMD -->|"ApplyRules"| DOMS
    CMD -->|"ApplyRules"| DOMT
    CMD -->|"ApplyRules"| DOMU

    %% Styling Classes
    classDef external fill:#fadbd8,stroke:#c0392b,stroke-width:2px;
    classDef presentation fill:#f2d7d5,stroke:#b03a2e,stroke-width:2px;
    classDef application fill:#d5f5e3,stroke:#27ae60,stroke-width:2px;
    classDef domain fill:#d6eaf8,stroke:#2980b9,stroke-width:2px;
    classDef infrastructure fill:#fcf3cf,stroke:#f39c12,stroke-width:2px;
    classDef configuration fill:#e8daef,stroke:#8e44ad,stroke-width:2px;

    %% Click Events for Component Mappings
    click API "https://github.com/log795-pfe020/stockai-backend/blob/main/Presentation.Api"
    click CON "https://github.com/log795-pfe020/stockai-backend/blob/main/Presentation.Consumers"
    click JOBS "https://github.com/log795-pfe020/stockai-backend/blob/main/Presentation.Jobs"
    click CMD "https://github.com/log795-pfe020/stockai-backend/blob/main/Application.Commands"
    click QRY "https://github.com/log795-pfe020/stockai-backend/blob/main/Application.Queries"
    click DOMC "https://github.com/log795-pfe020/stockai-backend/blob/main/Domain.Common"
    click DOMN "https://github.com/log795-pfe020/stockai-backend/blob/main/Domain.News"
    click DOMP "https://github.com/log795-pfe020/stockai-backend/blob/main/Domain.Portfolio"
    click DOMS "https://github.com/log795-pfe020/stockai-backend/blob/main/Domain.Stock"
    click DOMT "https://github.com/log795-pfe020/stockai-backend/blob/main/Domain.Time"
    click DOMU "https://github.com/log795-pfe020/stockai-backend/blob/main/Domain.User"
    click RAB "https://github.com/log795-pfe020/stockai-backend/blob/main/Infrastructure.RabbitMQ"
    click REPO "https://github.com/log795-pfe020/stockai-backend/blob/main/Infrastructure.Repositories"
    click CONF "https://github.com/log795-pfe020/stockai-backend/tree/main/Configuration"
```
