# gkanawati/anunx architecture

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

- Interactive diagram: https://gitdiagram.com/gkanawati/anunx
- Repository: https://github.com/gkanawati/anunx

## Overview

##### Project Type and Purpose
- This is a full-stack web application (“Anunx – Anúncios Online”) built on Next.js.
- It offers user authentication (Next-Auth + Google OAuth), CRUD operations for product listings, image uploads (Cloudinary), and data persistence in MongoDB Atlas.
- The same Next.js codebase powers both the client-side rendering (React/Material-UI) and the backend API routes (Node.js + Express-style controllers + Mongoose).

##### File-Structure & Architectural Patterns
Top-Level Directories
- pages/ – Next.js pages (UI + API endpoints under pages/api).
- public/ – Static assets (images, CSS).
- src/components/ – Reusable React UI components.
- src/controllers/ – Business-logic handlers invoked by API routes.
- src/models/ – Mongoose schemas for users, products.
- src/utils/ – Helpers (dbConnect, password hashing, form value schemas).
- src/contexts, src/themes, src/styles – App theming and state contexts.

Patterns
- Layered MVC-style:
– Presentation: pages/ + components/ + Material-UI theme.
– Controllers: src/controllers handle request logic.
– Models: src/models handle data schema & persistence.
- API-route boundary: Next.js API routes in pages/api delegate to controllers.
- External services: MongoDB Atlas (database), Cloudinary (file storage), NextAuth (OAuth).

##### README Insights
- Tech Stack: Next.js, React, Material-UI, MongoDB Atlas, Cloudinary, Mongoose, bcrypt, formik, React-Dropzone, NextAuth.
- Deployment: Vercel.
- Responsive design, dark/light theme via Material-UI.

a. Main Components
- Client Browser (React pages, Material-UI)
- Next.js Server (SSR + Static + API routes)
- API Controllers layer (signin, products, users)
- Data Layer: MongoDB Atlas cluster
- File Storage: Cloudinary service
- Auth Provider: NextAuth + Google OAuth

b. Relationships & Data Flows
1. User ↔ Browser UI: React pages (SSR/CSR), form submissions (Sign in, Publish ad).
2. Browser → Next.js API: HTTP calls to /api/auth, /api/products, /api/users.
3. API Controllers → Data Layer: Mongoose → MongoDB Atlas.
4. API Controllers → Cloudinary: Image upload/delete calls.
5. NextAuth → Google OAuth external endpoints for login.

c. Architectural Patterns & Principles
- Separation of concerns: UI vs Business logic vs Data access layers.
- Server-Side Rendering (SSR) and API-first: Next.js pages for SEO + fast initial load, RESTful API routes for data.
- Authentication middleware (NextAuth) guarding pages via CheckAuth component.

d. Key Technologies / Frameworks
- Next.js (pages/, routing, SSR, API routes)
- React + Material-UI (UI components, theming)
- Mongoose + MongoDB Atlas (ODM + cloud DB)
- Cloudinary SDK (image storage)
- NextAuth (session management, OAuth)

## Diagram (Mermaid)

