# fahad-dezloper/crowdify architecture

> Architecture diagram of the GitHub repository fahad-dezloper/crowdify, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-03-31). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/fahad-dezloper/crowdify
- Repository: https://github.com/fahad-dezloper/crowdify

## Overview

##### Main Components
- Frontend: Represented by Next.js and React components within the “app” directory. Key UI components include pages (e.g., landing, room views) and widgets like Navbar, ChatBot, MusicPlayer, Queue, and various UI elements in the “components” folder.
- API/Backend Services: Multiple API routes are located in “app/api”. These handle authentication (using NextAuth/OAuth via [...nextauth]), room creation and management, song queue management (streams), user data retrieval, and YouTube search integration.
- Database Layer: The Prisma ORM connects to a PostgreSQL database. The “prisma” directory with its migrations and schema files is crucial for representing the data model and how persistent data (users, rooms, songs, streams) is managed.
- External Services: The system interacts with external systems such as:
- The YouTube API for streaming and search.
- An external backend (Crowdify-backend with Redis and WebSocket) which is used for real-time updates.
Also the Authentication flows via NextAuth.
- Supporting utilities & shared libraries: “app/lib” for database utilities and helper functions (e.g., roomDetails), as well as “context” for managing shared state in the frontend (WebContext for real-time interactions).

##### Relationships and Interactions
- API Routes → Database: API endpoints use Prisma (via “prisma/schema.prisma” and “lib/db.ts”) to interact with the PostgreSQL database. Show this connection clearly.
- API Routes → External Services: Some API routes (like the YouTube search endpoint) interact with external services such as the YouTube API. Also, note that real-time updates and chat functions are likely managed through the external Crowdify-backend leveraging Redis and WebSockets.
- Shared State/Context: The WebContext is used to manage front-end state, particularly for real-time updates and chat functionality. This should be depicted as an integral part of the client’s architecture.

##### Architectural Patterns and Design Principles
- Full-Stack Separation: Emphasize the clear separation between frontend (Next.js/React UI) and backend API endpoints. Each function (auth, room management, music queue) is modularized under “app/api”.
- Real-Time Communication: The inclusion of subscriptions via WebSocket (implied by the tech stack and endpoints like streams) highlights a real-time architecture.
- MVC / Component-Based Organization: The project demonstrates a component-based structure typical of modern React applications. Organize the UI components and the service layers accordingly.
- Use of Middlewares & APIs: Note the “middleware.ts” which may handle cross-cutting concerns like authentication or logging.

##### Relevant Technologies and Frameworks
- Next.js and React for the frontend.
- TailwindCSS for styling.
- Prisma ORM and PostgreSQL for the persistence layer.
- NextAuth / OAuth for authentication.
- Redis and WebSockets for real-time features (used by the separate but interfacing backend).
- The YouTube API for streaming integration.

