# alanlapierre/habit-tracker architecture

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

- Interactive diagram: https://gitdiagram.com/alanlapierre/habit-tracker
- Repository: https://github.com/alanlapierre/habit-tracker

## Overview

##### Main Components
- Domain Layer:
- Contains business logic, domain models (e.g., Habit, HabitStatistics), exceptions, and utility classes.
- It includes services (HabitFinder, HabitStatisticsCalculator), use cases (AddHabitUseCase, CompleteHabitUseCase, etc.), mappers, DTOs (in dto package), and exception handling (HabitErrorHandler).

- Infrastructure Layer:
- Split into two major sub-layers:
▪ Persistence Sub-layer: Includes MongoDB connection (MongoDBConnection) and repository implementation (HabitRepositoryImpl) that implements the domain-level repository interface.
▪ UI Sub-layer: Manages the user interaction. Contains the HabitTrackerController, menu configuration (Menu, MenuOption, MenuConfiguration), action implementations (e.g., AddHabitAction, CompleteHabitAction, etc.), presenters (ConsoleMenuPresenter), selectors (HabitSelector), and UI exception handling (UIErrorHandler). There are also utility classes such as ConsoleManager for handling console IO.

- Main Application:
- The Main.kt file that is the entry point to the application. It is responsible for initializing and connecting the Domain and Infrastructure layers.

##### Relationships and Data Flow
- The Main entry point initializes:
- The Domain layer, setting up all the services, use cases, and value objects.
- The Infrastructure layer’s UI components (controller, actions, presenters) that interact with the user via the command line.
- The UI controller serves as a mediator:
- It receives user input, triggers the corresponding use cases in the Domain layer and processes the results.
- Persistence:
- The repository abstraction defined in the Domain layer is implemented by HabitRepositoryImpl. The Domain communicates with persistence via this repository interface.

##### Architectural Patterns and Design Principles
- Layered / Clean Architecture:
- Clear separation among Domain (business logic), Infrastructure (implementation details – UI and persistence), and the application bootstrapping (Main).
- SOLID Principles in Action:
- Single Responsibility: Each class has a well-defined role (e.g., ConsoleManager for console IO, HabitRepositoryImpl solely for data persistence).
- Open/Closed and Dependency Injection: New actions can be added to the UI without altering the Menu logic.
- Interaction flows use dependency injection and interfaces to ensure consistent, testable, and isolated layers.

##### Relevant Technologies
- Kotlin is the primary programming language.
- Gradle is used as the build tool.
- MongoDB (as suggested by the MongoDBConnection) for persistence.
- The use of interfaces for actions, presenters, and repository access ensures loose coupling.

##### Elements
- Annotations or legends to denote SOLID principles where they are applied (e.g., label “SRP” near ConsoleManager, “OCP” near menu actions).

a. A block labeled “Main (Main.kt)” at the top or center.
- Left: Domain Layer with sub-blocks for Entities (Habit, HabitStatistics), Use Cases (Add, Delete, Complete, etc.), Services, Mappers, DTOs, Exceptions, and Utilities.
- Right: Infrastructure Layer, divided into:
- UI Part: with blocks for HabitTrackerController, Menu (Menu, MenuConfiguration, MenuOption, ConsoleMenuPresenter, HabitSelector, etc.), and Action implementations.
- Persistence Part: showing HabitRepositoryImpl and MongoDBConnection.
- E.g., Menu → Action → Use Case → Repository.
d. Use annotations to indicate the responsibilities and note technology choices.

## Diagram (Mermaid)

