# fer-gc05/apihexagonal architecture

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

- Interactive diagram: https://gitdiagram.com/fer-gc05/apihexagonal
- Repository: https://github.com/fer-gc05/apihexagonal

## Overview

##### Main Components
- Domain Layer: This includes the core business logic. Represent the "src/domain" folder, which contains entities (e.g., comment, post, user) and domain-specific exceptions.
- Application Layer: This encapsulates the application logic using use cases and ports. Include:
- Use cases: "src/application/use_cases" (with separate directories for comment, post, and user operations like create, update, delete, login, register).
- Ports: "src/application/ports" (interfaces for repositories such as comment_repository, post_repository, user_repository).
- Infrastructure Layer: This deals with the technical implementations and external integrations. Add nodes for:
- API layer: "src/infrastructure/api" which holds the FastAPI application entry point (main.py under infrastructure/api), routers (for auth, comments, posts, users), dependencies, and API schemas.
- Database layer: "src/infrastructure/database" contains configuration, migration scripts (with Alembic) and data models (SQLAlchemy models for comment, post, user).
- Repositories: "src/infrastructure/repositories" which implement the interfaces defined in the ports using SQLAlchemy.
- Configuration & Environment: Represent configuration files and environment setups (e.g., ".env.example", "src/config/config.py" and the root main.py) to highlight how configurations and external settings influence the system.
- Testing: Although not the focus of the live system, include a component for tests (located in "src/test") to indicate unit test coverage.

##### Relationships and Interactions
- The Application Layer invokes domain logic and interacts with ports.
- Ports (interfaces) are implemented by repositories in the Infrastructure Layer.
- The API layer receives HTTP requests and routes these to the appropriate use cases in the application.
- Database interactions flow from repository implementations to the MySQL database (managed via SQLAlchemy and Alembic migrations).
- Data flow from the client (through the API) to the use cases and how that propagates to the domain and infrastructure layers.

##### Architectural Patterns and Principles
- Hexagonal Architecture by illustrating the separation into three concentric circles or layers: the Domain at the center, surrounded by the Application (with use cases and ports), and further by the Infrastructure (adapters such as API and database).
- Dependency inversion where the domain does not directly depend on the infrastructure, but instead the Application layer interacts with it via abstract ports.
- Framework-specific implementations like FastAPI (for the API entry point) and the role of Alembic for handling database migrations.

##### Relevant Technologies, Frameworks, and Libraries
- FastAPI for building the RESTful API.
- SQLAlchemy for ORM support and database model management.
- Alembic for handling database migrations.
- MySQL as the underlying database.
- JWT-based authentication as part of the API’s security (as indicated in the README).

## Diagram (Mermaid)

```mermaid
flowchart TD
    subgraph "Configuration & Environment"
        Env["Environment (.env.example)"]:::config
        ConfigFile["Configuration File"]:::config
    end

    subgraph "Infrastructure Layer"
        API["API (FastAPI Entry, Routers, Schemas, Dependencies)"]:::infrastructure
        Repos["Repositories (SQLAlchemy Implementations)"]:::infrastructure
        Database["Database (SQLAlchemy Models, Alembic Migrations, Configuration)"]:::infrastructure
    end

    subgraph "Application Layer"
        UseCases["Use Cases"]:::application
        Ports["Ports (Repository Interfaces)"]:::application
    end

    subgraph "Domain Layer"
        Domain["Domain (Entities & Exceptions)"]:::domain
    end

    subgraph "Testing"
        Testing["Testing (Unit Tests)"]:::testing
    end

    Env -->|"configures"| API
    Env -->|"configures"| Database
    ConfigFile -->|"configures"| API
    ConfigFile -->|"configures"| Database

    API -->|"calls"| UseCases
    UseCases -->|"invokes"| Domain
    UseCases -->|"uses"| Ports
    Ports -->|"implemented_by"| Repos
    Repos -->|"interacts_with"| Database

    Testing -->|"tests"| Domain
    Testing -->|"tests"| UseCases

    classDef config fill:#fff3cd,stroke:#856404,stroke-width:2px;
    classDef infrastructure fill:#cce5ff,stroke:#004085,stroke-width:2px;
    classDef application fill:#ccffcc,stroke:#009900,stroke-width:2px;
    classDef domain fill:#ffcccc,stroke:#cc0000,stroke-width:2px;
    classDef testing fill:#d6d8db,stroke:#6c757d,stroke-width:2px;

    click Domain "https://github.com/fer-gc05/apihexagonal/tree/main/src/domain"
    click UseCases "https://github.com/fer-gc05/apihexagonal/tree/main/src/application/use_cases"
    click Ports "https://github.com/fer-gc05/apihexagonal/tree/main/src/application/ports"
    click API "https://github.com/fer-gc05/apihexagonal/tree/main/src/infrastructure/api"
    click Database "https://github.com/fer-gc05/apihexagonal/tree/main/src/infrastructure/database"
    click Repos "https://github.com/fer-gc05/apihexagonal/tree/main/src/infrastructure/repositories"
    click Env "https://github.com/fer-gc05/apihexagonal/blob/main/.env.example"
    click ConfigFile "https://github.com/fer-gc05/apihexagonal/blob/main/src/config/config.py"
    click Testing "https://github.com/fer-gc05/apihexagonal/tree/main/src/test"
```
