# mehmetgencv/ktor-bank architecture

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

- Interactive diagram: https://gitdiagram.com/mehmetgencv/ktor-bank
- Repository: https://github.com/mehmetgencv/ktor-bank

## Overview

##### Project Type and Purpose
- Project Type: A backend REST API built with Ktor.
- Purpose: Provides a simple banking service with operations for creating accounts, depositing, withdrawing, and transferring money.
- Key Technologies: Ktor (as the server framework), Kotlin, Exposed (ORM), and H2 (in-memory database).

##### File Structure to Define Architectural Layers
- Top-Level Organization: The project follows a layered design with clear separation by packages inside src/main/kotlin.
- Core Directories:
- config: Contains application configuration files (e.g., database, serialization, exception handling) that set up the Ktor environment.
- dto: Houses Data Transfer Objects, defining the structure of incoming/outgoing data.
- model: Contains domain models such as Account, Transaction, and TransactionType that define the business entities.
- repository: Implements CRUD operations for the database interactions via the Exposed library.
- service: Contains business logic (e.g., AccountService) that mediates between API calls and data persistence.
- routes: Defines the API endpoints and maps HTTP requests to the corresponding service operations.
- exceptions: Lists custom exceptions used for error handling (e.g., AccountNotFoundException).

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Presentation Layer
    subgraph "Presentation Layer"
        A["Ktor Application (Main Entry)"]:::infrastructure
        B["API Routing"]:::infrastructure
        C["Data Transfer Objects (DTOs)"]:::data
    end

    %% Business Logic Layer
    subgraph "Business Logic Layer"
        D["Business Logic Service (AccountService)"]:::service
    end

    %% Data Access Layer
    subgraph "Data Access Layer"
        E["Account Repository"]:::repository
        F["Transaction Repository"]:::repository
        G["Data Models (Account, Transaction, TransactionType)"]:::data
    end

    %% Infrastructure & Configuration
    subgraph "Infrastructure & Configuration"
        H["Configuration"]:::config
        I["Exception Handling"]:::exceptions
        J["H2 Database"]:::database
        K["Database Configuration (H2 setup)"]:::database
    end

    %% Connections between layers
    A -->|"registersRoutes"| B
    B -->|"forwardsRequests"| D
    B -->|"uses"| C
    D -->|"invokes"| E
    D -->|"invokes"| F
    E -->|"accesses"| J
    F -->|"accesses"| J
    K -->|"configures"| J
    D -->|"throws"| I
    I -->|"handlesErrors"| B

    %% Configuration links
    H -->|"configures"| A
    H -->|"configures"| B
    H -->|"configures"| D

    %% Repository to Data Models mapping (dashed connections)
    E -.->|"mapsTo"| G
    F -.->|"mapsTo"| G

    %% Styles
    classDef infrastructure fill:#f9d,stroke:#333,stroke-width:2px;
    classDef service fill:#cdf,stroke:#333,stroke-width:2px;
    classDef repository fill:#cfc,stroke:#333,stroke-width:2px;
    classDef data fill:#ffd,stroke:#333,stroke-width:2px;
    classDef config fill:#ddf,stroke:#333,stroke-width:2px;
    classDef exceptions fill:#fdd,stroke:#333,stroke-width:2px;
    classDef database fill:#dff,stroke:#333,stroke-width:2px;

    %% Click Events
    click A "https://github.com/mehmetgencv/ktor-bank/blob/main/src/main/kotlin/Application.kt"
    click B "https://github.com/mehmetgencv/ktor-bank/blob/main/src/main/kotlin/routes/Routing.kt"
    click D "https://github.com/mehmetgencv/ktor-bank/blob/main/src/main/kotlin/service/AccountService.kt"
    click E "https://github.com/mehmetgencv/ktor-bank/blob/main/src/main/kotlin/repository/AccountRepository.kt"
    click F "https://github.com/mehmetgencv/ktor-bank/blob/main/src/main/kotlin/repository/TransactionRepository.kt"
    click G "https://github.com/mehmetgencv/ktor-bank/tree/main/src/main/kotlin/model"
    click C "https://github.com/mehmetgencv/ktor-bank/tree/main/src/main/kotlin/dto"
    click H "https://github.com/mehmetgencv/ktor-bank/tree/main/src/main/kotlin/config"
    click I "https://github.com/mehmetgencv/ktor-bank/tree/main/src/main/kotlin/exceptions"
    click K "https://github.com/mehmetgencv/ktor-bank/blob/main/src/main/resources/application.yaml"
```
