# lion3993vn/njs1703_himari_be architecture

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

- Interactive diagram: https://gitdiagram.com/lion3993vn/njs1703_himari_be
- Repository: https://github.com/lion3993vn/njs1703_himari_be

## Overview

##### Project Type & Purpose
- This is a backend API built on .NET 8.0 that uses Entity Framework Core for database interactions. It serves as the backend for a project called “Himari” and includes real-time AI chatbot streaming and integration with the PayOS VietQR payment system.
- The project is organized as a multi-project solution with clearly separated layers:
– The API layer (HimariServer.API) that handles HTTP requests.
– The Repository layer (HimariServer.Repository) which manages Entity Framework Core interactions, database migrations, and data access patterns (using repositories and unit of work).
– The Service layer (HimariServer.Service) which handles business logic and coordinates between the API layer and the repository, including integrations with external systems (e.g., Redis, Firebase, Mail, deep learning models).

##### File Structure
- The HimariServer.API directory contains Controllers, Middlewares, Program.cs, settings (appsettings.json in Development) and dependency injection – this is your entry point where client requests are handled.
- The HimariServer.Repository directory holds:
– DBContext, Entities (domain models), Enums, Migrations, and Repository implementations for handling CRUD operations.
– A UnitOfWork pattern that coordinates repository operations.
- The HimariServer.Service directory is organized into:
– Business Models that serve as DTOs.
– Service Interfaces and Implementations that encapsulate business logic.
– Integration points for real-time processing (e.g., ChatHub for streaming chat), and external integrations with payment (PayOS), onnx embedding for AI, Redis caching, Firebase storage, and email services.
- The existence of configuration files (Dockerfile, GitHub workflows, launchSettings) further indicates support for CI/CD, containerization, and local server runtime configurations.

##### Key Components
- API Layer (HimariServer.API):
– Controllers: Each controller (e.g., Auth, Blog, Payment, Chat) forms an entry point for different functionalities.
– Middleware: Exception handling and authentication from the pipeline.
– Dependency Injection Configuration: Sets up service instances for controllers.
- Service Layer (HimariServer.Service):
– Business Logic: Handles operations for domains like blogs, orders, products, notifications, authentication, and real-time chat.
– External Service Integrations: ChatHub for real-time communications, MailService for emails, Payment services (PayOS), and services for AI/embedding (using ONNX with Vietnamese embedding, Deepseek, Gemini, ChromaDB).
- Repository Layer (HimariServer.Repository):
– Data Access: Repositories for different domain entities (Blog, Category, Order, etc.) and the Entity Framework DBContext.
– Unit of Work: Coordinates transactions across repositories.
– Database Migrations & Entities: Show the automatic schema evolution.
- External Systems:
– SQL Server: Primary relational database as defined in the appsettings.
– Redis: For caching and session management.
– Firebase Storage: For file storage.
– Payment Gateway: PayOS integration for VietQR payments.
– Email Server: SMTP details (configured with Gmail settings) for sending notifications.
– AI & ML Utilities: The inclusion of an ONNX model and integration settings for deep learning tools (Deepseek, Gemini, HuggingFaceTokenizer).

##### Relationships & Data Flow
- Client / External Request Flow: Client requests hit the API Controllers.
- API Layer: Controllers delegate requests to the Service Layer after handling authentication and middleware processing.
- Service Layer: Processes business logic, performs validations, and then delegates data persistence or retrieval to the Repository Layer.
- Repository Layer: Interacts with the SQL Server using Entity Framework Core, managing data through repositories and the Unit of Work.
- Real-time interactions: Include a component for SignalR (ChatHub) that illustrates bi-directional communication for chat functionalities.

##### Architectural Patterns & Design Principles
- Layered Architecture: Clearly separate Controllers (presentation/API) → Services (business logic) → Repositories (data access).
- Domain-Driven Design: The project separates domain entities, business models, and service contracts.
- Integration Concerns: Document how external systems are leveraged and the configuration-driven approach (e.g., JWT, Redis, Firebase, Payment API keys).

