# kian-drego/pantry_pal architecture

> Architecture diagram of the GitHub repository kian-drego/pantry_pal, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-02-20). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/kian-drego/pantry_pal
- Repository: https://github.com/kian-drego/pantry_pal

## Overview

##### What you’re diagramming (project type + purpose)
**PantryPal** is a **full-stack web application** with:
- A **static frontend** (HTML pages + Tailwind + ES6 JS) served separately (opened via `index.html` / Live Server).
- A **Node.js + Express REST API** backend (`Backend/server.js`).
- A **MongoDB** database accessed through **Mongoose models** (`Backend/models/*`).
- **JWT-based auth** + **bcrypt** password hashing.
- Social/engagement capabilities: follow/unfollow, likes, saves, comments, profile updates, recipe CRUD, global feed/search, “points” gamification.

From the tree, the “real” architecture is:

##### Frontend (static, multi-page)
Directory: `Frontend/`
Pages (each implies UI modules + API calls):
- `Loginpage.html`
- `create profile.html` (profile creation/update UI)
- `homepage.html` (feed)
- `create.html` (recipe creation)
- `edit-recipe.html` (recipe editing)
- `profilepage.html`
- `search.html`
- `leaderboard.html`
- `analytics.html`

Also top-level `index.html` likely acts as an entry point / landing.

##### Backend (REST API)
Directory: `Backend/`
- `server.js` → Express app bootstrap, middleware, route mounting, DB connection.
- `routes/auth.js` → `/api/auth` endpoints (register/login/profile/follow logic).
- `routes/recipes.js` → `/api/recipes` endpoints (CRUD/feed/likes/saves/comments).
- `models/User.js`, `models/Recipe.js` → Mongoose schemas (core domain objects).
- `.env` → `MONGO_URI`, `JWT_SECRET`, `PORT`.

##### Database
- MongoDB (external service), used via Mongoose.

##### What the README confirms (API boundaries + responsibilities)
The README explicitly defines a RESTful split:
- `/api/auth`: registration, login, profile updates, follow logic
- `/api/recipes`: recipe creation, global feed, likes, saves, comments

##### *(Optional but helpful)* Token/Session boundary (JWT stored client-side)

- Browser → API: `HTTPS REST (JSON)`
- API → MongoDB: `Mongoose queries (CRUD)`
- API → Browser: `JSON responses + JWT on login`
- Browser → Browser storage: `stores JWT (localStorage/sessionStorage/cookie—label what you actually do in code if known)`

Frontend served statically (Live Server) and backend on `PORT=5000`.

- **Express App (server.js)**
- Middleware: JSON parser, CORS, auth middleware (if implemented), error handling (if implemented)
- Mount routes:
- **Auth Router** (`routes/auth.js`) at `/api/auth`
- **Recipes Router** (`routes/recipes.js`) at `/api/recipes`

- **Domain Models (Mongoose)**
- **User Model** (`models/User.js`)
- fields to call out conceptually: username/@handle, password hash, bio/photo, followers/following, points (if stored), saved recipes, etc.
- **Recipe Model** (`models/Recipe.js`)
- fields: creator reference, ingredients, steps, image url/data, likes, saves, comments, timestamps

- Auth Router → User Model → MongoDB
- Recipes Router → Recipe Model → MongoDB
- Recipes Router ↔ User Model (for points, saved recipes, author info, etc.)
- Auth Router ↔ Recipe Model (if profile pages pull recipe lists; only include if actually done)

**Auth mechanisms**:
- `bcrypt` used in Auth Router during registration/login (hash/compare)
- `JWT` issued by Auth Router, verified by middleware for protected endpoints

##### Follow/unfollow flow
- Browser (Profile/Search) → `POST/DELETE /api/auth/follow/:userId` (or similar)
- API updates **User** document(s): follower/following arrays + counters
- API returns updated stats (real-time updates in UI)

##### Recipe CRUD + gamification
- Create recipe: Browser → `POST /api/recipes`
- API writes Recipe + increments creator points (+10) in **User**
- Edit/Delete: Browser → `PUT/DELETE /api/recipes/:id` (protected)
- Feed: Browser → `GET /api/recipes/feed` (or `GET /api/recipes`)

