# apexnewbie/ai-health-management architecture

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

- Interactive diagram: https://gitdiagram.com/apexnewbie/ai-health-management
- Repository: https://github.com/apexnewbie/ai-health-management

## Overview

##### Project type and purpose
- This is a full-stack AI Health Management System:
- Backend: Spring Boot REST API (Java 17, Spring Security, JWT, MyBatis, Redis)
- Frontend: React static pages (fitness & diet UI, chat interface next)
- Persistence: Relational DB (SQL scripts in docs/V1_user.sql) and Redis cache
- Planned integrations: LLM chat, nutrition DB, blockchain/NFT, scheduling service

##### File structure
- src/main/java/com/example/aihealthmanagement:
- Application entry point (AiHealthManagementApplication.java)
- config: SecurityConfig, WebConfig
- controller: AuthController, ProfileController (REST endpoints)
- service / service/impl: business logic (AuthServiceImpl, CustomUserDetailsService)
- repository: UserRepository (MyBatis/JPA)
- security: JWT filter, token provider, CustomUserDetails
- common/exception: ServiceResponse, ServiceException, GlobalExceptionHandler
- dto & entity: data transfer objects and JPA entities
- src/main/resources/application.properties: DB & Redis configs, JWT settings
- docs: SQL schema and structure notes
- README: lists implemented features (authentication, global error handling, static React UI) and future modules

##### Key architectural patterns & design principles
- Layered architecture: Controller → Service → Repository → Database
- Stateless authentication: JWT issued by AuthService → validated by JwtAuthenticationFilter
- Cross-cutting concerns: GlobalExceptionHandler, unified ServiceResponse wrapper
- External integrations: AI/LLM, nutrition API, blockchain node

##### Main components
a. Clients
- Web Browser / Mobile app (React UI)
b. API Gateway / Spring Boot Backend
- Security Layer (JWT filter, Spring Security)
- API Layer (Controllers)
- Business Layer (Services)
- Data Layer (Repositories, MyBatis/JPA)
c. Data Stores
- Relational Database (MySQL/PostgreSQL)
- Redis Cache
d. External Services
- LLM Chat API (psychological support)
- Nutrition Database API
- Blockchain/NFT node
- Scheduling/Notification Service
e. DevOps & Monitoring (optional)
- Swagger/OpenAPI UI
- CI pipeline

