# vuthientan/be_cdw architecture

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

- Interactive diagram: https://gitdiagram.com/vuthientan/be_cdw
- Repository: https://github.com/vuthientan/be_cdw

## Overview

##### Project type and purpose
- This is a monolithic Spring Boot REST API for a food-ordering/inventory system.
- It exposes CRUD endpoints for users, roles, permissions, menu items, ingredients, carts, orders, reviews, stock-in batches, etc.
- It uses JWT + cookie filters for auth and also supports GitHub OAuth2 login, plus email notifications (account activation, password reset, invoices).

##### File structure
- Top-level:
– pom.xml, mvnw wrappers → Maven-built Spring Boot app
– src/main/java/com/cdw/cdw:
- configuration → security, JWT decoding, OAuth2 handlers, filters, message bundles
- controller → REST controllers (Authentication, User, MenuItem, Order, Inventory, Cart, Review, etc.)
- service → business logic for each domain area + EmailService + JwtPasswordResetService
- repository → Spring Data JPA repositories + Specifications for filtering
- domain/entity → JPA entities
- domain/dto → request/response DTOs for each endpoint
- mapper → MapStruct-style mappers between entities and DTOs
- exception → global and application exceptions
- CdwApplication.java → main class
– src/main/resources:
- application.yaml → configuration (DB, mail, security)
- db/migration → Flyway migrations
- templates → HTML email templates
- messages_*.properties → i18n messages
- No frontend code; purely a backend service.

##### README
- (Empty) — rely on file structure for insights.

a. Client (browser or mobile app)
b. API Gateway / HTTP interface (Spring MVC Controllers)
c. Security Layer
– JwtFromCookieFilter, CustomJwtDecoder, JwtAuthenticationEntryPoint, OAuth2LoginSuccessHandler
d. Service Layer (AuthenticationService, UserService, OrderService, IngredientService, CartService, StockInService, ReviewService, EmailService, etc.)
e. Data Layer
– Spring Data Repositories + JPA Entities
– Flyway migrations → RDBMS (PostgreSQL/MySQL/etc.)
f. External Services
– GitHub OAuth2 provider
– SMTP server (for sending emails)

##### Relationships and interactions
- Client → Controllers (HTTP request)
– pre-filter: JwtFromCookieFilter extracts JWT from cookie
– SecurityConfig routes unauthenticated endpoints (login, password reset) and protects others
– Controllers delegate to Services
- Services apply business logic, call Repositories
- Repositories → Database
- OAuth2LoginSuccessHandler on successful GitHub login → issue local JWT
- EmailService → SMTP server
- All error flows → GlobalException handler

