# tuhin114/prompt-sharing-application architecture

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

- Interactive diagram: https://gitdiagram.com/tuhin114/prompt-sharing-application
- Repository: https://github.com/tuhin114/prompt-sharing-application

## Overview

##### Main Components
- Frontend UI Layer: Represented by the Next.js “app” folder with pages such as “create-prompt”, “dashboard”, “profile”, “leaderboard”, “prompt”, etc. Also include reusable components and UI elements from the “components” and “src/components/ui” directories.
- API Routes/Backend Logic: This is defined in the “app/api” folder. It contains endpoints for authentication (e.g., /auth/[...nextauth]), prompts (/api/prompt and its variants for new, draft, sort, etc.), and various functionalities (bookmarks, categories, notifications, scheduling, etc.).
- Database Layer: A MongoDB backend that is integrated via Mongoose. This includes connection management (handled in utils/database.js and further configured via a “connectToDB” function) and models (found in the “models” directory for User, Prompt, Category, Notification, etc.).
- Authentication Module: Managed via NextAuth and GoogleProvider (with detailed custom callbacks for session and sign-in), ensuring that each session includes the user’s MongoDB ID and that new users are automatically created if they don’t exist.
- External Integrations and CI/CD: Indicate integration with external services (Google for authentication and MongoDB as the primary data store) and also note automated workflows (GitHub Workflows found in .github/workflows) such as the cron job setup.

##### Relationships and Data Flow
- The Frontend communicates with the Backend API routes using HTTP requests (GET, POST, etc.). For instance, forms (e.g., in CreatePrompt and EditPrompt components) submit data to endpoints like /api/prompt/new.
- The API layer receives data and interacts with the Database. For example, the POST /api/prompt route extracts input data, connects to MongoDB via the “connectToDB” function, creates a new prompt document using a corresponding Mongoose model, and returns a response.
- The Authentication module intercepts sign-in and session checks. It manages user creation, session enrichment (adding MongoDB _id), and redirects based on authentication status.
- Custom Hooks (in the “hooks” directory) are used across the UI to fetch data, handle search/filter operations, manage states like “submitting” status, and interact with API endpoints.

##### Architectural Patterns and Design Principles
- Separation of Concerns: The system is divided into distinct layers – UI components/pages (views), API routes (controllers/business logic), and database models (data layer).
- RESTful API Design: The various API endpoints follow HTTP methods (GET, POST) to perform specific operations.
- Component Reusability: There is a clear separation between UI components (e.g., PromptCard, Feed, Form) for a consistent user interface.
- Micro-interactions: Custom hooks and modular components allow for a smooth user experience and state management.
- Authentication and Authorization: NextAuth provides robust user authentication while integrating database validations using Mongoose schemas.
- CI/CD & Cron Jobs: GitHub workflows indicate automated deployment/build and scheduled tasks.

