# pruthavik01/roombuddy architecture

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

- Interactive diagram: https://gitdiagram.com/pruthavik01/roombuddy
- Repository: https://github.com/pruthavik01/roombuddy

## Overview

##### Project Type & Purpose
- This is a full-stack web application (RoomBuddy) for listing and booking hostels, built on Node.js/Express with MongoDB as the database and EJS templates on the front end.
- It follows a classical MVC pattern, augmented by middleware and external services (Cloudinary, SendGrid, OSM/Leaflet).

##### Main Components
a. Client (Browser)
- Static assets: HTML (rendered from EJS), CSS/Bootstrap, JS (map.js, script.js)
- Interacts over HTTP/HTTPS with Express server.
b. Web Server (Express.js)
- app.js: Application entry point
- cloudConfig.js: Cloudinary setup
- middleware.js & utils (ExpressError, wrapAsync)
- schema.js: Joi validation
- Controllers: listings.js, reviews.js, users.js
- Routes: listing.js, review.js, user.js
- Session management: express-session + connect-mongo
- Authentication: Passport.js (local strategy) + passport-local-mongoose
c. Database (MongoDB)
- Collections: users, listings, reviews
- init scripts for seeding (init/index.js & init/data.js)
- Mongoose models & schemas in models/
d. External Services
- Cloudinary (image storage) via multer-storage-cloudinary
- SendGrid (OTP email verification)
- MapTiler / OpenStreetMap / Nominatim (geocoding & map tiles)
e. Deployment/Hosting
- Hosted on Render (as indicated by public URL)
- Environment variables managed by dotenv

##### Relationships & Data Flow
- 1. Client sends HTTP requests (GET/POST/PATCH/DELETE) to Express routes
- 2. Express routes ➔ controller methods (wrapped in async error handler)
- 3. Controllers perform:
- Input validation via Joi (schema.js)
- Business logic (e.g., check owner-only access)
- DB operations via Mongoose models
- File uploads to Cloudinary (if any)
- OTP generation & email via SendGrid (signup flow)
- Session & cookie handling
- 4. Database reads/writes to MongoDB
- 5. Controllers render EJS views or respond with redirects/JSON
- 6. Client receives HTML/CSS/JS, updates UI (e.g., Leaflet map)

##### Architectural Patterns & Design Principles
- MVC:
- Models: /models
- Views: /views (EJS templates + includes + layouts)
- Controllers: /controllers
- Middleware chain:
- Authentication & authorization
- Error handling (ExpressError)
- Validation (Joi)
- Static asset serving (/public)
- Service Integration Layer: external API wrappers (Cloudinary, SendGrid, Nominatim)
- Single codebase monolith but logically separated into layers

## Diagram (Mermaid)

