# adamhavlicek/fastapi-todo-ddd architecture

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

- Interactive diagram: https://gitdiagram.com/adamhavlicek/fastapi-todo-ddd
- Repository: https://github.com/adamhavlicek/fastapi-todo-ddd

## Overview

The project is a FastAPI-based backend service that follows Domain-Driven Design (DDD) and Clean Architecture principles. Its main purpose is to provide a well-organized, modular API to manage domain-specific functionality—specifically for “user” and “task” features—with clearly separated responsibilities for presentation, domain logic, and data persistence (using PostgreSQL through SQLAlchemy).

##### Main Components
- API Layer: Represented by the FastAPI application (main.py) and the presentation layer within each feature (app/features/task/presentation and app/features/user/presentation). These layers handle incoming HTTP requests and define endpoints (e.g., create, update, delete, and get routes).
- Domain Layer: This includes the domain entities, use cases, and repository interfaces (e.g., files under app/features/task/domain and app/features/user/domain). This layer encapsulates business logic and rules.
- Data Layer: Shows the implementation details (repository implementations, data models, and query services) under app/features/task/data and app/features/user/data, and the global data access logic under app/core/database. This layer is responsible for communication with the PostgreSQL database.
- Core Components: Highlight shared modules like error handling (app/core/error), configuration (app/config.py), unit of work (app/core/unit_of_work), and common models.
- Infrastructure: The PostgreSQL database and Docker (with Docker Compose, Dockerfile, etc.), which offer deployment and operational support.

##### Relationships and Interactions
- These API routes then invoke use cases in the Domain Layer. Indicate directional flows showing that controllers (routes) call the appropriate use cases (e.g., create_user, update_task).
- Additionally, show how the “Unit of Work” wraps data operations to manage transactions and consistency.

##### Architectural Patterns and Design Principles
- Domain-driven concepts: Note the use of entities, value objects, aggregates, services, factories, and repositories. Annotate these within the domain components.
- Dependency Inversion: Indicate that high-level modules (domain and use cases) do not depend on low-level data access implementations but instead depend on abstractions.
- Docker and containerization: Represent the environment where the application and database run (Docker, docker-compose.yml, Dockerfile).

##### Relevant Technologies and Frameworks
- FastAPI as the main framework for handling HTTP endpoints.
- SQLAlchemy for ORM functionality to interact with the Postgres database.
- Docker for containerizing the application and managing deployments via Docker Compose.
- Python and Poetry for dependency management and building the project.

