# itzminos/ceng-495-hw1 architecture

> Architecture diagram of the GitHub repository itzminos/ceng-495-hw1, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-04-25). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/itzminos/ceng-495-hw1
- Repository: https://github.com/itzminos/ceng-495-hw1

## Overview

##### Project Type and Purpose
- This is a full-stack e-commerce web application built with Next.js (React) for the frontend and backend (serverless API routes), MongoDB (via Mongoose) for data persistence, and Vercel for deployment.
- It supports browsing items, user authentication/authorization (regular vs. admin), CRUD operations on items/users (admin), and rating/review functionality.

##### File Structure Analysis
- frontend/UI layer:
- components/ (Footer.js, ItemCard.js, Navbar.js)
- pages/ (Next.js pages: index.js, items/[id].js, cart.js, login.js, signup.js, user.js, admin.js, cancel.js, success.js)
- context/ (cardContext.js for client‐side state such as cart contents)
- public/ (static assets)
- backend/API layer (Next.js serverless functions):
- pages/api/ (auth.js, checkout.js, items.js, items/[id].js, items/[id]/reviews.js, user.js, users.js)
- data layer:
- lib/dbConnect.js (DB connection helper)
- models/ (Mongoose schemas: Item.js, Review.js, User.js)
- config & deployment:
- package.json (dependencies like next, react, mongoose, bcrypt, stripe?)
- postcss.config.mjs
- .gitignore, .vscode settings

##### Key Architectural Patterns
- MVC-style separation:
- Models: Mongoose schemas in models/
- Controllers: Next.js API routes in pages/api/ act as controllers handling requests/ responses
- Views: React components in pages/ and components/
- Serverless Functions: Each file under pages/api/ becomes an isolated serverless endpoint on Vercel.
- Client-side state management: React Context (cardContext.js) for shopping cart.
- Authentication/Authorization: JWT or cookie-based sessions via pages/api/auth.js guards protected pages (user.js, admin.js).
- Database cleanup logic: Cascade deletion logic in API controller or Mongoose pre/post hooks to remove related reviews.

##### Main Components & Interactions
- Browser (User Agent)
- Interacts with Next.js pages (SSR/SSG + client-side React)
- Calls API endpoints for data ops (login, fetch items, add review, checkout)
- Next.js Frontend
- Renders UI, holds cart state, invokes API routes via fetch/axios
- Next.js Serverless API
- pages/api/auth.js: login/signup, issue tokens or sessions
- pages/api/items*.js: CRUD on items, fetch item list/details
- pages/api/items/[id]/reviews.js: post/read reviews
- pages/api/checkout.js: payment integration (e.g., Stripe)
- pages/api/users.js & pages/api/user.js: user management, admin functions
- MongoDB (Atlas)
- Collections: users, items, reviews
- dbConnect.js manages connection pooling
- External Services
- Vercel for hosting and auto-deploy
- (Optional) Stripe or other payment gateway via checkout.js

##### Essential Elements to Capture
- Clear labels on each component
- UI
- Business Logic/API
- Database
- External Services
- Annotations for authentication/authorization boundaries

## Diagram (Mermaid)

