# normancao2702/cmpt213-asgn4-restapi architecture

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

- Interactive diagram: https://gitdiagram.com/normancao2702/cmpt213-asgn4-restapi
- Repository: https://github.com/normancao2702/cmpt213-asgn4-restapi

## Overview

##### Main Components
a. Front-End (Static Resources)
– index.html, game.js, game.css, audio assets in src/main/resources/public
– Served by Spring’s static resource handler at “/” on port 8080
b. Web Layer (Spring Boot Application)
– DispatcherServlet (Spring MVC)
– GameController (handles all /api/** endpoints)
– GlobalExceptionHandler (handles 404, 400, etc.)
c. DTO Layer
– ApiGameDTO, ApiBoardDTO, ApiLocationDTO convert model to JSON
d. Model Layer (Game Logic)
– MazeGame (holds player, cats, cheese)
– Maze, PathFinder, CellLocation, CellState, MoveDirection
– Cat (enemy logic)
e. In-Memory Store
– A simple List<MazeGame> inside GameController to track active games
f. Client Interaction
– Browser loads index.html
– game.js issues AJAX calls (GET /api/games, POST /api/games/{id}/moves, etc.)

##### Relationships & Data Flows
– Browser → HTTP GET “/” → Static index.html, CSS, JS
– game.js → HTTP GET/POST → GameController endpoints
– Controller receives JSON requests → invokes Model methods on MazeGame instances → returns DTO JSON responses → game.js updates UI
– Exception handler intercepts invalid calls → returns error codes to front-end

##### Architectural Patterns & Tech Stack
– Spring MVC (Controller → Model → DTO → View)
– RESTful API design (resource-oriented URIs, proper HTTP status codes)
– In-memory state management (no external database)
– Static resource serving from Spring Boot

##### Step-by-Step Construction
– Static Resource Handler
– DispatcherServlet
– GameController (with endpoints grouped)
– GlobalExceptionHandler beside the Controller
e. In the Model layer, show MazeGame, Maze, PathFinder, CellLocation, Cat, etc.
f. In-memory List<MazeGame> as a storage component connected to MazeGame.

##### Detail Without Over-Complicating
– Show how DTOs map to Models via factory methods.
– Indicate exception flows (e.g., GameNotFoundException → 404).

## Diagram (Mermaid)

```mermaid
flowchart TB
    %% Front-End Zone
    subgraph "Browser / Front-End"
        direction TB
        FE_HTML["index.html"]:::frontend
        FE_JS["game.js"]:::frontend
        FE_CSS["game.css"]:::frontend
        FE_AUDIO["BONK.WAV"]:::frontend
    end

    %% Backend REST API Zone
    subgraph "Spring Boot REST API"
        direction TB
        StaticHandler["Static Resource Handler"]:::rest
        Dispatcher["DispatcherServlet"]:::rest
        Controller["GameController\n(GET /api/games, POST /api/games/{id}/moves, etc.)"]:::rest
        ExceptionHandler["GlobalExceptionHandler"]:::rest
    end

    %% DTO Layer
    subgraph "DTO Layer"
        direction TB
        DTO_Game["ApiGameDTO"]:::dto
        DTO_Board["ApiBoardDTO"]:::dto
        DTO_Loc["ApiLocationDTO"]:::dto
    end

    %% Model Layer
    subgraph "Model Layer"
        direction TB
        Model_Game["MazeGame"]:::model
        Model_Maze["Maze"]:::model
        Model_PF["PathFinder"]:::model
        Model_CellLoc["CellLocation"]:::model
        Model_CellState["CellState"]:::model
        Model_MoveDir["MoveDirection"]:::model
        Model_Cat["Cat"]:::model
    end

    %% In-Memory Store
    InMemory["List<MazeGame> (In-Memory Store)"]:::storage

    %% Data Flows
    FE_HTML -->|"HTTP GET / → static resources"| StaticHandler
    FE_JS -->|"AJAX GET/POST /api"| Controller
    FE_CSS --> StaticHandler
    FE_AUDIO --> StaticHandler

    StaticHandler --> Dispatcher
    Dispatcher --> Controller
    Controller --> ExceptionHandler
    Controller --> DTO_Game
    Controller --> DTO_Board
    Controller --> DTO_Loc

    DTO_Game --> Model_Game
    DTO_Board --> Model_Maze
    DTO_Loc --> Model_CellLoc

    Model_Game --> Model_Maze
    Model_Game --> Model_PF
    Model_Game --> Model_CellLoc
    Model_Game --> Model_CellState
    Model_Game --> Model_MoveDir
    Model_Game --> Model_Cat

    Model_Game --> InMemory
    InMemory --> Model_Game

    Model_Game --> DTO_Game
    DTO_Game --> Controller
    Controller --> FE_JS

    ExceptionHandler -.-> FE_JS

    %% Click Events
    click FE_HTML "https://github.com/normancao2702/cmpt213-asgn4-restapi/blob/main/src/main/resources/public/index.html"
    click FE_JS "https://github.com/normancao2702/cmpt213-asgn4-restapi/blob/main/src/main/resources/public/scripts/game.js"
    click FE_CSS "https://github.com/normancao2702/cmpt213-asgn4-restapi/blob/main/src/main/resources/public/css/game.css"
    click FE_AUDIO "https://github.com/normancao2702/cmpt213-asgn4-restapi/blob/main/src/main/resources/public/res/BONK.WAV"
    click StaticHandler "https://github.com/normancao2702/cmpt213-asgn4-restapi/blob/main/src/main/java/ca/cmpt213/mazeapi/MazeGameApiApplication.java"
    click Controller "https://github.com/normancao2702/cmpt213-asgn4-restapi/blob/main/src/main/java/ca/cmpt213/mazeapi/controllers/GameController.java"
    click ExceptionHandler "https://github.com/normancao2702/cmpt213-asgn4-restapi/blob/main/src/main/java/ca/cmpt213/mazeapi/exceptions/GlobalExceptionHandler.java"
    click DTO_Game "https://github.com/normancao2702/cmpt213-asgn4-restapi/blob/main/src/main/java/ca/cmpt213/mazeapi/dto/ApiGameDTO.java"
    click DTO_Board "https://github.com/normancao2702/cmpt213-asgn4-restapi/blob/main/src/main/java/ca/cmpt213/mazeapi/dto/ApiBoardDTO.java"
    click DTO_Loc "https://github.com/normancao2702/cmpt213-asgn4-restapi/blob/main/src/main/java/ca/cmpt213/mazeapi/dto/ApiLocationDTO.java"
    click Model_Game "https://github.com/normancao2702/cmpt213-asgn4-restapi/blob/main/src/main/java/ca/cmpt213/mazeapi/model/MazeGame.java"
    click Model_Maze "https://github.com/normancao2702/cmpt213-asgn4-restapi/blob/main/src/main/java/ca/cmpt213/mazeapi/model/Maze.java"
    click Model_PF "https://github.com/normancao2702/cmpt213-asgn4-restapi/blob/main/src/main/java/ca/cmpt213/mazeapi/model/PathFinder.java"
    click Model_CellLoc "https://github.com/normancao2702/cmpt213-asgn4-restapi/blob/main/src/main/java/ca/cmpt213/mazeapi/model/CellLocation.java"
    click Model_CellState "https://github.com/normancao2702/cmpt213-asgn4-restapi/blob/main/src/main/java/ca/cmpt213/mazeapi/model/CellState.java"
    click Model_MoveDir "https://github.com/normancao2702/cmpt213-asgn4-restapi/blob/main/src/main/java/ca/cmpt213/mazeapi/model/MoveDirection.java"
    click Model_Cat "https://github.com/normancao2702/cmpt213-asgn4-restapi/blob/main/src/main/java/ca/cmpt213/mazeapi/model/Cat.java"
    click InMemory "https://github.com/normancao2702/cmpt213-asgn4-restapi/blob/main/src/main/java/ca/cmpt213/mazeapi/controllers/GameController.java"

    %% Styles
    classDef frontend fill:#e0f7fa,stroke:#006064,color:#006064;
    classDef rest fill:#e8f5e9,stroke:#2e7d32,color:#2e7d32;
    classDef dto fill:#fff9c4,stroke:#f9a825,color:#f57f17;
    classDef model fill:#fff3e0,stroke:#ef6c00,color:#e65100;
    classDef storage fill:#ffe0b2,stroke:#fb8c00,color:#e65100;
```