##### Engagement: likes/saves/comments
- Like: Browser → `POST /api/recipes/:id/like`
- Save: Browser → `POST /api/recipes/:id/save` (could update User.savedRecipes and/or Recipe.saves)
- Comment: Browser → `POST /api/recipes/:id/comments`

##### Search (dual search)
- Browser → `GET /api/recipes/search?q=...`
- Browser → `GET /api/auth/users/search?q=...` (or similar)
- Global sync note: UI updates across pages/modals after mutations (this is frontend state management behavior)

- Login/JWT
- Create recipe (+10 points)
- Follow/unfollow + real-time stat refresh

These three together will be the “best and most accurate” representation of this repo’s architecture.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% =======================
    %% Level 1: Container View
    %% =======================
    U["User (Home Chef)"]:::external

    subgraph "Client Device"
        B["Web Browser"]:::container

        subgraph "PantryPal Frontend (Static HTML/Tailwind/JS)"
            FE["Frontend Web Client"]:::container
            ENTRY["Landing/Entry Page (index.html)"]:::page

            subgraph "Views (Multi-page HTML)"
                L["Login Page UI"]:::page
                CP["Profile Creation/Update UI"]:::page
                H["Home Feed UI"]:::page
                CR["Recipe Creation UI"]:::page
                ER["Recipe Editing UI"]:::page
                PP["Profile Page UI"]:::page
                S["Search UI"]:::page
                LB["Leaderboard UI"]:::page
                AN["Analytics UI"]:::page
            end

            STATE["Client State Sync (likes/saves/follow cache)"]:::module
            JWTSTORE["JWT Stored Client-side (localStorage/sessionStorage)"]:::datastore
        end
    end

    subgraph "Backend"
        API["PantryPal API (Node.js + Express REST Server)"]:::container

        subgraph "Express App Composition"
            BOOT["Express App Bootstrap (server.js)"]:::module
            MW["Middleware (JSON parser, CORS, auth guard, error handling)"]:::module
            ENV["Environment/Configuration (.env)"]:::config
            NPM["Backend Dependencies/Scripts (npm)"]:::config
        end

        subgraph "Route Modules (REST)"
            AR["Auth Router (/api/auth)"]:::module
            RR["Recipes Router (/api/recipes)"]:::module
        end

        subgraph "Security (Cross-cutting)"
            JWT["JWT Auth (sign/verify Bearer token)"]:::security
            BCRYPT["bcrypt (hash/compare passwords)"]:::security
        end

        subgraph "Data Layer (Mongoose Models)"
            UM["User Model (Mongoose)"]:::model
            RM["Recipe Model (Mongoose)"]:::model
        end
    end

    subgraph "Data Store"
        MDB[("MongoDB (PantryPal Database)")]:::database
        UC[("users collection")]:::database
        RC[("recipes collection")]:::database
    end

    %% =======================
    %% Container Connections
    %% =======================
    U -->|"uses"| B
    B -->|"renders"| FE

    ENTRY -->|"navigates"| L
    ENTRY -->|"navigates"| H
    FE -->|"shared JS"| STATE

    FE -->|"REST/JSON over HTTP (dev: localhost:5000)"| API
    API -->|"JSON responses"| FE

    L -->|"POST /api/auth/login"| AR
    AR -->|"returns JWT"| FE
    FE -->|"stores token"| JWTSTORE
    FE -->|"Authorization: Bearer <JWT>"| API

    %% =======================
    %% Backend Wiring
    %% =======================
    API -->|"bootstraps"| BOOT
    BOOT -->|"loads config"| ENV
    BOOT -->|"uses"| MW
    BOOT -->|"mounts"| AR
    BOOT -->|"mounts"| RR
    BOOT -->|"npm scripts/run"| NPM

    AR -->|"uses for hashing"| BCRYPT
    AR -->|"issues/verifies"| JWT
    RR -->|"verifies"| JWT

    %% =======================
    %% Data Flows (Models/DB)
    %% =======================
    AR -->|"CRUD (users, profiles, follow)"| UM
    RR -->|"CRUD (recipes, feed, engagement)"| RM
    RR -->|"updates points/savedRecipes/author refs"| UM

    UM -->|"Mongoose queries"| MDB
    RM -->|"Mongoose queries"| MDB
    MDB -->|"stores"| UC
    MDB -->|"stores"| RC

    %% =======================
    %% Feature Flows (explicit)
    %% =======================
    CP -->|"PUT /api/auth/profile"| AR
    PP -->|"GET /api/auth/me"| AR
    PP -->|"POST/DELETE /api/auth/follow/:userId"| AR
    S -->|"GET /api/auth/users/search?q"| AR

    H -->|"GET /api/recipes/feed"| RR
    S -->|"GET /api/recipes/search?q"| RR
    CR -->|"POST /api/recipes (+10 points)"| RR
    ER -->|"PUT/DELETE /api/recipes/:id"| RR
    H -->|"POST /api/recipes/:id/like"| RR
    H -->|"POST /api/recipes/:id/save"| RR
    H -->|"POST /api/recipes/:id/comments"| RR

    LB -->|"GET (reads rankings/points)"| API
    AN -->|"GET (reads analytics aggregates)"| API

    %% =======================
    %% Click Events (from mapping)
    %% =======================
    click FE "https://github.com/kian-drego/pantry_pal/tree/main/Frontend/"
    click ENTRY "https://github.com/kian-drego/pantry_pal/blob/main/index.html"
    click L "https://github.com/kian-drego/pantry_pal/blob/main/Frontend/Loginpage.html"
    click CP "https://github.com/kian-drego/pantry_pal/blob/main/Frontend/create profile.html"
    click H "https://github.com/kian-drego/pantry_pal/blob/main/Frontend/homepage.html"
    click CR "https://github.com/kian-drego/pantry_pal/blob/main/Frontend/create.html"
    click ER "https://github.com/kian-drego/pantry_pal/blob/main/Frontend/edit-recipe.html"
    click PP "https://github.com/kian-drego/pantry_pal/blob/main/Frontend/profilepage.html"
    click S "https://github.com/kian-drego/pantry_pal/blob/main/Frontend/search.html"
    click LB "https://github.com/kian-drego/pantry_pal/blob/main/Frontend/leaderboard.html"
    click AN "https://github.com/kian-drego/pantry_pal/blob/main/Frontend/analytics.html"

    click API "https://github.com/kian-drego/pantry_pal/blob/main/Backend/server.js"
    click BOOT "https://github.com/kian-drego/pantry_pal/blob/main/Backend/server.js"
    click AR "https://github.com/kian-drego/pantry_pal/blob/main/Backend/routes/auth.js"
    click RR "https://github.com/kian-drego/pantry_pal/blob/main/Backend/routes/recipes.js"
    click UM "https://github.com/kian-drego/pantry_pal/blob/main/Backend/models/User.js"
    click RM "https://github.com/kian-drego/pantry_pal/blob/main/Backend/models/Recipe.js"
    click ENV "https://github.com/kian-drego/pantry_pal/blob/main/Backend/.env"
    click NPM "https://github.com/kian-drego/pantry_pal/blob/main/Backend/package.json"

    %% =======================
    %% Styles
    %% =======================
    classDef external fill:#F3F4F6,stroke:#6B7280,color:#111827,stroke-width:1px
    classDef container fill:#DBEAFE,stroke:#1D4ED8,color:#0F172A,stroke-width:1.5px
    classDef module fill:#DCFCE7,stroke:#16A34A,color:#052e16,stroke-width:1.25px
    classDef page fill:#E0F2FE,stroke:#0284C7,color:#0F172A,stroke-width:1.25px
    classDef database fill:#FEF3C7,stroke:#D97706,color:#0F172A,stroke-width:1.5px
    classDef datastore fill:#FEF3C7,stroke:#B45309,color:#0F172A,stroke-width:1.25px
    classDef model fill:#D1FAE5,stroke:#059669,color:#064e3b,stroke-width:1.25px
    classDef security fill:#FCE7F3,stroke:#BE185D,color:#0F172A,stroke-width:1.25px
    classDef config fill:#E5E7EB,stroke:#374151,color:#111827,stroke-width:1.25px
```