##### Architectural patterns and principles
- Layered (Controller → Service → Repository → Database)
- DTO mapping via mappers to decouple domain from API models
- Specifications pattern for flexible queries
- Externalized configuration (application.yaml, .env)
- Flyway for DB versioning
- Template engine for email content

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Client Layer
    subgraph "Client"
        direction TB
        Client["Client (Browser/Mobile)"]:::external
    end

    %% Security Layer
    subgraph "Security Layer"
        direction TB
        JwtFilter["JwtFromCookieFilter"]:::security
        JwtDecoder["CustomJwtDecoder"]:::security
        AuthEntry["JwtAuthenticationEntryPoint"]:::security
        OAuthHandler["OAuth2LoginSuccessHandler"]:::security
        SecurityCfg["SecurityConfig"]:::security
        PublicCfg["PublicEndpointConfig"]:::security
    end

    %% API Layer
    subgraph "API Layer (Controllers)"
        direction TB
        AuthCtrl["AuthenticationController"]:::api
        UserCtrl["UserController"]:::api
        RoleCtrl["RoleController"]:::api
        PermCtrl["PermissonController"]:::api
        MenuCtrl["MenuItemController"]:::api
        IngredCtrl["IngredientController"]:::api
        InvCtrl["InventoryController"]:::api
        CartCtrl["CartController"]:::api
        OrderCtrl["OrderController"]:::api
        ReviewCtrl["ReviewController"]:::api
        GithubCtrl["GithubOAuthController"]:::api
        ResetCtrl["PasswordResetController"]:::api
        LangCtrl["LanguageController"]:::api
    end

    %% Service Layer
    subgraph "Service Layer"
        direction TB
        AuthSvc["AuthenticationService"]:::service
        JwtResetSvc["JwtPasswordResetService"]:::service
        UserSvc["UserService"]:::service
        RoleSvc["RoleService"]:::service
        PermSvc["PermissionService"]:::service
        MenuSvc["MenuItemService"]:::service
        IngredSvc["IngredientService"]:::service
        StockInSvc["StockInService"]:::service
        CartSvc["CartService"]:::service
        OrderSvc["OrderService"]:::service
        ReviewSvc["ReviewService"]:::service
        EmailSvc["EmailService"]:::service
    end

    %% Mapping Layer
    subgraph "Mapping Layer (DTOs & Mappers)"
        direction TB
        DTOs["DTOs"]:::mapping
        Mappers["Mappers"]:::mapping
    end

    %% Repository Layer
    subgraph "Repository Layer"
        direction TB
        Repos["Repositories"]:::repo
        Specs["Specifications"]:::repo
    end

    %% Persistence Versioning
    Flyway["Flyway Migrations"]:::repo

    %% Database
    DB["Relational Database"]:::db

    %% External Services
    subgraph "External Services"
        direction TB
        GitHub["GitHub OAuth2"]:::external
        SMTP["SMTP Server"]:::external
    end

    %% Arrows
    Client -->|HTTP/HTTPS| JwtFilter
    JwtFilter --> SecurityCfg
    SecurityCfg --> AuthEntry
    SecurityCfg --> PublicCfg
    SecurityCfg --> AuthCtrl
    SecurityCfg --> UserCtrl
    SecurityCfg --> RoleCtrl
    SecurityCfg --> PermCtrl
    SecurityCfg --> MenuCtrl
    SecurityCfg --> IngredCtrl
    SecurityCfg --> InvCtrl
    SecurityCfg --> CartCtrl
    SecurityCfg --> OrderCtrl
    SecurityCfg --> ReviewCtrl
    SecurityCfg --> GithubCtrl
    SecurityCfg --> ResetCtrl
    SecurityCfg --> LangCtrl

    AuthCtrl --> AuthSvc
    UserCtrl --> UserSvc
    RoleCtrl --> RoleSvc
    PermCtrl --> PermSvc
    MenuCtrl --> MenuSvc
    IngredCtrl --> IngredSvc
    InvCtrl --> StockInSvc
    CartCtrl --> CartSvc
    OrderCtrl --> OrderSvc
    ReviewCtrl --> ReviewSvc
    GithubCtrl --> OAuthHandler
    OAuthHandler --> AuthSvc
    ResetCtrl --> JwtResetSvc

    AuthSvc --> Repos
    JwtResetSvc --> Repos
    UserSvc --> Repos
    RoleSvc --> Repos
    PermSvc --> Repos
    MenuSvc --> Repos
    IngredSvc --> Repos
    StockInSvc --> Repos
    CartSvc --> Repos
    OrderSvc --> Repos
    ReviewSvc --> Repos
    EmailSvc --> SMTP

    Repos --> DB
    Specs --> DB
    Flyway --> DB

    %% Click Events - Controllers
    click AuthCtrl "https://github.com/vuthientan/be_cdw/blob/master/src/main/java/com/cdw/cdw/controller/AuthenticationController.java"
    click UserCtrl "https://github.com/vuthientan/be_cdw/blob/master/src/main/java/com/cdw/cdw/controller/UserController.java"
    click RoleCtrl "https://github.com/vuthientan/be_cdw/blob/master/src/main/java/com/cdw/cdw/controller/RoleController.java"
    click PermCtrl "https://github.com/vuthientan/be_cdw/blob/master/src/main/java/com/cdw/cdw/controller/PermissonController.java"
    click MenuCtrl "https://github.com/vuthientan/be_cdw/blob/master/src/main/java/com/cdw/cdw/controller/MenuItemController.java"
    click IngredCtrl "https://github.com/vuthientan/be_cdw/blob/master/src/main/java/com/cdw/cdw/controller/IngredientController.java"
    click InvCtrl "https://github.com/vuthientan/be_cdw/blob/master/src/main/java/com/cdw/cdw/controller/InventoryController.java"
    click CartCtrl "https://github.com/vuthientan/be_cdw/blob/master/src/main/java/com/cdw/cdw/controller/CartController.java"
    click OrderCtrl "https://github.com/vuthientan/be_cdw/blob/master/src/main/java/com/cdw/cdw/controller/OrderController.java"
    click ReviewCtrl "https://github.com/vuthientan/be_cdw/blob/master/src/main/java/com/cdw/cdw/controller/ReviewController.java"
    click GithubCtrl "https://github.com/vuthientan/be_cdw/blob/master/src/main/java/com/cdw/cdw/controller/GithubOAuthController.java"
    click ResetCtrl "https://github.com/vuthientan/be_cdw/blob/master/src/main/java/com/cdw/cdw/controller/PasswordResetController.java"
    click LangCtrl "https://github.com/vuthientan/be_cdw/blob/master/src/main/java/com/cdw/cdw/controller/LanguageController.java"

    %% Click Events - Security
    click JwtFilter "https://github.com/vuthientan/be_cdw/blob/master/src/main/java/com/cdw/cdw/configuration/JwtFromCookieFilter.java"
    click JwtDecoder "https://github.com/vuthientan/be_cdw/blob/master/src/main/java/com/cdw/cdw/configuration/CustomJwtDecoder.java"
    click AuthEntry "https://github.com/vuthientan/be_cdw/blob/master/src/main/java/com/cdw/cdw/configuration/JwtAuthenticationEntryPoint.java"
    click OAuthHandler "https://github.com/vuthientan/be_cdw/blob/master/src/main/java/com/cdw/cdw/configuration/OAuth2LoginSuccessHandler.java"
    click SecurityCfg "https://github.com/vuthientan/be_cdw/blob/master/src/main/java/com/cdw/cdw/configuration/SecurityConfig.java"
    click PublicCfg "https://github.com/vuthientan/be_cdw/blob/master/src/main/java/com/cdw/cdw/configuration/PublicEndpointConfig.java"

    %% Click Events - Services
    click AuthSvc "https://github.com/vuthientan/be_cdw/blob/master/src/main/java/com/cdw/cdw/service/AuthenticationService.java"
    click JwtResetSvc "https://github.com/vuthientan/be_cdw/blob/master/src/main/java/com/cdw/cdw/service/JwtPasswordResetService.java"
    click UserSvc "https://github.com/vuthientan/be_cdw/blob/master/src/main/java/com/cdw/cdw/service/UserService.java"
    click RoleSvc "https://github.com/vuthientan/be_cdw/blob/master/src/main/java/com/cdw/cdw/service/RoleService.java"
    click PermSvc "https://github.com/vuthientan/be_cdw/blob/master/src/main/java/com/cdw/cdw/service/PermissionService.java"
    click MenuSvc "https://github.com/vuthientan/be_cdw/blob/master/src/main/java/com/cdw/cdw/service/MenuItemService.java"
    click IngredSvc "https://github.com/vuthientan/be_cdw/blob/master/src/main/java/com/cdw/cdw/service/IngredientService.java"
    click StockInSvc "https://github.com/vuthientan/be_cdw/blob/master/src/main/java/com/cdw/cdw/service/StockInService.java"
    click CartSvc "https://github.com/vuthientan/be_cdw/blob/master/src/main/java/com/cdw/cdw/service/CartService.java"
    click OrderSvc "https://github.com/vuthientan/be_cdw/blob/master/src/main/java/com/cdw/cdw/service/OrderService.java"
    click ReviewSvc "https://github.com/vuthientan/be_cdw/blob/master/src/main/java/com/cdw/cdw/service/ReviewService.java"
    click EmailSvc "https://github.com/vuthientan/be_cdw/blob/master/src/main/java/com/cdw/cdw/service/EmailService.java"

    %% Click Events - Repositories
    click Repos "https://github.com/vuthientan/be_cdw/tree/master/src/main/java/com/cdw/cdw/repository"
    click Specs "https://github.com/vuthientan/be_cdw/tree/master/src/main/java/com/cdw/cdw/repository/spec"

    %% Click Events - Flyway
    click Flyway "https://github.com/vuthientan/be_cdw/tree/master/src/main/resources/db/migration"

    %% Styles
    classDef api fill:#D0E6FF,stroke:#005BB5,color:#005BB5;
    classDef service fill:#E6F7E6,stroke:#228B22,color:#228B22;
    classDef repo fill:#FFE6CC,stroke:#FF8C00,color:#FF8C00;
    classDef security fill:#F3E6FF,stroke:#800080,color:#800080;
    classDef external fill:#F0F0F0,stroke:#808080,color:#808080;
    classDef mapping fill:#FFF7E6,stroke:#B8860B,color:#B8860B;
    classDef db fill:#FFE6E6,stroke:#B22222,color:#B22222;
```