##### Specific Elements
– Component labels such as “Frontend UI”, “API Routes”, “Database (MongoDB)”, “Authentication Module (NextAuth + Google)”, “Models (User, Prompt, etc.)”, and “Custom Hooks/Utilities”.
- User interaction on UI → API Request (e.g. creating a prompt, signing in).
- API Route → Database Connection (e.g., via connectToDB and Mongoose models).
- Authentication flow → Session callback during sign-in.
- Data flow from API → Frontend (responses feeding into components like Feed, PromptCard).
– Include annotations on key architectural patterns (e.g., “Separation of Lookup and CRUD operations in API”, “Reusable UI Components”, “Authentication Middleware via NextAuth”).

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Frontend UI Layer
    subgraph "Frontend UI Layer"
        A1["Create-Prompt Page"]:::frontend
        A2["Dashboard Page"]:::frontend
        A3["Profile Page"]:::frontend
        A4["Leaderboard Page"]:::frontend
        A5["Prompt Page"]:::frontend
        A6["Update-Prompt Page"]:::frontend
        A7["Components (Reusable)"]:::frontend
        A8["UI Components"]:::frontend
        A9["Custom Hooks (hooks)"]:::hooks
            subgraph "Hooks"
                H1["useBookmark"]:::hooks
                H2["useCategories"]:::hooks
                H3["useFollow"]:::hooks
            end
    end

    %% Backend API Layer
    subgraph "Backend API Layer"
        B1["API Routes"]:::backend
        B2["Authentication Module (NextAuth+Google)"]:::auth
    end

    %% Database Layer
    subgraph "Database Layer"
        C1["DB Connection"]:::db
        C2["User Model"]:::db
        C3["Prompt Model"]:::db
        C4["Category Model"]:::db
        C5["Notification Model"]:::db
        C6["SchedulePost Model"]:::db
    end

    %% External and CI/CD
    subgraph "External & CI/CD"
        D1["GitHub Workflows"]:::cicd
    end

    %% Relationships and Data Flows
    A1 -->|"HTTP_request"| B1
    A2 -->|"HTTP_request"| B1
    A3 -->|"HTTP_request"| B1
    A4 -->|"HTTP_request"| B1
    A5 -->|"HTTP_request"| B1
    A6 -->|"HTTP_request"| B1
    A9 -->|"API_interaction"| B1
    A2 -->|"Sign-in"| B2

    B1 -->|"DB_connection"| C1
    C1 -->|"uses_Model"| C2
    C1 -->|"uses_Model"| C3
    C1 -->|"uses_Model"| C4
    C1 -->|"uses_Model"| C5
    C1 -->|"uses_Model"| C6

    B1 -->|"Auth_request"| B2
    B2 -->|"validates_with"| C2

    B1 -- "HTTP_response" --> A5

    D1 -.->|"deploy/cron"| B1

    %% Click events for Frontend UI Layer
    click A1 "https://github.com/tuhin114/prompt-sharing-application/blob/main/app/create-prompt/page.jsx"
    click A2 "https://github.com/tuhin114/prompt-sharing-application/blob/main/app/dashboard/page.jsx"
    click A3 "https://github.com/tuhin114/prompt-sharing-application/blob/main/app/profile/page.jsx"
    click A4 "https://github.com/tuhin114/prompt-sharing-application/blob/main/app/leaderboard/page.jsx"
    click A5 "https://github.com/tuhin114/prompt-sharing-application/blob/main/app/prompt/page.jsx"
    click A6 "https://github.com/tuhin114/prompt-sharing-application/blob/main/app/update-prompt/page.jsx"
    click A7 "https://github.com/tuhin114/prompt-sharing-application/tree/main/components"
    click A8 "https://github.com/tuhin114/prompt-sharing-application/tree/main/src/components/ui"

    %% Click events for Backend API Layer and Authentication Module
    click B1 "https://github.com/tuhin114/prompt-sharing-application/tree/main/app/api"
    click B2 "https://github.com/tuhin114/prompt-sharing-application/blob/main/app/api/auth/[...nextauth]/route.js"

    %% Click events for Database Layer
    click C1 "https://github.com/tuhin114/prompt-sharing-application/blob/main/utils/database.js"
    click C2 "https://github.com/tuhin114/prompt-sharing-application/blob/main/models/user.js"
    click C3 "https://github.com/tuhin114/prompt-sharing-application/blob/main/models/prompt.js"
    click C4 "https://github.com/tuhin114/prompt-sharing-application/blob/main/models/category.js"
    click C5 "https://github.com/tuhin114/prompt-sharing-application/blob/main/models/notification.js"
    click C6 "https://github.com/tuhin114/prompt-sharing-application/blob/main/models/schedulePost.js"

    %% Click events for Custom Hooks
    click H1 "https://github.com/tuhin114/prompt-sharing-application/blob/main/hooks/useBookmark.js"
    click H2 "https://github.com/tuhin114/prompt-sharing-application/blob/main/hooks/useCategories.js"
    click H3 "https://github.com/tuhin114/prompt-sharing-application/blob/main/hooks/useFollow.js"

    %% Click event for External & CI/CD
    click D1 "https://github.com/tuhin114/prompt-sharing-application/blob/main/.github/workflows/cron-job.yml"

    %% Styles
    classDef frontend fill:#AED6F1,stroke:#1B4F72,stroke-width:2px;
    classDef backend fill:#F9E79F,stroke:#B9770E,stroke-width:2px;
    classDef db fill:#D5F5E3,stroke:#196F3D,stroke-width:2px;
    classDef auth fill:#FADBD8,stroke:#922B21,stroke-width:2px;
    classDef hooks fill:#E8DAEF,stroke:#6C3483,stroke-width:2px;
    classDef cicd fill:#FCF3CF,stroke:#B7950B,stroke-width:2px;
```
