# roopsagar-k/questionpaperhub architecture

> Architecture diagram of the GitHub repository roopsagar-k/questionpaperhub, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-08-12). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/roopsagar-k/questionpaperhub
- Repository: https://github.com/roopsagar-k/questionpaperhub

## Overview

##### Project Type and Purpose
- This is a **full-stack Next.js application** (app directory uses the Next.js App Router), written in TypeScript.
- Purpose: allow users to upload or create question papers, extract text via an LLM, publish interactive posts, and take tests.

##### Main Components
a. **Client / Frontend**
- Next.js React pages & Server Components under `app/` (e.g. `/menu`, `/auth`, `/create-paper`, `/tests`)
- UI library: ShadCN + Tailwind (components/ui)
- Context providers (`UserContext`, `TestInfoContext`)
b. **API / Backend Layer**
- Next.js API Routes under `app/api/…` grouped by resource: `auth`, `register`, `posts`, `comments`, `votes`, `bookmarks`, `tests`, `tests-taken`, `upload`, `admin`, `search`
- Each route implemented as a serverless function (e.g. `route.ts`)
c. **Database**
- PostgreSQL, accessed via Drizzle ORM (`lib/drizzle/schema.ts`, `lib/drizzle/db.ts`, migrations)
d. **External Services**
- Authentication: Auth.js (NextAuth)
- Cloudinary (image upload) via `lib/cloudinary.ts`
- LLM/Text extraction service (e.g. OpenAI) via `lib/signin.ts` or other utils
- PDF processing utilities (`lib/pdfToImage.ts`)
e. **Static Assets & Config**
- Public folder (animations, images)
- Config files: `next.config.mjs`, `tailwind.config.ts`, `.env.example`

##### Relationships & Data Flows
- **User → Frontend**: HTTP requests to Next.js pages (SSR/SSR) and client‐side interactions.
- **Frontend → API**: RESTful calls to `/api/...` endpoints for CRUD on posts, comments, tests, votes, bookmarks, file uploads.
- **API → Database**: Drizzle ORM executes SQL queries on PostgreSQL.
- **API → External Services**:
- Auth.js for session management
- Cloudinary for image uploads
- LLM endpoints for text extraction
- PDF utilities for local conversion
- **Database → Frontend**: via API, data serialized to JSON and rendered.

##### Architectural Patterns & Design Principles
- **Monolithic serverless**: All backend code lives in one Next.js project but is broken into discrete serverless routes.
- **Componentized UI**: small, reusable UI primitives (ShadCN) and feature components.
- **Context-driven state**: global contexts for user/session and test info.
- **ORM layer abstraction**: Drizzle provides type-safe DB access.
- **Layered separation**:
- Presentation (React/UI)
- Application/API (Next.js API routes)
- Persistence (Drizzle + Postgres)
- Infrastructure & services (Cloudinary, LLM, Auth)

a. Sketch the **User Interface** on the left:
- “Landing Page”, “Auth Pages”, “Home/Dashboard”, “Test Pages”, “Create Paper”
- Inside each, note key `route.ts` handlers
- “PostgreSQL via Drizzle ORM”
- Core tables: Users, Posts, Comments, Tests, TestAttempts, Bookmarks, Votes
d. On the far right, list **External Services**:
- Auth.js (OAuth / Session Store)
- Cloudinary (Image Storage)
- LLM API (Text Extraction)
- PDF Conversion Utility
- Frontend Auth pages → API/auth → Auth.js → Database
- Post creation UI → API/posts → Database, LLM
- File upload UI → API/upload → Cloudinary → Database
- Test-taking UI → API/tests-taken → Database

## Diagram (Mermaid)

