# stepsisstuck/sp-y2-bed-ca architecture

> Architecture diagram of the GitHub repository stepsisstuck/sp-y2-bed-ca, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-05-12). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/stepsisstuck/sp-y2-bed-ca
- Repository: https://github.com/stepsisstuck/sp-y2-bed-ca

## Overview

##### Project type and purpose
- This is a full-stack web application built with Node.js, Express and MySQL.
- The public/ folder holds static HTML/CSS/JS views (login, register, battle, review, etc.).
- The src/ folder implements an MVC-style REST API: routes → middleware → controllers → models → database.
- Purpose: provide a quiz/battle interface, user authentication, class & question management, customization, and reviews.

##### File structure
- public/: Client-side UI assets (HTML pages + JS that calls back-end endpoints).
- src/config/db.js: MySQL connection configuration.
- src/routes/*.js: Express route definitions for each resource (auth, battle, class, customization, question, review, spells, user).
- src/middleware/auth.js: JWT verification middleware protecting private routes.
- src/controllers/*.js: Business logic for each resource; calls models to interact with the database.
- src/models/user.js (and any others if expanded): Data-access layer, SQL queries or ORM logic.
- initTable.js / init.sql: Database schema creation/reset scripts.
- src/server.js: Express app setup, static file serving, route mounting, middleware, listening port.

##### README for additional insights
- Tech stack: bcrypt (password hashing), dotenv (env vars), express, jsonwebtoken (JWT auth), mysql2 (DB), nodemon (dev), supertest (testing), readline-sync.
- Scripts: npm run init_tables to reset schema, npm run run to start server.
- Features: Home, login, register, quiz/question page, battle, user/scoreboard, spell, class, customization, review.

##### Main components and interactions (to be reflected
a. Client Browser
b. Web Server (Express App)
- Route Layer: authRoutes, questionRoutes, battleRoutes, etc.
- Middleware: JWT Authentication.
- Controller Layer: authController, questionController, battleController, etc.
- Model/Data Layer: user model, question model, class model, etc.
c. Database
- MySQL instance with schema initialized by init.sql.
d. External/Supporting Services
- JWT service (jsonwebtoken library), bcrypt service, dotenv.

1. Client → (HTTP requests for pages/assets) → Express static-file handler → serves HTML/JS/CSS.
2. Client JS → (AJAX/Fetch) → Express API endpoints (routes).
3. Route → Middleware (auth.js) → Controller.
4. Controller → Model → MySQL.
5. Model → Controller → Route → Client (JSON responses).

##### Architectural patterns and design principles
- MVC-style layering (Routes → Controllers → Models).
- RESTful endpoints grouped by resource.
- Stateless authentication via JWT.
- Single Express monolith (no microservices).

##### Technologies & frameworks to annotate
- Node.js, Express, mysql2, jsonwebtoken, bcrypt, dotenv.
- Front-end: Vanilla HTML/CSS/JS served from public/.

## Diagram (Mermaid)

```mermaid
flowchart TB
    %% Client Layer
    Client["Client Browser"]:::client
    Static["Front-end Static Assets"]:::client
    click Static "https://github.com/stepsisstuck/sp-y2-bed-ca/tree/main/public/"
    Client -->|"HTTP GET HTML/CSS/JS"| Static
    Client -->|"AJAX/Fetch API Calls"| SvrEntry

    %% Server Layer
    subgraph "Express Server"
        direction TB
        SvrEntry["Express App Entry (src/server.js)"]:::server
        click SvrEntry "https://github.com/stepsisstuck/sp-y2-bed-ca/blob/main/src/server.js"

        subgraph "API Routes"
            direction TB
            AuthR["Auth Routes"]:::server
            click AuthR "https://github.com/stepsisstuck/sp-y2-bed-ca/blob/main/src/routes/authRoutes.js"
            BattleR["Battle Routes"]:::server
            click BattleR "https://github.com/stepsisstuck/sp-y2-bed-ca/blob/main/src/routes/battleRoutes.js"
            ClassR["Class Routes"]:::server
            click ClassR "https://github.com/stepsisstuck/sp-y2-bed-ca/blob/main/src/routes/classRoutes.js"
            CustomR["Customization Routes"]:::server
            click CustomR "https://github.com/stepsisstuck/sp-y2-bed-ca/blob/main/src/routes/customizationRoutes.js"
            QuestionR["Question Routes"]:::server
            click QuestionR "https://github.com/stepsisstuck/sp-y2-bed-ca/blob/main/src/routes/questionRoutes.js"
            ReviewR["Review Routes"]:::server
            click ReviewR "https://github.com/stepsisstuck/sp-y2-bed-ca/blob/main/src/routes/reviewRoutes.js"
            SpellsR["Spells Routes"]:::server
            click SpellsR "https://github.com/stepsisstuck/sp-y2-bed-ca/blob/main/src/routes/spellsRoutes.js"
            UserR["User Routes"]:::server
            click UserR "https://github.com/stepsisstuck/sp-y2-bed-ca/blob/main/src/routes/userRoutes.js"
        end

        subgraph "Middleware"
            direction TB
            AuthM["JWT Authentication"]:::server
            click AuthM "https://github.com/stepsisstuck/sp-y2-bed-ca/blob/main/src/middleware/auth.js"
        end

        subgraph "Controllers"
            direction TB
            AuthC["Auth Controller"]:::server
            click AuthC "https://github.com/stepsisstuck/sp-y2-bed-ca/blob/main/src/controllers/authController.js"
            BattleC["Battle Controller"]:::server
            click BattleC "https://github.com/stepsisstuck/sp-y2-bed-ca/blob/main/src/controllers/battleController.js"
            ClassC["Class Controller"]:::server
            click ClassC "https://github.com/stepsisstuck/sp-y2-bed-ca/blob/main/src/controllers/classController.js"
            CustomC["Customization Controller"]:::server
            click CustomC "https://github.com/stepsisstuck/sp-y2-bed-ca/blob/main/src/controllers/customizationController.js"
            QuestionC["Question Controller"]:::server
            click QuestionC "https://github.com/stepsisstuck/sp-y2-bed-ca/blob/main/src/controllers/questionController.js"
            ReviewC["Review Controller"]:::server
            click ReviewC "https://github.com/stepsisstuck/sp-y2-bed-ca/blob/main/src/controllers/reviewController.js"
            SpellsC["Spells Controller"]:::server
            click SpellsC "https://github.com/stepsisstuck/sp-y2-bed-ca/blob/main/src/controllers/spellsController.js"
            UserC["User Controller"]:::server
            click UserC "https://github.com/stepsisstuck/sp-y2-bed-ca/blob/main/src/controllers/userController.js"
        end

        subgraph "Models"
            direction TB
            UserM["User Model"]:::server
            click UserM "https://github.com/stepsisstuck/sp-y2-bed-ca/blob/main/src/models/user.js"
        end

        %% Flows within server
        SvrEntry -->|"Mounts Routes"| AuthR
        SvrEntry -->|"Mounts Routes"| BattleR
        SvrEntry -->|"Mounts Routes"| ClassR
        SvrEntry -->|"Mounts Routes"| CustomR
        SvrEntry -->|"Mounts Routes"| QuestionR
        SvrEntry -->|"Mounts Routes"| ReviewR
        SvrEntry -->|"Mounts Routes"| SpellsR
        SvrEntry -->|"Mounts Routes"| UserR

        %% Request Flow
        AuthR -->|"invoke"| AuthM
        BattleR -->|"invoke"| AuthM
        ClassR -->|"invoke"| AuthM
        CustomR -->|"invoke"| AuthM
        QuestionR -->|"invoke"| AuthM
        ReviewR -->|"invoke"| AuthM
        SpellsR -->|"invoke"| AuthM
        UserR -->|"invoke"| AuthM

        AuthM -->|"authorize"| AuthC
        AuthM -->|"authorize"| BattleC
        AuthM -->|"authorize"| ClassC
        AuthM -->|"authorize"| CustomC
        AuthM -->|"authorize"| QuestionC
        AuthM -->|"authorize"| ReviewC
        AuthM -->|"authorize"| SpellsC
        AuthM -->|"authorize"| UserC

        AuthC -->|"DB Access"| UserM
        BattleC -->|"DB Access"| UserM
        ClassC -->|"DB Access"| UserM
        CustomC -->|"DB Access"| UserM
        QuestionC -->|"DB Access"| UserM
        ReviewC -->|"DB Access"| UserM
        SpellsC -->|"DB Access"| UserM
        UserC -->|"DB Access"| UserM

        UserM -->|"mysql2"| DB
    end

    %% Database Layer
    DB["MySQL Database"]:::db
    click DB "https://github.com/stepsisstuck/sp-y2-bed-ca/blob/main/src/config/db.js"

    subgraph "DB Initialization Scripts"
        direction TB
        InitJS["initTable.js"]:::dbscript
        click InitJS "https://github.com/stepsisstuck/sp-y2-bed-ca/blob/main/initTable.js"
        InitSQL["init.sql"]:::dbscript
        click InitSQL "https://github.com/stepsisstuck/sp-y2-bed-ca/blob/main/init.sql"
    end

    %% Connections
    Static -.->|"Static File Handler"| SvrEntry

    %% Styles
    classDef client fill:#add8e6,stroke:#000,color:#000
    classDef server fill:#90ee90,stroke:#000,color:#000
    classDef db fill:#ffcc99,stroke:#000,color:#000
    classDef dbscript fill:#ffd699,stroke:#000,color:#000
```
