# hiteshchoudhary/image-ecommerce architecture

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

- Interactive diagram: https://gitdiagram.com/hiteshchoudhary/image-ecommerce
- Repository: https://github.com/hiteshchoudhary/image-ecommerce

## Overview

##### Project’s Purpose and Type
- This is a full-stack image selling application built with Next.js and React.
- It integrates with external services such as ImageKit (for image uploads), Razorpay (for payment processing), and NextAuth (for user authentication).
- It supports CRUD operations for products, user orders, and includes a component-based frontend alongside API endpoints.

##### File Structure
- The top-level directories and files set up configuration (e.g., .env, next.config.ts, tailwind.config.ts), and source code.
- The "app" directory contains the core Next.js pages and API endpoints. It is organized into:
- Pages for standard UI routes (e.g., “/login”, “/register”, “/products”, “/orders”, “/admin”).
- Dedicated API folders for authentication (/api/auth), image handling (/api/imagekit-auth), orders (/api/orders), products (/api/products), and Razorpay webhook (/api/webhook/razorpay).
- The "lib" directory includes utility functions and modules for API clients, authentication logic, and database access.
- The "models" directory defines the data models (User, Product, Order) that connect with the business logic.
- Additional directories such as “components” (for UI elements), “public” (static resources), and "scripts" (for database seeding and mail testing) further clarify separation of concerns.

##### Key Components
a. Frontend (Client Side)
- Next.js UI pages in "app" (e.g., homepage, login, register, products detail page, orders, admin).
- Reusable React components (e.g., Header, ProductCard, Notification, FileUpload, ImageGallery).
- Global styling with Tailwind CSS.
- Layout and Providers used to maintain state and handle session data via NextAuth.
b. Backend API Layer
- API endpoints handling user authentication, registration, product CRUD operations, order processing, image uploads, and payment webhook processing.
- These endpoints in the "app/api" folder handle requests and business logic.
c. External Service Integrations
- NextAuth for authentication (configured under /api/auth).
- ImageKit for image hosting and manipulation (/api/imagekit-auth).
- Razorpay to process payments and handle webhook callbacks (/api/webhook/razorpay).
d. Data and Persistence
- Database operations managed through utility functions in "lib/db" and defined models in "models".
- The models (User, Product, Order) capture the essential business entities.

##### Relationships and Interactions
- Frontend interacts with the backend API endpoints for data operations (authentication, product display/management, order processing).
- API endpoints communicate with the database (using models and utility functions in "lib/db") to perform CRUD operations.
- API endpoints also connect to external services:
- NextAuth for secure authentication flows.
- ImageKit for handling image uploads.
- Razorpay for payment processing and via webhook for asynchronous event handling.
- The routing structure indicates clear separation between the API layer and the component-based UI layer managed by Next.js.

