# nightmoon1996/catfacttodotracker architecture

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

- Interactive diagram: https://gitdiagram.com/nightmoon1996/catfacttodotracker
- Repository: https://github.com/nightmoon1996/catfacttodotracker

## Overview

##### Project type and purpose
- This is a backend‐only RESTful API built with .NET 9 Minimal APIs.
- It exposes authentication endpoints (register/login) and ToDo endpoints that enrich new ToDos with a cat fact and current Bangkok weather.
- Uses an in‐memory EF Core database, JWT for security, external HTTP calls, and Swagger for docs.

##### File structure
- Program.cs: bootstraps the web host, configures services & endpoint routing.
- Data/ApplicationDbContext.cs: EF Core DbContext with DbSets for Users and TodoItems.
- Models/*.cs: DTOs and domain entities (User, TodoItem) plus response models (CatFactResponse, WeatherResponse).
- Services/AuthService.cs & TodoService.cs: business logic for authentication and ToDo operations (incl. external HTTP calls).
- appsettings.json / appsettings.Development.json: configuration (JWT keys, external API URLs).
- Properties/launchSettings.json & Swagger: dev tooling and API documentation.

##### Key README insights
- JWT Bearer Authentication with BCrypt password hashing.
- External services: “cat fact” API and weather API (hard‐coded to Bangkok).
- In‐memory EF Core – no external database server, but still a distinct data layer.
- Minimal API style – no MVC controllers, just endpoints in Program.cs wired to services.

##### Main components
a. Client (e.g. Postman or Browser)
b. ASP.NET Core Minimal API Layer (Program.cs hosting endpoints)
c. Authentication Module
- JWT Bearer middleware
- AuthService (register/login + token gen)
d. ToDo Module
- TodoService
- External HTTP Client A: Cat Fact API
- External HTTP Client B: Weather API
e. Data Layer
- ApplicationDbContext (EF Core In‐Memory DB)
- Entities: User, TodoItem
f. API Documentation
- Swagger / OpenAPI UI
g. Configuration Store
- appsettings.json / Environment Variables

##### Relationships & data‐flow
1. Client → HTTP POST /api/auth/register or /api/auth/login → Minimal API endpoint
→ AuthService → ApplicationDbContext → (User table) → returns JWT
2. Client → HTTP GET/POST /api/todos with Bearer token → JWT Middleware validates →
Minimal API endpoint → TodoService
a. If POST:
i. Create TodoItem entity in DbContext
ii. Call Cat Fact API over HTTP → map to CatFactResponse
iii. Call Weather API over HTTP → map to WeatherResponse
iv. Save enriched TodoItem in DbContext → return to client
b. If GET:
i. Query DB for user’s TodoItems → include cat fact and weather fields
ii. Return list to client
3. Swagger UI → served by Swashbuckle middleware

##### Architectural patterns & principles
- Layered architecture: Presentation (endpoints) → Business (Services) → Data (EF Core)
- Dependency Injection for services & DbContext
- External integration via HTTP Client in TodoService
- Secure perimeter enforced by JWT middleware
- Configuration abstraction via IConfiguration and environment overrides

##### Final advice
- Be exhaustive: represent each service class, middleware, and external integration.
- Configuration sources (appsettings.json vs env vars).
- Swagger as a sidecar of the API layer.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Client Layer
    subgraph "Client/User Interaction"
        Client["Client\n(Postman/Browser)"]:::externalUser
    end

    %% API Layer
    subgraph "API Layer"
        Program["Minimal API\nProgram.cs"]:::api
        JWTMiddleware["JWT Bearer Middleware"]:::middleware
        SwaggerUI["Swagger / OpenAPI UI"]:::swagger
    end

    %% Service Layer
    subgraph "Service Layer"
        AuthService["AuthService"]:::service
        TodoService["TodoService"]:::service
    end

    %% Data Layer
    subgraph "Data Layer"
        DbContext["ApplicationDbContext"]:::db
        subgraph "Models"
            UserEntity["User"]:::entity
            TodoEntity["TodoItem"]:::entity
            RegisterModel["RegisterModel"]:::entity
            LoginModel["LoginModel"]:::entity
            TodoCreateModel["TodoCreateModel"]:::entity
            TodoViewModel["TodoViewModel"]:::entity
            CatFactResponse["CatFactResponse"]:::entity
            WeatherResponse["WeatherResponse"]:::entity
        end
    end

    %% External Services
    subgraph "External Services"
        CatFactAPI["Cat Fact API"]:::external
        WeatherAPI["Weather API"]:::external
    end

    %% Configuration & Tooling
    subgraph "Configuration & Tooling"
        AppSettings["appsettings.json\nappsettings.Development.json"]:::config
        LaunchSettings["launchSettings.json"]:::config
        SolutionFile["TodoList.sln"]:::config
        ProjectFile["TodoList.csproj"]:::config
        README["README.md"]:::config
    end

    %% Connections
    Client -->|HTTPS + JWT Bearer| Program
    Program -->|uses| JWTMiddleware
    Program -->|routes to| AuthService
    Program -->|routes to| TodoService
    Program -->|serves| SwaggerUI

    AuthService -.->|EF Core In-Memory| DbContext
    TodoService -.->|EF Core In-Memory| DbContext

    DbContext -->|manages| UserEntity
    DbContext -->|manages| TodoEntity

    TodoService -->|HTTP GET| CatFactAPI
    TodoService -->|HTTP GET| WeatherAPI

    AppSettings -->|provides config| Program
    LaunchSettings -->|dev tooling| SwaggerUI
    SolutionFile -->|solution| ProjectFile

    %% Click Events
    click Program "https://github.com/nightmoon1996/catfacttodotracker/blob/main/Program.cs"
    click DbContext "https://github.com/nightmoon1996/catfacttodotracker/blob/main/Data/ApplicationDbContext.cs"
    click UserEntity "https://github.com/nightmoon1996/catfacttodotracker/blob/main/Models/User.cs"
    click TodoEntity "https://github.com/nightmoon1996/catfacttodotracker/blob/main/Models/TodoItem.cs"
    click RegisterModel "https://github.com/nightmoon1996/catfacttodotracker/blob/main/Models/RegisterModel.cs"
    click LoginModel "https://github.com/nightmoon1996/catfacttodotracker/blob/main/Models/LoginModel.cs"
    click TodoCreateModel "https://github.com/nightmoon1996/catfacttodotracker/blob/main/Models/TodoCreateModel.cs"
    click TodoViewModel "https://github.com/nightmoon1996/catfacttodotracker/blob/main/Models/TodoViewModel.cs"
    click CatFactResponse "https://github.com/nightmoon1996/catfacttodotracker/blob/main/Models/CatFactResponse.cs"
    click WeatherResponse "https://github.com/nightmoon1996/catfacttodotracker/blob/main/Models/WeatherResponse.cs"
    click AuthService "https://github.com/nightmoon1996/catfacttodotracker/blob/main/Services/AuthService.cs"
    click TodoService "https://github.com/nightmoon1996/catfacttodotracker/blob/main/Services/TodoService.cs"
    click AppSettings "https://github.com/nightmoon1996/catfacttodotracker/blob/main/appsettings.json"
    click LaunchSettings "https://github.com/nightmoon1996/catfacttodotracker/blob/main/Properties/launchSettings.json"
    click SolutionFile "https://github.com/nightmoon1996/catfacttodotracker/blob/main/TodoList.sln"
    click ProjectFile "https://github.com/nightmoon1996/catfacttodotracker/blob/main/TodoList.csproj"
    click README "https://github.com/nightmoon1996/catfacttodotracker/blob/main/README.md"

    %% Styles
    classDef api fill:#D0E8FF,stroke:#3399FF
    classDef service fill:#D0E8FF,stroke:#3399FF
    classDef db fill:#FFE0B2,stroke:#FF9900
    classDef entity fill:#FFE0B2,stroke:#FF9900
    classDef external fill:#D0FFD6,stroke:#22AA33
    classDef middleware fill:#EEEEEE,stroke:#888888
    classDef config fill:#EEEEEE,stroke:#888888
    classDef swagger fill:#EEEEEE,stroke:#888888
    classDef externalUser fill:#EEEEEE,stroke:#888888
```