##### Relationships & interactions
- Client → HTTP(S) → Spring Boot REST endpoints
- Security Filter validates/attaches JWT → Controller
- Controller invokes Service → Repository
- Repository ↔ Database (CRUD)
- Service ↔ Redis (cache tokens, session data)
- Service ↔ External LLM / Nutrition / Blockchain APIs
- Errors bubble up → GlobalExceptionHandler → unified ServiceResponse

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Clients
    subgraph "Frontend" 
        direction TB
        Browser["Web Browser / Mobile App (React UI)"]:::frontend
    end

    %% Backend Application
    subgraph "Spring Boot Application" 
        direction TB
        AEP["AiHealthManagementApplication"]:::backend
        subgraph "Configuration Layer"
            direction TB
            SC["SecurityConfig"]:::backend
            WC["WebConfig"]:::backend
        end
        subgraph "Security Layer"
            direction TB
            JAF["JwtAuthenticationFilter"]:::backend
            JTP["JwtTokenProvider"]:::backend
            CUD["CustomUserDetails"]:::backend
        end
        subgraph "API Layer (Controllers)"
            direction TB
            AC["AuthController"]:::backend
            PC["ProfileController"]:::backend
        end
        subgraph "Business Layer (Services)"
            direction TB
            AS["AuthService"]:::backend
            CUDS["CustomUserDetailsService"]:::backend
            ASI["AuthServiceImpl"]:::backend
        end
        subgraph "Data Layer (Repository)"
            direction TB
            UR["UserRepository"]:::backend
        end
        subgraph "Domain Models & DTOs"
            direction TB
            UE["User Entity"]:::backend
            AD["Auth DTOs"]:::backend
        end
        subgraph "Cross-Cutting Concerns"
            direction TB
            GEH["GlobalExceptionHandler"]:::backend
            SE["ServiceException"]:::backend
            SR["ServiceResponse"]:::backend
        end
    end

    %% Data Stores
    subgraph "Data Stores"
        direction TB
        DB["Relational DB (MySQL/PostgreSQL)"]:::datastore
        RDC["Redis Cache"]:::datastore
    end

    %% External Services
    subgraph "External Services"
        direction TB
        LLM["LLM Chat API"]:::external
        NUT["Nutrition Database API"]:::external
        BC["Blockchain/NFT Node"]:::external
        SCH["Scheduling/Notification Service"]:::external
    end

    %% Connections
    Browser -->|"HTTP(S) Requests"| JAF
    JAF -->|"JWT validation"| AC
    AC -->|"invokes"| AS
    PC -->|"invokes"| AS
    AS -->|"uses"| CUDS
    AS -->|"calls"| UR
    UR -->|"CRUD operations"| DB
    AS -->|"cache tokens/data"| RDC
    AS -->|"calls"| LLM
    AS -->|"calls"| NUT
    AS -->|"calls"| BC
    AS -->|"schedules tasks"| SCH
    SC --> AEP
    WC --> AEP
    AEP --> SC
    AEP --> WC
    GEH --> AC
    GEH --> PC

    %% Swagger UI
    Swagger["Swagger/OpenAPI UI"]:::frontend
    Browser -->|"access docs"| Swagger

    %% Click Events
    click AEP "https://github.com/apexnewbie/ai-health-management/blob/main/src/main/java/com/example/aihealthmanagement/AiHealthManagementApplication.java"
    click SC "https://github.com/apexnewbie/ai-health-management/blob/main/src/main/java/com/example/aihealthmanagement/config/SecurityConfig.java"
    click WC "https://github.com/apexnewbie/ai-health-management/blob/main/src/main/java/com/example/aihealthmanagement/config/WebConfig.java"
    click JAF "https://github.com/apexnewbie/ai-health-management/blob/main/src/main/java/com/example/aihealthmanagement/security/JwtAuthenticationFilter.java"
    click JTP "https://github.com/apexnewbie/ai-health-management/blob/main/src/main/java/com/example/aihealthmanagement/security/JwtTokenProvider.java"
    click CUD "https://github.com/apexnewbie/ai-health-management/blob/main/src/main/java/com/example/aihealthmanagement/security/CustomUserDetails.java"
    click AC "https://github.com/apexnewbie/ai-health-management/blob/main/src/main/java/com/example/aihealthmanagement/controller/AuthController.java"
    click PC "https://github.com/apexnewbie/ai-health-management/blob/main/src/main/java/com/example/aihealthmanagement/controller/ProfileController.java"
    click AS "https://github.com/apexnewbie/ai-health-management/blob/main/src/main/java/com/example/aihealthmanagement/service/AuthService.java"
    click CUDS "https://github.com/apexnewbie/ai-health-management/blob/main/src/main/java/com/example/aihealthmanagement/service/CustomUserDetailsService.java"
    click ASI "https://github.com/apexnewbie/ai-health-management/blob/main/src/main/java/com/example/aihealthmanagement/service/impl/AuthServiceImpl.java"
    click UR "https://github.com/apexnewbie/ai-health-management/blob/main/src/main/java/com/example/aihealthmanagement/repository/UserRepository.java"
    click UE "https://github.com/apexnewbie/ai-health-management/blob/main/src/main/java/com/example/aihealthmanagement/entity/User.java"
    click AD "https://github.com/apexnewbie/ai-health-management/blob/main/src/main/java/com/example/aihealthmanagement/dto/AuthDto.java"
    click GEH "https://github.com/apexnewbie/ai-health-management/blob/main/src/main/java/com/example/aihealthmanagement/exception/GlobalExceptionHandler.java"
    click SE "https://github.com/apexnewbie/ai-health-management/blob/main/src/main/java/com/example/aihealthmanagement/common/ServiceException.java"
    click SR "https://github.com/apexnewbie/ai-health-management/blob/main/src/main/java/com/example/aihealthmanagement/common/ServiceResponse.java"
    click APPPROPS "https://github.com/apexnewbie/ai-health-management/blob/main/src/main/resources/application.properties"
    click DBSCRIPT "https://github.com/apexnewbie/ai-health-management/blob/main/docs/V1_user.sql"
    click DOCSTRUCT "https://github.com/apexnewbie/ai-health-management/blob/main/docs/structure.md"
    click TESTS "https://github.com/apexnewbie/ai-health-management/blob/main/src/test/java/com/example/aihealthmanagement/AiHealthManagementApplicationTests.java"

    %% Styles
    classDef frontend fill:#E0F7FA,stroke:#006064,color:#006064
    classDef backend fill:#E8F5E9,stroke:#1B5E20,color:#1B5E20
    classDef datastore fill:#FFF3E0,stroke:#E65100,color:#E65100
    classDef external fill:#F3E5F5,stroke:#6A1B9A,color:#6A1B9A
```