```mermaid
flowchart TB
    %% UI Layer
    subgraph "Frontend UI Layer"
        direction TB
        Pages["Next.js Pages (pages/)"]:::frontend
        AppEntry["_app.js / _document.js"]:::frontend
    end

    %% API Layer
    subgraph "Next.js API Routes" 
        direction TB
        AuthAPI["Auth API (pages/api/auth/[...nextauth].js)"]:::api
        SignInAPI["Sign In API (pages/api/auth/signin.js)"]:::api
        ProdAdd["Add Product API (pages/api/products/add.js)"]:::api
        ProdDel["Delete Product API (pages/api/products/delete.js)"]:::api
        ProdUpd["Update Product API (pages/api/products/update/[id].js)"]:::api
        UserAPI["Users API (pages/api/users.js)"]:::api
    end

    %% Controllers Layer
    subgraph "Controllers (Business Logic)" 
        direction TB
        AuthCtrl["Auth Controller (src/controllers/auth/signin.js)"]:::controller
        ProdCtrl["Products Controller (src/controllers/products.js)"]:::controller
        UserCtrl["Users Controller (src/controllers/users.js)"]:::controller
    end

    %% Data & Utils Layer
    subgraph "Data & Utils Layer"
        direction TB
        DBConn["DB Connection (src/utils/dbConnect.js)"]:::util
        UserModel["User Schema (src/models/users.js)"]:::model
        ProdModel["Product Schema (src/models/products.js)"]:::model
    end

    %% External Services
    subgraph "External Services"
        direction TB
        MongoDB[(MongoDB Atlas)]:::data
        Cloudinary[(Cloudinary Storage)]:::data
        GoogleOAuth((Google OAuth)):::external
    end

    %% Relationships
    Pages -->|SSR/CSR| AppEntry
    AppEntry -->|Initial Page Render| Pages
    Pages -->|HTTP Requests| AuthAPI
    Pages -->|HTTP Requests| SignInAPI
    Pages -->|HTTP Requests| ProdAdd
    Pages -->|HTTP Requests| ProdDel
    Pages -->|HTTP Requests| ProdUpd
    Pages -->|HTTP Requests| UserAPI

    AuthAPI -->|calls| AuthCtrl
    SignInAPI -->|calls| AuthCtrl
    ProdAdd -->|calls| ProdCtrl
    ProdDel -->|calls| ProdCtrl
    ProdUpd -->|calls| ProdCtrl
    UserAPI -->|calls| UserCtrl

    AuthCtrl -->|uses| DBConn
    ProdCtrl -->|uses| DBConn
    UserCtrl -->|uses| DBConn

    AuthCtrl -->|reads/writes| UserModel
    ProdCtrl -->|reads/writes| ProdModel
    UserCtrl -->|reads/writes| UserModel

    ProdCtrl -->|upload/delete| Cloudinary
    AuthCtrl -->|OAuth Flow| GoogleOAuth

    DBConn -->|connects| MongoDB

    %% Click Events
    click Pages "https://github.com/gkanawati/anunx/tree/master/pages/"
    click AppEntry "https://github.com/gkanawati/anunx/blob/master/pages/_app.js"
    click AppEntry "https://github.com/gkanawati/anunx/blob/master/pages/_document.js"
    click AuthAPI "https://github.com/gkanawati/anunx/blob/master/pages/api/auth/[...nextauth].js"
    click SignInAPI "https://github.com/gkanawati/anunx/blob/master/pages/api/auth/signin.js"
    click ProdAdd "https://github.com/gkanawati/anunx/blob/master/pages/api/products/add.js"
    click ProdDel "https://github.com/gkanawati/anunx/blob/master/pages/api/products/delete.js"
    click ProdUpd "https://github.com/gkanawati/anunx/blob/master/pages/api/products/update/[id].js"
    click UserAPI "https://github.com/gkanawati/anunx/blob/master/pages/api/users.js"
    click AuthCtrl "https://github.com/gkanawati/anunx/blob/master/src/controllers/auth/signin.js"
    click ProdCtrl "https://github.com/gkanawati/anunx/blob/master/src/controllers/products.js"
    click UserCtrl "https://github.com/gkanawati/anunx/blob/master/src/controllers/users.js"
    click DBConn "https://github.com/gkanawati/anunx/blob/master/src/utils/dbConnect.js"
    click UserModel "https://github.com/gkanawati/anunx/blob/master/src/models/users.js"
    click ProdModel "https://github.com/gkanawati/anunx/blob/master/src/models/products.js"

    %% Styles
    classDef frontend fill:#D0E6F8,stroke:#3B82F6,color:#1E3A8A
    classDef api fill:#D1FAE5,stroke:#10B981,color:#065F46
    classDef controller fill:#A7F3D0,stroke:#059669,color:#065F46
    classDef util fill:#FEF3C7,stroke:#D97706,color:#92400E
    classDef model fill:#FEE2E2,stroke:#EF4444,color:#991B1B
    classDef data fill:#FFEDD5,stroke:#F97316,color:#7C2D12
    classDef external fill:#E9D5FF,stroke:#8B5CF6,color:#5B21B6
```
