# ytw418/breeder_web architecture

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

- Interactive diagram: https://gitdiagram.com/ytw418/breeder_web
- Repository: https://github.com/ytw418/breeder_web

## Overview

##### Project type and purpose
- This is a full-stack Next.js 14 application in TypeScript that implements a peer-to-peer second-hand marketplace and community (“Breeder”).
- Core domains: product listings, user profiles, real-time chat, reviews, and authentication.

##### File structure
- app/ (Next.js “App Router” pages & client components): all public UI routes, nested folders for pages (main, chat, posts, products, profiles, auth).
- pages/api/: REST API endpoints organized by domain (auth, chat, posts, products, users).
- components/: reusable UI atoms, auth widgets, feature compositions, theme provider.
- libs/: shared utilities split into client (fetcher, SWR, providers) and server (Prisma client wrapper, session & handler middlewares).
- prisma/: database schema and migrations.
- public/: static assets (images, robots.txt).
- config & infra: next.config.js, tailwind.config.js, middleware.ts.

##### Key insights from the README
- Tech stack: Next.js, React, TypeScript, TailwindCSS, Prisma (ORM), iron-session (auth), SWR (data fetching).
- Coding rules enforced by .cursor/rules (API, page, style conventions).
- API responses always `{ success: boolean, ... }`.
- DB access exclusively via libs/server/client.ts (Prisma wrapper).

##### Elements
- Labels for each component (Browser, Next.js SSR, API handlers, DB, CDN, Auth service)
- Green for API/middleware
- Orange for database
- Gray for external services/CDN
- Frontend (app/ + SWR + components/)
- Backend (pages/api/ + libs/server/)
- Persistence (Prisma + PostgreSQL)
- Third-party (OAuth, Vercel env)

## Diagram (Mermaid)