```mermaid
flowchart TB
  subgraph "Users & User Agents"
    USER["User\n(Browser/Mobile)"]
  end

  subgraph "Frontend Layer"
    direction TB
    layout["layout.tsx"]:::frontend
    pageRoot["page.tsx"]:::frontend
    loginPage["login page"]:::frontend
    registerPage["register page"]:::frontend
    homePage["home page"]:::frontend
    dashboardPage["dashboard page"]:::frontend
    createPage["create-paper/[id]"]:::frontend
    testPage["tests/[id]"]:::frontend

    componentsui["components/ui"]:::frontend
    button["button.tsx"]:::frontend
    input["input.tsx"]:::frontend
    card["card.tsx"]:::frontend
    dialog["dialog.tsx"]:::frontend

    sharedPost["Post.tsx"]:::frontend
    sharedQuestion["Question.tsx"]:::frontend
    sharedQuestions["Questions.tsx"]:::frontend
    sharedTimer["Timer.tsx"]:::frontend
    authLoginComp["Login.tsx"]:::frontend
    authRegisterComp["Register.tsx"]:::frontend
    commentSection["CommentSection.tsx"]:::frontend

    userContext["UserContext.tsx"]:::frontend
    testInfoContext["TestInfoContext.tsx"]:::frontend
  end

  subgraph "Backend Layer"
    direction TB
    authAPI["auth/[...nextauth]/route.ts"]:::backend
    registerAPI["register/route.ts"]:::backend
    postsAPI["posts/route.ts"]:::backend
    postByIdAPI["posts/[id]/route.ts"]:::backend
    commentsAPI["comments/route.ts"]:::backend
    commentByIdAPI["comments/[id]/route.ts"]:::backend
    votesAPI["votes/[id]/route.ts"]:::backend
    bookmarksAPI["bookmarks/route.ts"]:::backend
    testsAPI["tests/route.ts"]:::backend
    testByIdAPI["tests/[id]/route.ts"]:::backend
    addQuestionsAPI["tests/add-questions/route.ts"]:::backend
    testsFileAPI["tests/file/route.ts"]:::backend
    testsImagesAPI["tests/images/route.ts"]:::backend
    testsTakenAPI["tests-taken/route.ts"]:::backend
    testsTakenByIdAPI["tests-taken/[testId]/route.ts"]:::backend
    uploadAPI["upload/route.ts"]:::backend
    searchAPI["search/route.ts"]:::backend
    adminAPI["admin/route.ts"]:::backend

    libCloudinary["cloudinary.ts"]:::backend
    libPDF["pdfToImage.ts"]:::backend
    libSignin["signin.ts"]:::backend
    libUploadImage["uploadImage.ts"]:::backend
    libUtils["utils.ts"]:::backend

    drizzleDB["db.ts"]:::backend
    drizzleSchema["schema.ts"]:::backend
    drizzleMigration["0000_next_zodiak.sql"]:::backend
    drizzleMigrate["migrate.ts"]:::backend
  end

  subgraph "Data & Services Layer"
    direction TB
    Postgres["PostgreSQL\n(via Drizzle)"]:::db
    AuthJS["Auth.js\n(NextAuth)"]:::external
    CloudinaryService["Cloudinary API"]:::external
    LLMService["LLM API\n(OpenAI)"]:::external
    PDFService["PDF Conversion\nUtility"]:::external
  end

  USER --> NextFrontend["Next.js Frontend"]:::frontend
  NextFrontend --> layout
  NextFrontend --> pageRoot
  NextFrontend --> loginPage
  NextFrontend --> registerPage
  NextFrontend --> homePage
  NextFrontend --> dashboardPage
  NextFrontend --> createPage
  NextFrontend --> testPage
  NextFrontend --> componentsui
  componentsui --> button
  componentsui --> input
  componentsui --> card
  componentsui --> dialog
  NextFrontend --> sharedPost
  NextFrontend --> sharedQuestion
  NextFrontend --> sharedQuestions
  NextFrontend --> sharedTimer
  NextFrontend --> authLoginComp
  NextFrontend --> authRegisterComp
  NextFrontend --> commentSection
  NextFrontend --> userContext
  NextFrontend --> testInfoContext

  NextFrontend --> authAPI
  NextFrontend --> registerAPI
  NextFrontend --> postsAPI
  NextFrontend --> commentsAPI
  NextFrontend --> votesAPI
  NextFrontend --> bookmarksAPI
  NextFrontend --> testsAPI
  NextFrontend --> testsTakenAPI
  NextFrontend --> uploadAPI
  NextFrontend --> searchAPI
  NextFrontend --> adminAPI

  authAPI --> AuthJS
  registerAPI --> drizzleDB
  postsAPI --> drizzleDB
  postByIdAPI --> drizzleDB
  commentsAPI --> drizzleDB
  commentByIdAPI --> drizzleDB
  votesAPI --> drizzleDB
  bookmarksAPI --> drizzleDB
  testsAPI --> drizzleDB
  testByIdAPI --> drizzleDB
  addQuestionsAPI --> drizzleDB
  testsFileAPI --> drizzleDB
  testsImagesAPI --> drizzleDB
  testsTakenAPI --> drizzleDB
  testsTakenByIdAPI --> drizzleDB
  uploadAPI --> CloudinaryService
  searchAPI --> drizzleDB
  adminAPI --> drizzleDB

  authAPI --> libSignin
  uploadAPI --> libCloudinary
  testsFileAPI --- libPDF
  postsAPI --- libSignin

  drizzleDB --> drizzleSchema
  drizzleDB --> drizzleMigration
  drizzleDB --> drizzleMigrate

  CloudinaryService --> Postgres
  AuthJS --> Postgres
  LLMService --> drizzleDB

  classDef frontend fill:#D0E8FF,stroke:#0366D6,color:#0366D6;
  classDef backend fill:#E6F9D5,stroke:#2E8B57,color:#2E8B57;
  classDef db fill:#FFE7BA,stroke:#CD7F32,color:#CD7F32;
  classDef external fill:#F3E5FF,stroke:#800080,color:#800080;

  click layout "https://github.com/roopsagar-k/questionpaperhub/blob/master/app/layout.tsx"
  click pageRoot "https://github.com/roopsagar-k/questionpaperhub/blob/master/app/page.tsx"
  click loginPage "https://github.com/roopsagar-k/questionpaperhub/blob/master/app/(auth)/login/page.tsx"
  click registerPage "https://github.com/roopsagar-k/questionpaperhub/blob/master/app/(auth)/register/page.tsx"
  click homePage "https://github.com/roopsagar-k/questionpaperhub/blob/master/app/(menu)/home/page.tsx"
  click dashboardPage "https://github.com/roopsagar-k/questionpaperhub/blob/master/app/(menu)/dashboard/page.tsx"
  click createPage "https://github.com/roopsagar-k/questionpaperhub/blob/master/app/create-paper/[id]/page.tsx"
  click testPage "https://github.com/roopsagar-k/questionpaperhub/blob/master/app/tests/[id]/page.tsx"
  click componentsui "https://github.com/roopsagar-k/questionpaperhub/tree/master/components/ui/"
  click button "https://github.com/roopsagar-k/questionpaperhub/blob/master/components/ui/button.tsx"
  click input "https://github.com/roopsagar-k/questionpaperhub/blob/master/components/ui/input.tsx"
  click card "https://github.com/roopsagar-k/questionpaperhub/blob/master/components/ui/card.tsx"
  click dialog "https://github.com/roopsagar-k/questionpaperhub/blob/master/components/ui/dialog.tsx"
  click userContext "https://github.com/roopsagar-k/questionpaperhub/blob/master/context/UserContext.tsx"
  click testInfoContext "https://github.com/roopsagar-k/questionpaperhub/blob/master/context/TestInfoContext.tsx"
  click authAPI "https://github.com/roopsagar-k/questionpaperhub/blob/master/app/api/auth/[...nextauth]/route.ts"
  click registerAPI "https://github.com/roopsagar-k/questionpaperhub/blob/master/app/api/register/route.ts"
  click postsAPI "https://github.com/roopsagar-k/questionpaperhub/blob/master/app/api/posts/route.ts"
  click postByIdAPI "https://github.com/roopsagar-k/questionpaperhub/blob/master/app/api/posts/[id]/route.ts"
  click commentsAPI "https://github.com/roopsagar-k/questionpaperhub/blob/master/app/api/comments/route.ts"
  click commentByIdAPI "https://github.com/roopsagar-k/questionpaperhub/blob/master/app/api/comments/[id]/route.ts"
  click votesAPI "https://github.com/roopsagar-k/questionpaperhub/blob/master/app/api/votes/[id]/route.ts"
  click bookmarksAPI "https://github.com/roopsagar-k/questionpaperhub/blob/master/app/api/bookmarks/route.ts"
  click testsAPI "https://github.com/roopsagar-k/questionpaperhub/blob/master/app/api/tests/route.ts"
  click testByIdAPI "https://github.com/roopsagar-k/questionpaperhub/blob/master/app/api/tests/[id]/route.ts"
  click addQuestionsAPI "https://github.com/roopsagar-k/questionpaperhub/blob/master/app/api/tests/add-questions/route.ts"
  click testsFileAPI "https://github.com/roopsagar-k/questionpaperhub/blob/master/app/api/tests/file/route.ts"
  click testsImagesAPI "https://github.com/roopsagar-k/questionpaperhub/blob/master/app/api/tests/images/route.ts"
  click testsTakenAPI "https://github.com/roopsagar-k/questionpaperhub/blob/master/app/api/tests-taken/route.ts"
  click testsTakenByIdAPI "https://github.com/roopsagar-k/questionpaperhub/blob/master/app/api/tests-taken/[testId]/route.ts"
  click uploadAPI "https://github.com/roopsagar-k/questionpaperhub/blob/master/app/api/upload/route.ts"
  click searchAPI "https://github.com/roopsagar-k/questionpaperhub/blob/master/app/api/search/route.ts"
  click adminAPI "https://github.com/roopsagar-k/questionpaperhub/blob/master/app/api/admin/route.ts"
  click drizzleDB "https://github.com/roopsagar-k/questionpaperhub/blob/master/lib/drizzle/db.ts"
  click drizzleSchema "https://github.com/roopsagar-k/questionpaperhub/blob/master/lib/drizzle/schema.ts"
  click drizzleMigration "https://github.com/roopsagar-k/questionpaperhub/blob/master/lib/drizzle/migrations/0000_next_zodiak.sql"
  click drizzleMigrate "https://github.com/roopsagar-k/questionpaperhub/blob/master/lib/drizzle/migrate.ts"
  click libCloudinary "https://github.com/roopsagar-k/questionpaperhub/blob/master/lib/cloudinary.ts"
  click libPDF "https://github.com/roopsagar-k/questionpaperhub/blob/master/lib/pdfToImage.ts"
  click libSignin "https://github.com/roopsagar-k/questionpaperhub/blob/master/lib/signin.ts"
  click libUploadImage "https://github.com/roopsagar-k/questionpaperhub/blob/master/lib/uploadImage.ts"
  click libUtils "https://github.com/roopsagar-k/questionpaperhub/blob/master/lib/utils.ts"
```