```mermaid
flowchart TB
    %% Client Side
    subgraph "Client (Browser)" 
        direction TB
        EJS["EJS Pages"]:::frontend
        CSS["CSS / Bootstrap"]:::frontend
        ClientJS["Client JS (map.js, script.js)"]:::frontend
        Images["Static Images"]:::frontend
    end

    click CSS "https://github.com/pruthavik01/roombuddy/blob/main/public/css/style.css"
    click ClientJS "https://github.com/pruthavik01/roombuddy/blob/main/public/js/map.js"
    click ClientJS "https://github.com/pruthavik01/roombuddy/blob/main/public/js/script.js"
    click Images "https://github.com/pruthavik01/roombuddy/tree/main/public/image"

    %% Server Side
    subgraph "Express Server" 
        direction TB
        Entry["app.js"]:::server
        click Entry "https://github.com/pruthavik01/roombuddy/blob/main/app.js"
        Config["cloudConfig.js"]:::server
        click Config "https://github.com/pruthavik01/roombuddy/blob/main/cloudConfig.js"
        Middleware["middleware.js"]:::server
        click Middleware "https://github.com/pruthavik01/roombuddy/blob/main/middleware.js"
        Validation["schema.js"]:::server
        click Validation "https://github.com/pruthavik01/roombuddy/blob/main/schema.js"
        
        subgraph "Routes"
            direction TB
            ListingRoute["routes/listing.js"]:::server
            click ListingRoute "https://github.com/pruthavik01/roombuddy/blob/main/routes/listing.js"
            ReviewRoute["routes/review.js"]:::server
            click ReviewRoute "https://github.com/pruthavik01/roombuddy/blob/main/routes/review.js"
            UserRoute["routes/user.js"]:::server
            click UserRoute "https://github.com/pruthavik01/roombuddy/blob/main/routes/user.js"
        end

        subgraph "Controllers"
            direction TB
            ListingCtrl["controllers/listings.js"]:::server
            click ListingCtrl "https://github.com/pruthavik01/roombuddy/blob/main/controllers/listings.js"
            ReviewCtrl["controllers/reviews.js"]:::server
            click ReviewCtrl "https://github.com/pruthavik01/roombuddy/blob/main/controllers/reviews.js"
            UserCtrl["controllers/users.js"]:::server
            click UserCtrl "https://github.com/pruthavik01/roombuddy/blob/main/controllers/users.js"
        end

        subgraph "Models"
            direction TB
            ListingModel["models/listing.js"]:::server
            click ListingModel "https://github.com/pruthavik01/roombuddy/blob/main/models/listing.js"
            ReviewModel["models/review.js"]:::server
            click ReviewModel "https://github.com/pruthavik01/roombuddy/blob/main/models/review.js"
            UserModel["models/user.js"]:::server
            click UserModel "https://github.com/pruthavik01/roombuddy/blob/main/models/user.js"
        end

        subgraph "Utilities"
            direction TB
            ErrorUtil["utils/ExpressError.js"]:::server
            click ErrorUtil "https://github.com/pruthavik01/roombuddy/blob/main/utils/ExpressError.js"
            AsyncUtil["utils/wrapAsync.js"]:::server
            click AsyncUtil "https://github.com/pruthavik01/roombuddy/blob/main/utils/wrapAsync.js"
        end

        subgraph "Init Scripts"
            direction TB
            InitIndex["init/index.js"]:::server
            click InitIndex "https://github.com/pruthavik01/roombuddy/blob/main/init/index.js"
            InitData["init/data.js"]:::server
            click InitData "https://github.com/pruthavik01/roombuddy/blob/main/init/data.js"
        end

        Package["package.json"]:::server
        click Package "https://github.com/pruthavik01/roombuddy/blob/main/package.json"
        Lock["package-lock.json"]:::server
        click Lock "https://github.com/pruthavik01/roombuddy/blob/main/package-lock.json"
        GitIgnore[".gitignore"]:::server
        click GitIgnore "https://github.com/pruthavik01/roombuddy/blob/main/.gitignore"
        Readme["README.md"]:::server
        click Readme "https://github.com/pruthavik01/roombuddy/blob/main/README.md"
    end

    %% Database
    subgraph "MongoDB" 
        direction TB
        UsersColl["Collection: users"]:::db
        ListingsColl["Collection: listings"]:::db
        ReviewsColl["Collection: reviews"]:::db
        SessionsColl["Collection: sessions"]:::db
    end

    %% External Services
    subgraph "External Services" 
        direction TB
        Cloudinary["Cloudinary"]:::external
        SendGrid["SendGrid"]:::external
        MapAPI["MapTiler/OSM/Nominatim"]:::external
    end

    %% Views
    subgraph "EJS Views" 
        direction TB
        ListingsViews["views/listings/*.ejs"]:::frontend
        click ListingsViews "https://github.com/pruthavik01/roombuddy/tree/main/views/listings/"
        UsersViews["views/users/*.ejs"]:::frontend
        click UsersViews "https://github.com/pruthavik01/roombuddy/tree/main/views/users/"
        Includes["views/includes/*.ejs"]:::frontend
        click Includes "https://github.com/pruthavik01/roombuddy/tree/main/views/includes/"
        Layouts["views/layouts/boilerplate.ejs"]:::frontend
        click Layouts "https://github.com/pruthavik01/roombuddy/blob/main/views/layouts/boilerplate.ejs"
        ErrorView["views/error.ejs"]:::frontend
        click ErrorView "https://github.com/pruthavik01/roombuddy/blob/main/views/error.ejs"
    end

    %% Connections
    EJS -->|renders via HTML| Entry
    CSS -->|served static| Entry
    ClientJS -->|AJAX HTTP/HTTPS| Entry
    Images -->|static assets| Entry

    Entry -->|uses Routes| ListingRoute
    Entry -->|uses Routes| ReviewRoute
    Entry -->|uses Routes| UserRoute
    ListingRoute -->|calls| ListingCtrl
    ReviewRoute -->|calls| ReviewCtrl
    UserRoute -->|calls| UserCtrl

    ListingCtrl -->|validates via| Validation
    ListingCtrl -->|uses models| ListingModel
    ListingCtrl -->|uploads images to| Cloudinary
    ReviewCtrl -->|uses models| ReviewModel
    UserCtrl -->|validates via| Validation
    UserCtrl -->|uses models| UserModel
    UserCtrl -->|sends OTP via| SendGrid

    Entry -->|session store| SessionsColl
    ListingModel -->|Mongoose TCP| UsersColl
    ListingModel -->|Mongoose TCP| ListingsColl
    ReviewModel -->|Mongoose TCP| ReviewsColl
    UserModel -->|Mongoose TCP| UsersColl

    Entry -->|geocoding REST| MapAPI

    ListingCtrl -->|renders views| ListingsViews
    UserCtrl -->|renders views| UsersViews
    Entry -->|renders includes| Includes
    Entry -->|renders layout| Layouts
    Entry -->|renders error| ErrorView

    Entry -->|connects| GitIgnore
    Entry -->|connects| Readme

    %% Styles
    classDef frontend fill:#BBDEFB,stroke:#1E88E5,color:#0D47A1;
    classDef server fill:#C8E6C9,stroke:#388E3C,color:#1B5E20;
    classDef db fill:#FFE0B2,stroke:#F57C00,color:#E65100;
    classDef external fill:#E1BEE7,stroke:#8E24AA,color:#4A148C;
```
