# sapphirepegasus/mern-hotelbooking architecture

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

- Interactive diagram: https://gitdiagram.com/sapphirepegasus/mern-hotelbooking
- Repository: https://github.com/sapphirepegasus/mern-hotelbooking

## Overview

##### Project type and purpose
- This is a MERN-stack full-stack web application for hotel booking (React frontend, Express/Node backend, MongoDB database).
- The README confirms: “Please run the backend first and then the frontend” and mentions a MongoDB token.

##### File structure
- Top-level “api” folder: Express server, MVC pattern (models, controllers, routes), utility middleware (error handling, token verification), package files.
- Top-level “client” folder: React app (components, pages, hooks, context for Auth and Search, public static assets).
- No other services or microservices—monolith back end and single-page front end.

##### Main components
a. Client (Browser)
- React SPA
- Pages: Home, List, Hotel, Login, Signup, Reserve
- Components: Header, Footer, Featured, SearchItem, etc.
- Context providers: AuthContext, SearchContext
- Custom hook: useFetch for API calls
b. API Server
- Express app (api/index.js)
- Routes layer: /auth, /hotels, /rooms, /users
- Controllers: business logic for each route
- Models: Mongoose schemas for User, Hotel, Room
- Middleware: verifyToken, error handler
c. Database
- MongoDB Atlas (external service, connection via .env token)
d. External/third-party
- JWT for authentication
- Mongoose ORM
- Axios or fetch (client-side)

##### Relationships and data flows
- Browser ↔ API over HTTPS/REST (CRUD calls: GET hotels, POST login, etc.)
- API ↔ MongoDB via Mongoose driver
- Frontend Context (AuthContext) wraps protected pages, passes JWT in Authorization header
- useFetch abstracts calls to endpoints

##### Architectural patterns and design principles
- Client: component-based hierarchy, single-page application, unidirectional data flow (React + Context)
- Server: MVC (Model-View-Controller) with routes → controllers → models
- Secured routes via token verification middleware
- Separation of concerns: UI vs API vs Data layer

##### Detailed elements
- Component names (e.g., Featured, SearchItem) only if needed for clarity
- Token verification middleware in the server layer preceding protected routes
- DB connection string pulled from .env (annotation)
- Error handler middleware catching controller errors