##### Extra Details
- The presence of shared libraries (e.g., “app/lib” and “context”) should be drawn as supporting modules that multiple front-end components or API endpoints depend upon.
- Be sure to include the authentication pathway using NextAuth (indicating the reliance on OAuth providers) and how that integrates with secure access to rooms and functionalities.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Frontend Layer
    subgraph "Frontend (Next.js/React UI)"
        FE1["Pages & Layout (Next.js)"]:::frontend
        FE2["UI Elements (React Components)"]:::frontend
    end

    %% API/Backend Layer
    subgraph "API/Backend Services"
        API1["General API Routes"]:::api
        API2["Authentication (NextAuth/OAuth)"]:::api
        API3["Room Management"]:::api
        API4["Song Queue/Streams"]:::api
        API5["User Data Endpoint"]:::api
        API6["YouTube Search Integration"]:::api
        API7["Middleware Handler"]:::api
    end

    %% Database/Persistence Layer
    subgraph "Database (Prisma & PostgreSQL)"
        DB1["Prisma Schema"]:::database
        DB2["Prisma Migrations"]:::database
        DB3["Database Utility"]:::database
    end

    %% External Services
    subgraph "External Services"
        EX1["YouTube API"]:::external
        EX2["Crowdify Backend (Redis & WebSocket)"]:::external
    end

    %% Shared Utilities & Context
    subgraph "Shared Utilities & Context"
        SH1["Helper Functions"]:::shared
        SH2["Shared State/Context (WebContext)"]:::shared
    end

    %% Connections between Frontend and API/Backend
    FE1 -->|"HTTP_requests"| API1
    FE2 -->|"User_interactions"| API1

    %% Connections within API/Backend
    API1 -->|"auth"| API2
    API1 -->|"room_ops"| API3
    API1 -->|"stream_ops"| API4
    API1 -->|"user_fetch"| API5
    API1 -->|"youtube_query"| API6
    API1 -->|"middleware"| API7

    %% API endpoints interacting with the Database
    API2 -->|"db_auth"| DB3
    API3 -->|"CRUD_ops"| DB1
    API4 -->|"queue_updates"| DB1
    API5 -->|"data_fetch"| DB1

    %% API endpoints interacting with External Services
    API6 -->|"search_call"| EX1
    API3 -->|"realtime_notify"| EX2
    API4 -->|"realtime_notify"| EX2

    %% Frontend using Shared Context
    FE2 -->|"state_management"| SH2

    %% API using Helper Functions
    API3 -->|"utilizes"| SH1

    %% Class Definitions for Colors
    classDef frontend fill:#cce5ff,stroke:#333,stroke-width:2px;
    classDef api fill:#d4edda,stroke:#333,stroke-width:2px;
    classDef database fill:#fff3cd,stroke:#333,stroke-width:2px;
    classDef external fill:#f8d7da,stroke:#333,stroke-width:2px;
    classDef shared fill:#d1ecf1,stroke:#333,stroke-width:2px;

    %% Click Events for Frontend UI
    click FE1 "https://github.com/fahad-dezloper/crowdify/blob/main/app/page.tsx"
    click FE1 "https://github.com/fahad-dezloper/crowdify/blob/main/app/layout.tsx"
    click FE1 "https://github.com/fahad-dezloper/crowdify/blob/main/app/room/[roomId]/page.tsx"
    click FE1 "https://github.com/fahad-dezloper/crowdify/blob/main/app/room/page.tsx"
    click FE2 "https://github.com/fahad-dezloper/crowdify/blob/main/app/components/ChatBot.tsx"
    click FE2 "https://github.com/fahad-dezloper/crowdify/blob/main/app/components/LandingPage.tsx"
    click FE2 "https://github.com/fahad-dezloper/crowdify/blob/main/app/components/MusicPlayer.tsx"
    click FE2 "https://github.com/fahad-dezloper/crowdify/blob/main/app/components/Navbar.tsx"
    click FE2 "https://github.com/fahad-dezloper/crowdify/blob/main/app/components/Queue.tsx"

    %% Click Events for API/Backend Services
    click API1 "https://github.com/fahad-dezloper/crowdify/tree/main/app/api"
    click API2 "https://github.com/fahad-dezloper/crowdify/blob/main/app/api/auth/[...nextauth]/route.ts"
    click API3 "https://github.com/fahad-dezloper/crowdify/blob/main/app/api/room/[roomId]/route.ts"
    click API3 "https://github.com/fahad-dezloper/crowdify/blob/main/app/api/room/create/route.ts"
    click API4 "https://github.com/fahad-dezloper/crowdify/blob/main/app/api/streams/route.ts"
    click API5 "https://github.com/fahad-dezloper/crowdify/blob/main/app/api/user/fetchUser/route.ts"
    click API6 "https://github.com/fahad-dezloper/crowdify/blob/main/app/api/youtube-search/route.ts"
    click API7 "https://github.com/fahad-dezloper/crowdify/blob/main/middleware.ts"

    %% Click Events for Database/Persistence Layer
    click DB1 "https://github.com/fahad-dezloper/crowdify/blob/main/prisma/schema.prisma"
    click DB2 "https://github.com/fahad-dezloper/crowdify/tree/main/prisma/migrations"
    click DB3 "https://github.com/fahad-dezloper/crowdify/blob/main/app/lib/db.ts"

    %% Click Events for Shared Utilities & Context
    click SH1 "https://github.com/fahad-dezloper/crowdify/blob/main/app/lib/fns/roomDetails.ts"
    click SH2 "https://github.com/fahad-dezloper/crowdify/blob/main/app/context/WebContext.tsx"
```