##### Architectural Patterns and Design Principles
- Separation of concerns: UI, API, and integration layers are kept distinct.
- Modularization: Components are reusable and the API routes follow Next.js’s file-based routing.
- Security and scalability: Using middleware, environment configurations (.env files) and clear separation of public vs. private elements.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Frontend Subgraph
    subgraph "Frontend"
        FE_AGG("Next.js Frontend"):::frontend
        subgraph "UI Pages"
            FE_Home("Home Page"):::frontend
            FE_Login("Login Page"):::frontend
            FE_Register("Register Page"):::frontend
            FE_Product("Product Detail Page"):::frontend
            FE_Orders("Orders Page"):::frontend
            FE_Admin("Admin Page"):::frontend
        end
        FE_Comp("Reusable Components"):::frontend
    end

    %% Aggregated Backend Gateway Node
    APIG("API Gateway (Backend Endpoints)"):::api

    %% Backend API Endpoints Subgraph
    subgraph "Backend API Endpoints"
        API_Auth("Auth API (NextAuth)"):::api
        API_Image("Image Upload API (ImageKit)"):::api
        API_Orders("Orders API"):::api
        API_Products("Products API"):::api
        API_Payment("Payment Webhook API (Razorpay)"):::api
    end

    %% Data & Persistence Subgraph
    subgraph "Data & Persistence"
        DB_Models("Data Models"):::data
        DB_Util("Database Utility"):::data
    end

    %% Utility Libraries Subgraph
    subgraph "Utility Libraries"
        API_Client("API Client Library"):::data
        Auth_Lib("Auth Library"):::data
    end

    %% External Services Subgraph
    subgraph "External Services"
        EXT_NextAuth("NextAuth Service"):::external
        EXT_ImageKit("ImageKit Service"):::external
        EXT_Razorpay("Razorpay Service"):::external
    end

    %% Connections from Frontend to API Gateway
    FE_AGG --> APIG
    FE_Home --> FE_AGG
    FE_Login --> FE_AGG
    FE_Register --> FE_AGG
    FE_Product --> FE_AGG
    FE_Orders --> FE_AGG
    FE_Admin --> FE_AGG
    FE_Comp --> FE_AGG

    %% Connections from API Gateway to individual API endpoints
    APIG --> API_Auth
    APIG --> API_Image
    APIG --> API_Orders
    APIG --> API_Products
    APIG --> API_Payment

    %% Connections from API Gateway to Data & Persistence and Utility Libraries
    APIG --> DB_Models
    APIG --> API_Client

    %% External service integrations from specific API endpoints
    API_Auth --> EXT_NextAuth
    API_Image --> EXT_ImageKit
    API_Payment --> EXT_Razorpay

    %% Click Events for Frontend UI Pages
    click FE_Home "https://github.com/hiteshchoudhary/image-ecommerce/blob/main/app/page.tsx"
    click FE_Login "https://github.com/hiteshchoudhary/image-ecommerce/blob/main/app/login/page.tsx"
    click FE_Register "https://github.com/hiteshchoudhary/image-ecommerce/blob/main/app/register/page.tsx"
    click FE_Product "https://github.com/hiteshchoudhary/image-ecommerce/blob/main/app/products/[id]/page.tsx"
    click FE_Orders "https://github.com/hiteshchoudhary/image-ecommerce/blob/main/app/orders/page.tsx"
    click FE_Admin "https://github.com/hiteshchoudhary/image-ecommerce/blob/main/app/admin/page.tsx"
    click FE_Comp "https://github.com/hiteshchoudhary/image-ecommerce/tree/main/app/components"

    %% Click Events for Backend API Endpoints
    click API_Auth "https://github.com/hiteshchoudhary/image-ecommerce/tree/main/app/api/auth"
    click API_Image "https://github.com/hiteshchoudhary/image-ecommerce/blob/main/app/api/imagekit-auth/route.ts"
    click API_Orders "https://github.com/hiteshchoudhary/image-ecommerce/tree/main/app/api/orders"
    click API_Products "https://github.com/hiteshchoudhary/image-ecommerce/tree/main/app/api/products"
    click API_Payment "https://github.com/hiteshchoudhary/image-ecommerce/blob/main/app/api/webhook/razorpay/route.ts"

    %% Click Events for Data & Persistence and Utility Libraries
    click DB_Models "https://github.com/hiteshchoudhary/image-ecommerce/tree/main/models"
    click DB_Util "https://github.com/hiteshchoudhary/image-ecommerce/blob/main/lib/db.ts"
    click API_Client "https://github.com/hiteshchoudhary/image-ecommerce/blob/main/lib/api-client.ts"
    click Auth_Lib "https://github.com/hiteshchoudhary/image-ecommerce/blob/main/lib/auth.ts"

    %% Styling
    classDef frontend fill:#cce5ff,stroke:#004085,stroke-width:2px;
    classDef api fill:#d4edda,stroke:#155724,stroke-width:2px;
    classDef data fill:#fff3cd,stroke:#856404,stroke-width:2px;
    classDef external fill:#fde2e4,stroke:#d6336c,stroke-width:2px;
```
