# leyen1403/myapi architecture

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

- Interactive diagram: https://gitdiagram.com/leyen1403/myapi
- Repository: https://github.com/leyen1403/myapi

## Overview

##### Project type and purpose
- This is a backend RESTful API built with ASP .NET Core, following a Clean/Onion architecture.
- Purpose: expose endpoints for managing employees and fetching external data (JSONPlaceholder, jokes).

##### File structure
- Top-level project “MyApi.sln” containing four projects:
- MyApi.Api – ASP .NET Core Web API (Presentation layer)
- MyApi.Application – CQRS handlers (Application layer)
- MyApi.Core – Domain entities, interfaces, models, options (Core layer)
- MyApi.Infranstructure – EF Core DbContext, repositories, HTTP client services (Infrastructure layer)
- Each layer has its own DependencyInjection.cs to register services.
- Migrations under Infrastructure represent database schema evolution.

##### Main components
A. Presentation / API layer
- Controllers: EmployeesController, ExternalVendorsController
- Program.cs / Startup: configures middleware, routing, dependency injection
B. Application layer
- Command handlers (AddEmployeeCommand, UpdateEmployeeCommand, DeleteEmployeeCommand)
- Query handlers (GetAllEmployeesQuery, GetEmployeeByIdQuery, GetJsonplaceholderDataQuery, GetRandomJokeQuery, GetTenJokeQuery)
C. Core layer
- Entities (EmployeeEntity), Models (Joke, Jsonplaceholder)
- Interfaces (IEmployeeRepository, IExternalVendorRepository, IJokeHttpClientService, IJsonplaceholderHttpClientService)
- Options (ConnectionStringOptions)
D. Infrastructure layer
- EF Core AppDbContext & Migrations
- Repositories (EmployeeRepository, ExternalVendorRepository) implementing Core interfaces
- HTTP‐client services (JokeHttpClientService, JsonplaceholderHttpClientService) implementing Core interfaces
E. External systems
- SQL Database
- JSONPlaceholder REST API
- Joke API

##### Relationships and data flows
1. Client → HTTP request → API Controllers
2. Controller → calls Application layer (Command or Query handlers)
3. Handler → depends on Core interfaces (`IEmployeeRepository`, `IJokeHttpClientService`, etc.)
4. Interface implementations reside in Infrastructure
- Repositories → AppDbContext → Database
- HTTP‐client services → external REST APIs
5. Responses bubble back: external/DB → Infrastructure → Application handler → API → Client