```mermaid
flowchart TB
    %% Frontend Layer
    subgraph "Frontend (app/ & components/ & hooks/)" 
        direction TB
        Browser["Client Browser"]:::ext
        MainPage["Main Page\n(app/(web)/main)"]:::ui
        ChatPage["Chat Pages\n(app/(web)/chat)"]:::ui
        PostsPage["Posts Pages\n(app/(web)/posts)"]:::ui
        ProductsPage["Products Pages\n(app/(web)/products)"]:::ui
        ProfilesPage["Profiles Pages\n(app/(web)/profiles)"]:::ui
        AuthPage["Auth Pages\n(app/auth/login)"]:::ui
        EditProfilePage["Edit Profile Page\n(app/editProfile)"]:::ui
        Components["UI Components & Hooks\n(components/, hooks/)"]:::ui
        LibsClient["Shared Client Utils\n(libs/client/)"]:::ui
    end

    %% Backend Layer
    subgraph "Backend (pages/api/ & libs/server/ & middleware.ts)"
        direction TB
        AppServer["Next.js App Server"]:::api
        APIAuth["Auth API\n(pages/api/auth/)"]:::api
        APIChat["Chat API\n(pages/api/chat/)"]:::api
        APIPosts["Posts API\n(pages/api/posts/)"]:::api
        APIProducts["Products API\n(pages/api/products/)"]:::api
        APIUsers["Users API\n(pages/api/users/)"]:::api
        APIFiles["Files API\n(pages/api/files.ts)"]:::api
        APIReviews["Reviews API\n(pages/api/reviews.ts)"]:::api
        Middleware["Edge & API Middleware\n(middleware.ts)"]:::api
        LibsServer["Server Utils & Prisma Client\n(libs/server/)"]:::api
    end

    %% Persistence Layer
    subgraph "Persistence (Prisma & DB)"
        direction TB
        PrismaSchema["Prisma Schema\n(prisma/schema.prisma)"]:::db
        Migrations["Prisma Migrations\n(prisma/migrations/)"]:::db
        PostgresDB["PostgreSQL Database"]:::db
    end

    %% External Services
    subgraph "External Services & CDN"
        direction TB
        OAuth["OAuth Providers\n(e.g., Apple)"]:::ext
        CDN["Static Assets CDN\n(public/)"]:::ext
        VercelEnv["Vercel Env Vars"]:::ext
    end

    %% Relationships
    Browser -->|"HTTP Requests"| AppServer
    AppServer -->|"Serves UI\n(SSR/SSG/CSR)"| Browser

    Browser -->|"API Calls via SWR/fetch"| APIAuth
    Browser --> APIChat
    Browser --> APIPosts
    Browser --> APIProducts
    Browser --> APIUsers
    Browser --> APIFiles
    Browser --> APIReviews

    APIAuth -->|"/api/auth/*"| Middleware
    APIChat -->|"/api/chat/*"| Middleware
    APIPosts -->|"/api/posts/*"| Middleware
    APIProducts -->|"/api/products/*"| Middleware
    APIUsers -->|"/api/users/*"| Middleware
    APIFiles -->|"/api/files"| Middleware
    APIReviews -->|"/api/reviews"| Middleware

    Middleware -->|withSession & withHandler| LibsServer
    LibsServer -->|PrismaClient| PrismaSchema
    LibsServer --> PrismaSchema
    PrismaSchema --> Migrations
    PrismaSchema -->|Queries| PostgresDB
    Migrations --> PostgresDB

    APIAuth --> OAuth
    CDN --> Browser
    VercelEnv --> AppServer

    %% Click Events
    click MainPage "https://github.com/ytw418/breeder_web/blob/main/app/(web)/(main)/page.tsx"
    click ChatPage "https://github.com/ytw418/breeder_web/blob/main/app/(web)/chat/ChatClient.tsx"
    click PostsPage "https://github.com/ytw418/breeder_web/blob/main/app/(web)/posts/PostsClient.tsx"
    click ProductsPage "https://github.com/ytw418/breeder_web/blob/main/app/(web)/products/[id]/ProductClient.tsx"
    click ProfilesPage "https://github.com/ytw418/breeder_web/blob/main/app/(web)/profiles/[id]/ProfileClient.tsx"
    click AuthPage "https://github.com/ytw418/breeder_web/blob/main/app/auth/login/page.tsx"
    click EditProfilePage "https://github.com/ytw418/breeder_web/blob/main/app/editProfile/page.tsx"
    click Components "https://github.com/ytw418/breeder_web/tree/main/components/atoms"
    click LibsClient "https://github.com/ytw418/breeder_web/blob/main/libs/client/fetcher.tsx"
    click APIAuth "https://github.com/ytw418/breeder_web/blob/main/pages/api/auth/login.ts"
    click APIChat "https://github.com/ytw418/breeder_web/blob/main/pages/api/chat/index.ts"
    click APIPosts "https://github.com/ytw418/breeder_web/blob/main/pages/api/posts/index.ts"
    click APIProducts "https://github.com/ytw418/breeder_web/blob/main/pages/api/products/index.ts"
    click APIUsers "https://github.com/ytw418/breeder_web/blob/main/pages/api/users/me/index.ts"
    click APIFiles "https://github.com/ytw418/breeder_web/blob/main/pages/api/files.ts"
    click APIReviews "https://github.com/ytw418/breeder_web/blob/main/pages/api/reviews.ts"
    click Middleware "https://github.com/ytw418/breeder_web/blob/main/middleware.ts"
    click LibsServer "https://github.com/ytw418/breeder_web/blob/main/libs/server/client.ts"
    click PrismaSchema "https://github.com/ytw418/breeder_web/blob/main/prisma/schema.prisma"
    click Migrations "https://github.com/ytw418/breeder_web/tree/main/prisma/migrations/20250427_initial"
    click CDN "https://github.com/ytw418/breeder_web/tree/main/public/images"
    click OAuth "https://github.com/ytw418/breeder_web/blob/main/pages/api/auth/apple/callback.ts"

    %% Styles
    classDef ui fill:#d0e8ff,stroke:#0366d6;
    classDef api fill:#e6ffed,stroke:#28a745;
    classDef db fill:#fff5b1,stroke:#dbab09;
    classDef ext fill:#e1e4e8,stroke:#959da5;
```