## Diagram (Mermaid)

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

    %% API Layer
    subgraph "API Layer"
        A1["Controllers"]:::api
        A2["Middleware & DI"]:::api
        A3["Entry Point & Config"]:::api
    end

    %% Service Layer
    subgraph "Service Layer"
        S1["Business Logic & Implementations"]:::service
        S2["Service Interfaces & DTOs"]:::service
        S3["Real-time Modules (ChatHub)"]:::service
    end

    %% Repository Layer
    subgraph "Repository Layer"
        R1["Data Access & Entities"]:::repository
        R2["Repositories & UnitOfWork"]:::repository
        R3["Migrations"]:::repository
    end

    %% External Systems
    subgraph "External Systems"
        E1["SQL Server"]:::external
        E2["Redis"]:::external
        E3["Firebase Storage"]:::external
        E4["Payment Gateway (PayOS)"]:::external
        E5["Email SMTP"]:::external
        E6["AI & ML Utilities"]:::external
    end

    %% Connections
    Client -->|"HTTP"| A1
    A1 -->|"calls"| S1
    A1 -->|"passes-through"| A2
    S1 -->|"accesses"| R1
    S1 -->|"coordinates"| R2
    S1 -->|"queries"| E1
    S1 -->|"caches"| E2
    S1 -->|"stores"| E3
    S1 -->|"processes"| E4
    S1 -->|"emails"| E5
    S1 -->|"generates"| E6

    %% Click Events for API Layer
    click A1 "https://github.com/lion3993vn/njs1703_himari_be/tree/main/HimariServer.API/Controllers"
    click A2 "https://github.com/lion3993vn/njs1703_himari_be/tree/main/HimariServer.API/Middlewares"
    click A3 "https://github.com/lion3993vn/njs1703_himari_be/blob/main/HimariServer.API/Program.cs"

    %% Click Events for Service Layer
    click S1 "https://github.com/lion3993vn/njs1703_himari_be/tree/main/HimariServer.Service/Services/Implements"
    click S2 "https://github.com/lion3993vn/njs1703_himari_be/tree/main/HimariServer.Service/Services/Interfaces"
    click S3 "https://github.com/lion3993vn/njs1703_himari_be/blob/main/HimariServer.Service/Hubs/ChatHub.cs"

    %% Click Events for Repository Layer
    click R1 "https://github.com/lion3993vn/njs1703_himari_be/tree/main/HimariServer.Repository/DBContext"
    click R2 "https://github.com/lion3993vn/njs1703_himari_be/tree/main/HimariServer.Repository/Repositories"
    click R3 "https://github.com/lion3993vn/njs1703_himari_be/tree/main/HimariServer.Repository/Migrations"

    %% Click Events for External Systems
    click E1 "https://github.com/lion3993vn/njs1703_himari_be/blob/main/HimariServer.API/appsettings.Development.json"
    click E2 "https://github.com/lion3993vn/njs1703_himari_be/blob/main/HimariServer.Service/Services/Implements/RedisService.cs"
    click E3 "https://github.com/lion3993vn/njs1703_himari_be/blob/main/HimariServer.Service/Services/Implements/FirebaseStorageService.cs"
    click E4 "https://github.com/lion3993vn/njs1703_himari_be/blob/main/HimariServer.Service/Services/Implements/PayOSService.cs"
    click E5 "https://github.com/lion3993vn/njs1703_himari_be/blob/main/HimariServer.Service/Services/Implements/MailService.cs"
    click E6 "https://github.com/lion3993vn/njs1703_himari_be/blob/main/HimariServer.Service/Services/Implements/OnnxEmbeddingService.cs"

    %% Styles
    classDef api fill:#F7C6C7,stroke:#333,stroke-width:2px;
    classDef service fill:#C6E2FF,stroke:#333,stroke-width:2px;
    classDef repository fill:#C9F7C6,stroke:#333,stroke-width:2px;
    classDef external fill:#FFFACD,stroke:#333,stroke-width:2px;
```