```mermaid
flowchart TB
    %% Layers and Components

    subgraph "User Layer"
        Browser(["Browser (User Agent)"]):::ext
    end

    subgraph "Next.js Frontend (React UI)" 
        direction TB
        HomePage["Home Page"]:::ui
        ItemPage["Item Details Page"]:::ui
        CartPage["Cart Page"]:::ui
        LoginPage["Login Page"]:::ui
        SignupPage["Signup Page"]:::ui
        UserPage["User Profile Page"]:::ui
        AdminPage["Admin Dashboard Page"]:::ui
        SuccessPage["Checkout Success Page"]:::ui
        CancelPage["Checkout Cancel Page"]:::ui
        Footer["Footer Component"]:::ui
        ItemCard["ItemCard Component"]:::ui
        Navbar["Navbar Component"]:::ui
        CardContext["Cart Context"]:::ui
    end

    subgraph "Serverless API (Next.js)" 
        direction TB
        AuthAPI["Auth Endpoint"]:::api
        ItemsAPI["Items Collection Endpoint"]:::api
        ItemAPI["Single Item Endpoint"]:::api
        ReviewsAPI["Item Reviews Endpoint"]:::api
        CheckoutAPI["Checkout/Payment Endpoint"]:::api
        UsersAPI["Users Collection Endpoint"]:::api
        UserAPI["Single User Endpoint"]:::api
    end

    subgraph "Data Layer"
        direction TB
        DB[("MongoDB\n(cylinder)")]:::db
        DBConnect["DB Connection Helper"]:::db
        ItemModel["Item Model"]:::db
        ReviewModel["Review Model"]:::db
        UserModel["User Model"]:::db
    end

    subgraph "External Services"
        direction TB
        Stripe(["Stripe\n(Payment Gateway)"]):::ext
        Vercel(["Vercel\n(Deployment)"]):::ext
    end

    %% Connections

    Browser -->|"Navigates to pages"| HomePage
    Browser --> ItemPage
    Browser --> CartPage
    Browser --> LoginPage
    Browser --> SignupPage
    Browser --> UserPage
    Browser --> AdminPage
    Browser --> SuccessPage
    Browser --> CancelPage

    HomePage -->|"renders with components"| Navbar
    HomePage --> Footer
    ItemPage --> ItemCard
    CartPage --> CardContext

    %% Frontend to API
    HomePage -->|"GET /api/items"| ItemsAPI
    ItemPage -->|"GET /api/items/:id"| ItemAPI
    ItemPage -->|"GET /api/items/:id/reviews"| ReviewsAPI
    LoginPage -->|"POST /api/auth"| AuthAPI
    SignupPage -->|"POST /api/auth"| AuthAPI
    UserPage -->|"GET/PUT /api/user"| UserAPI
    AdminPage -->|"GET/POST/DELETE /api/users"| UsersAPI
    CartPage -->|"POST /api/checkout"| CheckoutAPI
    SuccessPage --> CheckoutAPI
    CancelPage --> CheckoutAPI

    %% API to DB
    AuthAPI -->|"Connect/Mongoose"| DBConnect
    ItemsAPI --> DBConnect
    ItemAPI --> DBConnect
    ReviewsAPI --> DBConnect
    UsersAPI --> DBConnect
    UserAPI --> DBConnect

    DBConnect --> DB
    DB --> ItemModel
    DB --> ReviewModel
    DB --> UserModel

    %% Checkout flow
    CheckoutAPI -->|"Create charge"| Stripe
    Stripe -->|"Webhook/Response"| CheckoutAPI

    %% Deployment
    Vercel --> Browser
    Vercel --> HomePage
    Vercel --> AuthAPI
    Vercel --> DBConnect

    %% Click Events

    click Footer "https://github.com/itzminos/ceng-495-hw1/blob/main/components/Footer.js"
    click ItemCard "https://github.com/itzminos/ceng-495-hw1/blob/main/components/ItemCard.js"
    click Navbar "https://github.com/itzminos/ceng-495-hw1/blob/main/components/Navbar.js"
    click HomePage "https://github.com/itzminos/ceng-495-hw1/blob/main/pages/index.js"
    click ItemPage "https://github.com/itzminos/ceng-495-hw1/blob/main/pages/items/[id].js"
    click CartPage "https://github.com/itzminos/ceng-495-hw1/blob/main/pages/cart.js"
    click LoginPage "https://github.com/itzminos/ceng-495-hw1/blob/main/pages/login.js"
    click SignupPage "https://github.com/itzminos/ceng-495-hw1/blob/main/pages/signup.js"
    click UserPage "https://github.com/itzminos/ceng-495-hw1/blob/main/pages/user.js"
    click AdminPage "https://github.com/itzminos/ceng-495-hw1/blob/main/pages/admin.js"
    click SuccessPage "https://github.com/itzminos/ceng-495-hw1/blob/main/pages/success.js"
    click CancelPage "https://github.com/itzminos/ceng-495-hw1/blob/main/pages/cancel.js"
    click CardContext "https://github.com/itzminos/ceng-495-hw1/blob/main/context/cardContext.js"
    click AuthAPI "https://github.com/itzminos/ceng-495-hw1/blob/main/pages/api/auth.js"
    click CheckoutAPI "https://github.com/itzminos/ceng-495-hw1/blob/main/pages/api/checkout.js"
    click ItemsAPI "https://github.com/itzminos/ceng-495-hw1/blob/main/pages/api/items.js"
    click ItemAPI "https://github.com/itzminos/ceng-495-hw1/blob/main/pages/api/items/[id].js"
    click ReviewsAPI "https://github.com/itzminos/ceng-495-hw1/blob/main/pages/api/items/[id]/reviews.js"
    click UsersAPI "https://github.com/itzminos/ceng-495-hw1/blob/main/pages/api/users.js"
    click UserAPI "https://github.com/itzminos/ceng-495-hw1/blob/main/pages/api/user.js"
    click DBConnect "https://github.com/itzminos/ceng-495-hw1/blob/main/lib/dbConnect.js"
    click ItemModel "https://github.com/itzminos/ceng-495-hw1/blob/main/models/Item.js"
    click ReviewModel "https://github.com/itzminos/ceng-495-hw1/blob/main/models/Review.js"
    click UserModel "https://github.com/itzminos/ceng-495-hw1/blob/main/models/User.js"
    click Vercel "https://github.com/itzminos/ceng-495-hw1/blob/main/.vscode"

    %% Styles
    classDef ui fill:#cce5ff,stroke:#004085,color:#004085
    classDef api fill:#d4edda,stroke:#155724,color:#155724
    classDef db fill:#fff3cd,stroke:#856404,color:#856404
    classDef ext fill:#e2e3e5,stroke:#6c757d,color:#6c757d
```