##### Diagrammatic Elements
- Clear labels for every component (e.g., “User Routes”, “Task Use Cases”, “User Repository Interface”, “Postgres Database”, etc.).

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Global Entities
    Client("Client"):::infra

    %% API Layer
    subgraph "API Layer"
        FastAPI("FastAPI App"):::api
        TaskRoutes("Task Routes (Task Endpoints)"):::api
        UserRoutes("User Routes (User Endpoints)"):::api
    end

    %% Domain Layer
    subgraph "Domain Layer"
        TaskDomain("Task Domain (Entities, Use Cases, Repositories)"):::domain
        UserDomain("User Domain (Entities, Use Cases, Repositories)"):::domain
    end

    %% Data Layer
    subgraph "Data Layer"
        TaskData("Task Data (Repositories, Models)"):::data
        UserData("User Data (Repositories, Models)"):::data
        GlobalData("Global Data Access (SQLAlchemy)"):::data
        PostgreSQL("PostgreSQL Database"):::infra
    end

    %% Core Components
    subgraph "Core Components"
        Config("Configuration"):::core
        ErrorHandling("Error Handling"):::core
        UnitOfWork("Unit Of Work"):::core
        CommonModels("Common Models"):::core
        BaseRepositories("Base Repositories"):::core
        BaseUseCases("Base Use Cases"):::core
    end

    %% Infrastructure
    subgraph "Infrastructure"
        Docker("Docker Deployment"):::infra
        PostgresModule("Postgres Integration"):::infra
    end

    %% Connections between layers
    Client -->|"sends_request"| FastAPI
    FastAPI -->|"routes_to"| TaskRoutes
    FastAPI -->|"routes_to"| UserRoutes

    TaskRoutes -->|"calls"| TaskDomain
    UserRoutes -->|"calls"| UserDomain

    TaskDomain -->|"uses"| UnitOfWork
    UserDomain -->|"uses"| UnitOfWork

    TaskDomain -->|"defines_interface"| BaseRepositories
    UserDomain -->|"defines_interface"| BaseRepositories

    BaseRepositories -->|"implemented_by"| TaskData
    BaseRepositories -->|"implemented_by"| UserData

    TaskData -->|"accesses"| GlobalData
    UserData -->|"accesses"| GlobalData

    GlobalData -->|"communicates_with"| PostgreSQL

    GlobalData -->|"utilizes"| PostgresModule
    PostgresModule -->|"connects_to"| PostgreSQL

    %% Core components usage
    FastAPI -->|"loads"| Config
    TaskDomain -->|"uses"| CommonModels
    UserDomain -->|"uses"| CommonModels
    FastAPI -->|"handles_errors_with"| ErrorHandling

    %% Infrastructure wrapping components
    Docker -->|"containers"| FastAPI
    Docker -->|"containers"| PostgreSQL

    %% Styles
    classDef api fill:#ADD8E6,stroke:#333,stroke-width:2px;
    classDef domain fill:#90EE90,stroke:#333,stroke-width:2px;
    classDef data fill:#FFB6C1,stroke:#333,stroke-width:2px;
    classDef core fill:#FFFACD,stroke:#333,stroke-width:2px;
    classDef infra fill:#D3D3D3,stroke:#333,stroke-width:2px;

    %% Click Events for API Layer
    click FastAPI "https://github.com/adamhavlicek/fastapi-todo-ddd/blob/master/main.py"
    click TaskRoutes "https://github.com/adamhavlicek/fastapi-todo-ddd/tree/master/app/features/task/presentation/routes"
    click UserRoutes "https://github.com/adamhavlicek/fastapi-todo-ddd/tree/master/app/features/user/presentation/routes"

    %% Click Events for Domain Layer
    click TaskDomain "https://github.com/adamhavlicek/fastapi-todo-ddd/tree/master/app/features/task/domain"
    click UserDomain "https://github.com/adamhavlicek/fastapi-todo-ddd/tree/master/app/features/user/domain"

    %% Click Events for Data Layer
    click TaskData "https://github.com/adamhavlicek/fastapi-todo-ddd/tree/master/app/features/task/data"
    click UserData "https://github.com/adamhavlicek/fastapi-todo-ddd/tree/master/app/features/user/data"
    click GlobalData "https://github.com/adamhavlicek/fastapi-todo-ddd/tree/master/app/core/database"

    %% Click Events for Core Components
    click Config "https://github.com/adamhavlicek/fastapi-todo-ddd/blob/master/app/config.py"
    click ErrorHandling "https://github.com/adamhavlicek/fastapi-todo-ddd/tree/master/app/core/error"
    click UnitOfWork "https://github.com/adamhavlicek/fastapi-todo-ddd/tree/master/app/core/unit_of_work"
    click CommonModels "https://github.com/adamhavlicek/fastapi-todo-ddd/tree/master/app/core/models"
    click BaseRepositories "https://github.com/adamhavlicek/fastapi-todo-ddd/tree/master/app/core/repositories"
    click BaseUseCases "https://github.com/adamhavlicek/fastapi-todo-ddd/tree/master/app/core/use_cases"

    %% Click Events for Infrastructure
    click Docker "https://github.com/adamhavlicek/fastapi-todo-ddd/tree/master/Dockerfile"
    click PostgresModule "https://github.com/adamhavlicek/fastapi-todo-ddd/tree/master/app/core/database/postgres"
```