##### Architectural patterns and design principles
- Clean or Onion architecture: strict dependency rule (outer layers depend on inner layers only via interfaces).
- CQRS pattern: split Commands (writes) and Queries (reads) to organize business logic.
- Dependency Injection in each layer to decouple implementation.
- EF Core Code-First migrations for schema management.
- HttpClient wrapper services for external API integration.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Clients
    client["Clients / Consumers"]:::external

    %% API Layer
    subgraph "API (Presentation Layer)"
        direction TB
        Program["Program.cs / Startup"]:::presentation
        ApiDI["DependencyInjection.cs"]:::presentation
        EmployeesController["EmployeesController"]:::presentation
        ExternalVendorsController["ExternalVendorsController"]:::presentation
    end

    %% Application Layer
    subgraph "Application (CQRS Layer)"
        direction TB
        AppDI["DependencyInjection.cs"]:::application
        AddEmployeeHandler["AddEmployeeCommand Handler"]:::application
        UpdateEmployeeHandler["UpdateEmployeeCommand Handler"]:::application
        DeleteEmployeeHandler["DeleteEmployeeCommand Handler"]:::application
        GetAllEmployeesHandler["GetAllEmployeesQuery Handler"]:::application
        GetEmployeeByIdHandler["GetEmployeeByIdQuery Handler"]:::application
        GetJsonplaceholderHandler["GetJsonplaceholderDataQuery Handler"]:::application
        GetRandomJokeHandler["GetRandomJokeQuery Handler"]:::application
        GetTenJokeHandler["GetTenJokeQuery Handler"]:::application
    end

    %% Core Layer
    subgraph "Core (Domain Layer)"
        direction TB
        CoreDI["DependencyInjection.cs"]:::core
        EmployeeEntity["EmployeeEntity"]:::core
        JokeModel["Joke Model"]:::core
        JsonplaceholderModel["Jsonplaceholder Model"]:::core
        IEmployeeRepo["IEmployeeRepository Interface"]:::core
        IExternalVendorRepo["IExternalVendorRepository Interface"]:::core
        IJokeClientInterface["IJokeHttpClientService Interface"]:::core
        IJsonplaceholderClientInterface["IJsonplaceholderHttpClientService Interface"]:::core
        ConnectionOptions["ConnectionStringOptions"]:::core
    end

    %% Infrastructure Layer
    subgraph "Infrastructure (Persistence & External Services)"
        direction TB
        InfraDI["DependencyInjection.cs"]:::infra
        AppDbContext["AppDbContext (EF Core)"]:::infra
        Migrations["Migrations"]:::infra
        EmployeeRepoImpl["EmployeeRepository"]:::infra
        ExternalVendorRepoImpl["ExternalVendorRepository"]:::infra
        JokeHttpService["JokeHttpClientService"]:::infra
        JsonplaceholderHttpService["JsonplaceholderHttpClientService"]:::infra
        IJokeHttpClientServiceInfra["IJokeHttpClientService"]:::infra
        IJsonplaceholderHttpClientServiceInfra["IJsonplaceholderHttpClientService"]:::infra
    end

    %% External Systems
    subgraph "External Systems"
        direction TB
        SqlDb["SQL Database"]:::external
        JsonPlaceholderAPI["JSONPlaceholder REST API"]:::external
        JokeAPI["Joke API"]:::external
    end

    %% Connections
    client -->|"calls"| EmployeesController
    client -->|"calls"| ExternalVendorsController

    EmployeesController -->|"uses"| AddEmployeeHandler
    EmployeesController -->|"uses"| UpdateEmployeeHandler
    EmployeesController -->|"uses"| DeleteEmployeeHandler
    EmployeesController -->|"uses"| GetAllEmployeesHandler
    EmployeesController -->|"uses"| GetEmployeeByIdHandler

    ExternalVendorsController -->|"uses"| GetJsonplaceholderHandler
    ExternalVendorsController -->|"uses"| GetRandomJokeHandler
    ExternalVendorsController -->|"uses"| GetTenJokeHandler

    AddEmployeeHandler -->|"depends on"| IEmployeeRepo
    UpdateEmployeeHandler -->|"depends on"| IEmployeeRepo
    DeleteEmployeeHandler -->|"depends on"| IEmployeeRepo
    GetAllEmployeesHandler -->|"depends on"| IEmployeeRepo
    GetEmployeeByIdHandler -->|"depends on"| IEmployeeRepo

    GetJsonplaceholderHandler -->|"depends on"| IExternalVendorRepo
    GetRandomJokeHandler -->|"depends on"| IJokeClientInterface
    GetTenJokeHandler -->|"depends on"| IJokeClientInterface

    IEmployeeRepo -->|"implemented by"| EmployeeRepoImpl
    IExternalVendorRepo -->|"implemented by"| ExternalVendorRepoImpl
    IJokeClientInterface -->|"implemented by"| JokeHttpService
    IJsonplaceholderClientInterface -->|"implemented by"| JsonplaceholderHttpService

    EmployeeRepoImpl -->|"uses"| AppDbContext
    AppDbContext -->|"migrations"| Migrations
    AppDbContext -->|"reads/writes"| SqlDb

    JokeHttpService -->|"calls"| JokeAPI
    JsonplaceholderHttpService -->|"calls"| JsonPlaceholderAPI

    %% Click Events - API
    click EmployeesController "https://github.com/leyen1403/myapi/blob/master/MyApi.Api/Controllers/EmployeesController.cs"
    click ExternalVendorsController "https://github.com/leyen1403/myapi/blob/master/MyApi.Api/Controllers/ExternalVendorsController.cs"
    click Program "https://github.com/leyen1403/myapi/blob/master/MyApi.Api/Program.cs"
    click ApiDI "https://github.com/leyen1403/myapi/blob/master/MyApi.Api/DependencyInjection.cs"

    %% Click Events - Application
    click AddEmployeeHandler "https://github.com/leyen1403/myapi/blob/master/MyApi.Application/Commands/AddEmployeeCommand.cs"
    click UpdateEmployeeHandler "https://github.com/leyen1403/myapi/blob/master/MyApi.Application/Commands/UpdateEmployeeCommand.cs"
    click DeleteEmployeeHandler "https://github.com/leyen1403/myapi/blob/master/MyApi.Application/Commands/DeleteEmployeeCommand.cs"
    click GetAllEmployeesHandler "https://github.com/leyen1403/myapi/blob/master/MyApi.Application/Queries/GetAllEmployeesQuery.cs"
    click GetEmployeeByIdHandler "https://github.com/leyen1403/myapi/blob/master/MyApi.Application/Queries/GetEmployeeByIdQuery.cs"
    click GetJsonplaceholderHandler "https://github.com/leyen1403/myapi/blob/master/MyApi.Application/Queries/GetJsonplaceholderDataQuery.cs"
    click GetRandomJokeHandler "https://github.com/leyen1403/myapi/blob/master/MyApi.Application/Queries/GetRandomJokeQuery.cs"
    click GetTenJokeHandler "https://github.com/leyen1403/myapi/blob/master/MyApi.Application/Queries/GetTenJokeQuery.cs"
    click AppDI "https://github.com/leyen1403/myapi/blob/master/MyApi.Application/DependencyInjection.cs"

    %% Click Events - Core
    click EmployeeEntity "https://github.com/leyen1403/myapi/blob/master/MyApi.Core/Entities/EmployeeEntity.cs"
    click JokeModel "https://github.com/leyen1403/myapi/blob/master/MyApi.Core/Models/Joke.cs"
    click JsonplaceholderModel "https://github.com/leyen1403/myapi/blob/master/MyApi.Core/Models/Jsonplaceholder.cs"
    click IEmployeeRepo "https://github.com/leyen1403/myapi/blob/master/MyApi.Core/Interfaces/IEmployeeRepository.cs"
    click IExternalVendorRepo "https://github.com/leyen1403/myapi/blob/master/MyApi.Core/Interfaces/IExternalVendorRepository.cs"
    click IJokeClientInterface "https://github.com/leyen1403/myapi/blob/master/MyApi.Core/Interfaces/IJokeHttpClientService.cs"
    click IJsonplaceholderClientInterface "https://github.com/leyen1403/myapi/blob/master/MyApi.Core/Interfaces/IJsonplaceholderHttpClientService.cs"
    click ConnectionOptions "https://github.com/leyen1403/myapi/blob/master/MyApi.Core/Options/ConnectionStringOptions.cs"
    click CoreDI "https://github.com/leyen1403/myapi/blob/master/MyApi.Core/DependencyInjection.cs"

    %% Click Events - Infrastructure
    click AppDbContext "https://github.com/leyen1403/myapi/blob/master/MyApi.Infranstructure/Data/AppDbContext.cs"
    click Migrations "https://github.com/leyen1403/myapi/tree/master/MyApi.Infranstructure/Migrations/"
    click EmployeeRepoImpl "https://github.com/leyen1403/myapi/blob/master/MyApi.Infranstructure/Repositories/EmployeeRepository.cs"
    click ExternalVendorRepoImpl "https://github.com/leyen1403/myapi/blob/master/MyApi.Infranstructure/Repositories/ExternalVendorRepository.cs"
    click JokeHttpService "https://github.com/leyen1403/myapi/blob/master/MyApi.Infranstructure/Services/JokeHttpClientService.cs"
    click JsonplaceholderHttpService "https://github.com/leyen1403/myapi/blob/master/MyApi.Infranstructure/Services/JsonplaceholderHttpClientService.cs"
    click IJokeHttpClientServiceInfra "https://github.com/leyen1403/myapi/blob/master/MyApi.Infranstructure/Services/IJokeHttpClientService.cs"
    click IJsonplaceholderHttpClientServiceInfra "https://github.com/leyen1403/myapi/blob/master/MyApi.Infranstructure/Services/IJsonplaceholderHttpClientService.cs"
    click InfraDI "https://github.com/leyen1403/myapi/blob/master/MyApi.Infranstructure/DependencyInjection.cs"

    %% Styles
    classDef presentation fill:#E3F2FD,stroke:#1E88E5;
    classDef application fill:#E8F5E9,stroke:#43A047;
    classDef core fill:#FFF9C4,stroke:#FDD835;
    classDef infra fill:#FFE0B2,stroke:#FB8C00;
    classDef external fill:#ECEFF1,stroke:#90A4AE;
```