```mermaid
flowchart TD
    idMain["Main (Main.kt)"]:::main

    subgraph "Domain Layer"
        idHabit["Habit"]:::domain
        idHabitStats["HabitStatistics"]:::domain
        idDTO["DTOs"]:::domain
        idExceptions["Exceptions"]:::error
        idHabitErrorHandler["HabitErrorHandler"]:::error
        idMappers["Mappers"]:::domain
        idHabitFinder["HabitFinder"]:::domain
        idHabitStatCalc["HabitStatisticsCalculator"]:::domain
        idAddUseCase["AddHabitUseCase"]:::domain
        idCompleteUseCase["CompleteHabitUseCase"]:::domain
        idDeleteUseCase["DeleteHabitUseCase"]:::domain
        idListUseCase["ListHabitsUseCase"]:::domain
        idCalcStatsUseCase["CalculateHabitStatisticsUseCase"]:::domain
        idClock["Clock"]:::utility
    end

    subgraph "Infrastructure Layer"
        subgraph "UI"
            idController["HabitTrackerController"]:::ui
            idHabitAction["HabitAction"]:::ui
            idAddAction["AddHabitAction"]:::ui
            idCompleteAction["CompleteHabitAction"]:::ui
            idDeleteAction["DeleteHabitAction"]:::ui
            idExitAction["ExitAction"]:::ui
            idListAction["ListHabitsAction"]:::ui
            idShowStatsAction["ShowHabitStatisticsAction"]:::ui
            idMenu["Menu"]:::ui
            idMenuOption["MenuOption"]:::ui
            idConsoleMenuPresenter["ConsoleMenuPresenter"]:::ui
            idMenuPresenter["MenuPresenter"]:::ui
            idHabitSelector["HabitSelector"]:::ui
            idUIErrorHandler["UIErrorHandler"]:::error
            idConsoleManager["ConsoleManager"]:::utility
        end
        subgraph "Persistence"
            idMongo["MongoDBConnection"]:::persistence
            idRepoImpl["HabitRepositoryImpl"]:::persistence
        end
        subgraph "Infra Utilities"
            idMessages["Messages"]:::utility
        end
    end

    idMain -->|"calls"| idController

    idController -->|"displays"| idMenu
    idMenu -->|"selects"| idAddAction
    idMenu -->|"selects"| idCompleteAction
    idMenu -->|"selects"| idDeleteAction
    idMenu -->|"selects"| idListAction
    idMenu -->|"selects"| idShowStatsAction
    idMenu -->|"selects"| idExitAction

    idAddAction -->|"triggers"| idAddUseCase
    idCompleteAction -->|"triggers"| idCompleteUseCase
    idDeleteAction -->|"triggers"| idDeleteUseCase
    idListAction -->|"triggers"| idListUseCase
    idShowStatsAction -->|"triggers"| idCalcStatsUseCase

    idAddUseCase -->|"uses"| idHabitFinder
    idCompleteUseCase -->|"uses"| idHabitFinder
    idDeleteUseCase -->|"uses"| idHabitFinder
    idListUseCase -->|"uses"| idHabitFinder
    idCalcStatsUseCase -->|"uses"| idHabitStatCalc

    idAddUseCase -->|"repo"| idRepoImpl
    idCompleteUseCase -->|"repo"| idRepoImpl
    idDeleteUseCase -->|"repo"| idRepoImpl
    idListUseCase -->|"repo"| idRepoImpl
    idCalcStatsUseCase -->|"repo"| idRepoImpl

    idRepoImpl -->|"connects"| idMongo

    idController -->|"presents"| idConsoleMenuPresenter
    idController -->|"selectsHabit"| idHabitSelector
    idController -->|"error"| idUIErrorHandler
    idController -->|"io"| idConsoleManager
    idConsoleMenuPresenter -->|"io"| idConsoleManager
    idMenu -->|"presents"| idMenuPresenter

    idAddUseCase -->|"time"| idClock
    idAddUseCase -.->|"handleError"| idHabitErrorHandler
    idCompleteUseCase -.->|"handleError"| idHabitErrorHandler
    idDeleteUseCase -.->|"handleError"| idHabitErrorHandler
    idListUseCase -.->|"handleError"| idHabitErrorHandler
    idCalcStatsUseCase -.->|"handleError"| idHabitErrorHandler

    classDef main fill:#f96,stroke:#333,stroke-width:2px;
    classDef domain fill:#bbf,stroke:#333,stroke-width:2px;
    classDef ui fill:#9f6,stroke:#333,stroke-width:2px;
    classDef persistence fill:#f66,stroke:#333,stroke-width:2px;
    classDef utility fill:#fc9,stroke:#333,stroke-width:2px;
    classDef error fill:#f99,stroke:#333,stroke-width:2px;

    click idMain "https://github.com/alanlapierre/habit-tracker/blob/main/src/main/kotlin/Main.kt"
    click idHabit "https://github.com/alanlapierre/habit-tracker/blob/main/src/main/kotlin/cli/domain/model/Habit.kt"
    click idHabitStats "https://github.com/alanlapierre/habit-tracker/blob/main/src/main/kotlin/cli/domain/model/HabitStatistics.kt"
    click idDTO "https://github.com/alanlapierre/habit-tracker/blob/main/src/main/kotlin/cli/domain/dto/Dtos.kt"
    click idExceptions "https://github.com/alanlapierre/habit-tracker/blob/main/src/main/kotlin/cli/domain/exception/Exceptions.kt"
    click idHabitErrorHandler "https://github.com/alanlapierre/habit-tracker/blob/main/src/main/kotlin/cli/domain/exception/HabitErrorHandler.kt"
    click idMappers "https://github.com/alanlapierre/habit-tracker/blob/main/src/main/kotlin/cli/domain/mapper/Mappers.kt"
    click idHabitFinder "https://github.com/alanlapierre/habit-tracker/blob/main/src/main/kotlin/cli/domain/service/HabitFinder.kt"
    click idHabitStatCalc "https://github.com/alanlapierre/habit-tracker/blob/main/src/main/kotlin/cli/domain/service/HabitStatisticsCalculator.kt"
    click idAddUseCase "https://github.com/alanlapierre/habit-tracker/blob/main/src/main/kotlin/cli/domain/usecase/AddHabitUseCase.kt"
    click idCompleteUseCase "https://github.com/alanlapierre/habit-tracker/blob/main/src/main/kotlin/cli/domain/usecase/CompleteHabitUseCase.kt"
    click idDeleteUseCase "https://github.com/alanlapierre/habit-tracker/blob/main/src/main/kotlin/cli/domain/usecase/DeleteHabitUseCase.kt"
    click idListUseCase "https://github.com/alanlapierre/habit-tracker/blob/main/src/main/kotlin/cli/domain/usecase/ListHabitsUseCase.kt"
    click idCalcStatsUseCase "https://github.com/alanlapierre/habit-tracker/blob/main/src/main/kotlin/cli/domain/usecase/CalculateHabitStatisticsUseCase.kt"
    click idClock "https://github.com/alanlapierre/habit-tracker/blob/main/src/main/kotlin/cli/domain/utils/Clock.kt"
    click idMongo "https://github.com/alanlapierre/habit-tracker/blob/main/src/main/kotlin/cli/infrastructure/persistence/MongoDBConnection.kt"
    click idRepoImpl "https://github.com/alanlapierre/habit-tracker/blob/main/src/main/kotlin/cli/infrastructure/persistence/repository/HabitRepositoryImpl.kt"
    click idController "https://github.com/alanlapierre/habit-tracker/blob/main/src/main/kotlin/cli/infrastructure/ui/HabitTrackerController.kt"
    click idHabitAction "https://github.com/alanlapierre/habit-tracker/blob/main/src/main/kotlin/cli/infrastructure/ui/action/HabitAction.kt"
    click idAddAction "https://github.com/alanlapierre/habit-tracker/blob/main/src/main/kotlin/cli/infrastructure/ui/action/impl/AddHabitAction.kt"
    click idCompleteAction "https://github.com/alanlapierre/habit-tracker/blob/main/src/main/kotlin/cli/infrastructure/ui/action/impl/CompleteHabitAction.kt"
    click idDeleteAction "https://github.com/alanlapierre/habit-tracker/blob/main/src/main/kotlin/cli/infrastructure/ui/action/impl/DeleteHabitAction.kt"
    click idExitAction "https://github.com/alanlapierre/habit-tracker/blob/main/src/main/kotlin/cli/infrastructure/ui/action/impl/ExitAction.kt"
    click idListAction "https://github.com/alanlapierre/habit-tracker/blob/main/src/main/kotlin/cli/infrastructure/ui/action/impl/ListHabitsAction.kt"
    click idShowStatsAction "https://github.com/alanlapierre/habit-tracker/blob/main/src/main/kotlin/cli/infrastructure/ui/action/impl/ShowHabitStatisticsAction.kt"
    click idHabitSelector "https://github.com/alanlapierre/habit-tracker/blob/main/src/main/kotlin/cli/infrastructure/ui/action/HabitSelector.kt"
    click idMenu "https://github.com/alanlapierre/habit-tracker/blob/main/src/main/kotlin/cli/infrastructure/ui/menu/Menu.kt"
    click idMenuOption "https://github.com/alanlapierre/habit-tracker/blob/main/src/main/kotlin/cli/infrastructure/ui/menu/MenuOption.kt"
    click idConsoleMenuPresenter "https://github.com/alanlapierre/habit-tracker/blob/main/src/main/kotlin/cli/infrastructure/ui/menu/presenter/ConsoleMenuPresenter.kt"
    click idMenuPresenter "https://github.com/alanlapierre/habit-tracker/blob/main/src/main/kotlin/cli/infrastructure/ui/menu/presenter/MenuPresenter.kt"
    click idUIErrorHandler "https://github.com/alanlapierre/habit-tracker/blob/main/src/main/kotlin/cli/infrastructure/ui/exception/UIErrorHandler.kt"
    click idConsoleManager "https://github.com/alanlapierre/habit-tracker/blob/main/src/main/kotlin/cli/infrastructure/ui/utils/ConsoleManager.kt"
    click idMessages "https://github.com/alanlapierre/habit-tracker/blob/main/src/main/kotlin/cli/infrastructure/utils/Messages.kt"
```