##### Capturing essential architecture
- Don’t clutter with every CSS file—focus on logical components.
- How authentication flows from Login page → AuthContext → API/auth → JWT → subsequent requests.
- REST endpoints and their mapping to controllers/models.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Client Layer
    subgraph "Client Layer"
        direction TB
        App["React SPA\n(App.js)"]:::client
        subgraph "Pages"
            direction TB
            Home["Home Page\n(Home.jsx)"]:::client
            List["List Page\n(List.jsx)"]:::client
            HotelPg["Hotel Page\n(Hotel.jsx)"]:::client
            Login["Login Page\n(Login.jsx)"]:::client
            Signup["Signup Page\n(Signup.jsx)"]:::client
        end
        subgraph "Components"
            direction TB
            Header["Header Component\n(Header.jsx)"]:::client
            Footer["Footer Component\n(Footer.jsx)"]:::client
            Featured["Featured Component\n(Featured.jsx)"]:::client
            SearchItem["SearchItem Component\n(SearchItem.jsx)"]:::client
            Reserve["Reserve Component\n(Reserve.jsx)"]:::client
        end
        subgraph "State & Hooks"
            direction TB
            AuthCtx["AuthContext Provider\n(AuthContext.js)"]:::client
            SearchCtx["SearchContext Provider\n(SearchContext.js)"]:::client
            UseFetch["useFetch Hook\n(useFetch.js)"]:::client
        end
    end

    %% Server Layer
    subgraph "Server Layer"
        direction TB
        Express["Express Server\n(index.js)"]:::server
        subgraph "Middleware"
            direction TB
            Verify["Token Verification\n(verifyToken.js)"]:::server
            ErrorHandler["Error Handling\n(error.js)"]:::server
        end
        subgraph "Routes"
            direction TB
            AuthR["/auth Routes\n(auth.js)"]:::server
            HotelR["/hotels Routes\n(hotels.js)"]:::server
            RoomR["/rooms Routes\n(rooms.js)"]:::server
            UserR["/users Routes\n(users.js)"]:::server
        end
        subgraph "Controllers"
            direction TB
            AuthC["Auth Controller\n(auth.js)"]:::server
            HotelC["Hotel Controller\n(hotel.js)"]:::server
            RoomC["Room Controller\n(room.js)"]:::server
            UserC["User Controller\n(user.js)"]:::server
        end
        subgraph "Models"
            direction TB
            UserM["User Model\n(User.js)"]:::server
            HotelM["Hotel Model\n(Hotel.js)"]:::server
            RoomM["Room Model\n(Room.js)"]:::server
        end
    end

    %% Data Layer
    MongoDB[(MongoDB Atlas)]:::database

    %% Connections: Client to Server
    Home -->|"useFetch (REST/JSON)"| UseFetch
    List -->|"useFetch (REST/JSON)"| UseFetch
    HotelPg -->|"useFetch (REST/JSON)"| UseFetch
    Reserve -->|"useFetch (REST/JSON)"| UseFetch
    Login -->|"useFetch (REST/JSON)"| UseFetch
    Signup -->|"useFetch (REST/JSON)"| UseFetch

    UseFetch -->|"HTTP Requests (GET/POST)"| Express
    AuthCtx -->|wraps| Home
    AuthCtx -->|wraps| List
    AuthCtx -->|wraps| HotelPg
    AuthCtx -->|wraps| Reserve

    %% Server internal flow
    Express --> ErrorHandler
    Express --> Verify
    Express --> AuthR
    Express --> HotelR
    Express --> RoomR
    Express --> UserR

    AuthR --> AuthC
    HotelR --> Verify
    HotelR --> HotelC
    RoomR --> Verify
    RoomR --> RoomC
    UserR --> Verify
    UserR --> UserC

    AuthC --> UserM
    HotelC --> HotelM
    RoomC --> RoomM
    UserC --> UserM

    UserM --> MongoDB
    HotelM --> MongoDB
    RoomM --> MongoDB

    %% Click Events
    click App "https://github.com/sapphirepegasus/mern-hotelbooking/blob/main/client/src/App.js"
    click Home "https://github.com/sapphirepegasus/mern-hotelbooking/blob/main/client/src/pages/home/Home.jsx"
    click List "https://github.com/sapphirepegasus/mern-hotelbooking/blob/main/client/src/pages/list/List.jsx"
    click HotelPg "https://github.com/sapphirepegasus/mern-hotelbooking/blob/main/client/src/pages/hotel/Hotel.jsx"
    click Login "https://github.com/sapphirepegasus/mern-hotelbooking/blob/main/client/src/pages/login/Login.jsx"
    click Signup "https://github.com/sapphirepegasus/mern-hotelbooking/blob/main/client/src/pages/signup/Signup.jsx"
    click Header "https://github.com/sapphirepegasus/mern-hotelbooking/blob/main/client/src/components/header/Header.jsx"
    click Footer "https://github.com/sapphirepegasus/mern-hotelbooking/blob/main/client/src/components/footer/Footer.jsx"
    click Featured "https://github.com/sapphirepegasus/mern-hotelbooking/blob/main/client/src/components/featured/Featured.jsx"
    click SearchItem "https://github.com/sapphirepegasus/mern-hotelbooking/blob/main/client/src/components/searchItem/SearchItem.jsx"
    click Reserve "https://github.com/sapphirepegasus/mern-hotelbooking/blob/main/client/src/components/reserve/Reserve.jsx"
    click AuthCtx "https://github.com/sapphirepegasus/mern-hotelbooking/blob/main/client/src/context/AuthContext.js"
    click SearchCtx "https://github.com/sapphirepegasus/mern-hotelbooking/blob/main/client/src/context/SearchContext.js"
    click UseFetch "https://github.com/sapphirepegasus/mern-hotelbooking/blob/main/client/src/hooks/useFetch.js"
    click Express "https://github.com/sapphirepegasus/mern-hotelbooking/blob/main/api/index.js"
    click AuthR "https://github.com/sapphirepegasus/mern-hotelbooking/blob/main/api/routes/auth.js"
    click HotelR "https://github.com/sapphirepegasus/mern-hotelbooking/blob/main/api/routes/hotels.js"
    click RoomR "https://github.com/sapphirepegasus/mern-hotelbooking/blob/main/api/routes/rooms.js"
    click UserR "https://github.com/sapphirepegasus/mern-hotelbooking/blob/main/api/routes/users.js"
    click AuthC "https://github.com/sapphirepegasus/mern-hotelbooking/blob/main/api/controllers/auth.js"
    click HotelC "https://github.com/sapphirepegasus/mern-hotelbooking/blob/main/api/controllers/hotel.js"
    click RoomC "https://github.com/sapphirepegasus/mern-hotelbooking/blob/main/api/controllers/room.js"
    click UserC "https://github.com/sapphirepegasus/mern-hotelbooking/blob/main/api/controllers/user.js"
    click UserM "https://github.com/sapphirepegasus/mern-hotelbooking/blob/main/api/models/User.js"
    click HotelM "https://github.com/sapphirepegasus/mern-hotelbooking/blob/main/api/models/Hotel.js"
    click RoomM "https://github.com/sapphirepegasus/mern-hotelbooking/blob/main/api/models/Room.js"
    click Verify "https://github.com/sapphirepegasus/mern-hotelbooking/blob/main/api/utils/verifyToken.js"
    click ErrorHandler "https://github.com/sapphirepegasus/mern-hotelbooking/blob/main/api/utils/error.js"

    %% Styles
    classDef client fill:#D6EAF8,stroke:#1B4F72,color:#1B2631;
    classDef server fill:#D5F5E3,stroke:#186A3B,color:#145A32;
    classDef database fill:#FCF3CF,stroke:#B9770E,color:#7E5109;
```
