# mrquackduck/colirbackend architecture

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

- Interactive diagram: https://gitdiagram.com/mrquackduck/colirbackend
- Repository: https://github.com/mrquackduck/colirbackend

## Overview

##### And Outline the Main Components
- Web API Layer: This is the entry point for all client requests. It includes controllers for authentication, messaging, user management, and room operations. Within the Web API you have additional components for SignalR hubs (ChatHub, ClearRoomHub, RegistrationHub, VoiceChatHub) that support real-time communication.
- Business Logic Layer (BLL): Contains services, models, factories, and request models that encapsulate the core business rules for rooms, messages, users, and statistics. It also includes an AutoMapper profile for object transformation.
- Data Access Layer (DAL): Handles interactions with the PostgreSQL database using Entity Framework Core. It comprises the DbContext, entity definitions (such as Attachment, Message, Room, User, etc.), repositories, and UnitOfWork to abstract and coordinate data operations.
- Exception Handling Modules: Separate project providing custom exceptions (e.g., NotFoundException, NotEnoughPermissionsException) to capture different error conditions.
- Integration & Unit Tests: Projects for testing the BLL, DAL, and Web API integration ensure that the components interact correctly.
- API Related Services: Within the Web API project, additional services such as OAuth2 integration for GitHub and Google as well as token and event handling are defined.

##### Relationships and Data Flow
- The Web API receives client requests and forwards them to the corresponding BLL services.
- Services in the BLL call the DAL via repository interfaces to read/write data. The mapping between DAL entities and BLL models is managed through AutoMapper.
- Exception modules are used centrally to manage any errors or permission issues.
- Authentication is handled by services that generate JWT tokens, manage refresh tokens, and integrate with third-party OAuth2 providers.
- All data passing through the API is encrypted on the client side; the API handles only transmission and storage of already encrypted content.
- Real-time messaging flows via SignalR hubs in the Web API, which interact with the service layer to relay chat messages and manage room states.

##### Architectural Patterns and Key Design Principles
- Layered Architecture: Clearly defined separation between the presentation layer (Web API), business logic (BLL), and data access (DAL) ensures maintainability and testability.
- Repository and UnitOfWork Patterns: Implemented in the DAL to abstract database operations.
- Dependency Injection: Interfaces for services and repositories across all layers promote loose coupling.
- SignalR for Real-Time Communication: Essential for chat and voice features.
- Security Concerns: End-to-end encryption means that while the API handles encrypted data, the key management is handled client-side. Additionally, thorough error handling and permission checks are enforced.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% External Client
    Client["Client Request"]:::external

    %% Web API Layer
    subgraph "Web API Layer"
        Controllers["Controllers"]:::webapi
        Hubs["SignalR Hubs"]:::webapi
        Middleware["Middleware"]:::webapi
        APIServices["API Services"]:::webapi
    end

    %% Business Logic Layer (BLL)
    subgraph "Business Logic Layer (BLL)"
        Services["Services"]:::bll
        Models["Models"]:::bll
        RequestModels["Request Models"]:::bll
        AutoMapper["AutoMapper Profile"]:::bll
        RoomCleanerFactory["RoomCleanerFactory"]:::bll
        BLLTests["BLL Tests"]:::tests
    end

    %% Data Access Layer (DAL)
    subgraph "Data Access Layer (DAL)"
        DbContext["ColirDbContext"]:::dal
        Entities["Entities"]:::dal
        Repositories["Repositories"]:::dal
        UnitOfWork["UnitOfWork"]:::dal
        StringEncryptor["StringEncryptor"]:::dal
        DALTests["DAL Tests"]:::tests
    end

    %% Exception Handling Modules
    Exceptions["Exceptions"]:::exceptions

    %% Integration & Unit Tests (Web API)
    IntegrationTests["WebAPI Integration Tests"]:::tests

    %% External Services & Dependencies
    subgraph "External Services & Dependencies"
        PostgreSQL["PostgreSQL Database"]:::external
        Docker["Docker Deployment"]:::external
        DotNet[".NET Runtime"]:::external
        OAuth2["OAuth2 Providers (GitHub,Google)"]:::external
    end

    %% Data Flow Connections
    Client -->|"sendsRequest"| Controllers
    Controllers -->|"calls"| Services
    Controllers -->|"triggers"| Hubs
    Controllers -->|"utilizes"| Middleware
    Controllers -->|"uses"| APIServices

    Hubs -->|"realTimeMsg"| Services

    Services -->|"dataOps"| Repositories
    Services -->|"mapsData"| AutoMapper
    Services -->|"errorHandling"| Exceptions

    Repositories -->|"dbAccess"| DbContext

    Middleware -->|"handlesErrors"| Exceptions

    APIServices -->|"integratesWith"| OAuth2

    %% Tests Connections
    IntegrationTests --- Controllers
    BLLTests --- Services
    DALTests --- Repositories

    %% External Dependencies Usage
    DbContext -->|"EFCore"| PostgreSQL
    APIServices -->|"JWT_OAuth"| DotNet
    APIServices -->|"oauthCalls"| OAuth2
    Docker --- DotNet

    %% Click Events
    click Controllers "https://github.com/mrquackduck/colirbackend/tree/master/Colir.WebApi/Controllers"
    click Hubs "https://github.com/mrquackduck/colirbackend/tree/master/Colir.WebApi/Hubs"
    click Middleware "https://github.com/mrquackduck/colirbackend/tree/master/Colir.WebApi/Middleware"
    click APIServices "https://github.com/mrquackduck/colirbackend/tree/master/Colir.WebApi/ApiRelatedServices"
    click AutoMapper "https://github.com/mrquackduck/colirbackend/blob/master/Colir.BLL/AutomapperProfile.cs"
    click RoomCleanerFactory "https://github.com/mrquackduck/colirbackend/blob/master/Colir.BLL/Factories/RoomCleanerFactory.cs"
    click BLLTests "https://github.com/mrquackduck/colirbackend/blob/master/Colir.BLL.Tests"
    click DbContext "https://github.com/mrquackduck/colirbackend/blob/master/Colir.DAL/ColirDbContext.cs"
    click Entities "https://github.com/mrquackduck/colirbackend/tree/master/Colir.DAL/Entities"
    click Repositories "https://github.com/mrquackduck/colirbackend/tree/master/Colir.DAL/Repositories"
    click UnitOfWork "https://github.com/mrquackduck/colirbackend/blob/master/Colir.DAL/UnitOfWork.cs"
    click StringEncryptor "https://github.com/mrquackduck/colirbackend/blob/master/Colir.DAL/Encrpyion/StringEncryptor.cs"
    click DALTests "https://github.com/mrquackduck/colirbackend/blob/master/Colir.DAL.Tests"
    click Exceptions "https://github.com/mrquackduck/colirbackend/blob/master/Colir.Exceptions"
    click IntegrationTests "https://github.com/mrquackduck/colirbackend/blob/master/Colir.WebApi.IntegrationTests"

    %% Styles
    classDef webapi fill:#eeffee,stroke:#006600,stroke-width:2px;
    classDef bll fill:#e6f7ff,stroke:#0055aa,stroke-width:2px;
    classDef dal fill:#fff0e6,stroke:#aa5500,stroke-width:2px;
    classDef exceptions fill:#ffe6e6,stroke:#aa0000,stroke-width:2px;
    classDef tests fill:#f9f9f9,stroke:#999999,stroke-width:2px,stroke-dasharray: 5 5;
    classDef external fill:#e8e8ff,stroke:#3333aa,stroke-width:2px;
```
