# alivrick/web_backend_booking_hotel architecture

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

- Interactive diagram: https://gitdiagram.com/alivrick/web_backend_booking_hotel
- Repository: https://github.com/alivrick/web_backend_booking_hotel

## Overview

##### Main Components
- Backend API (Spring Boot): This is organized using a layered approach with controllers, services, repositories, models (entities), DTOs, and security configurations. It implements key functionalities such as hotel searching, booking management, real‑time room availability, payment integration, and authentication.
- Frontends (React + Vite): Although not part of the file tree, the README indicates three distinct UI modules – Admin, Rental, and User. Each interacts with the backend through designated REST APIs.
- Database: The backend interacts with a relational database (PostgreSQL/MySQL) to persist all data (users, hotels, bookings, payment records, etc.).
- External Services:
⁃ JWT & Authentication services (including token management, email confirmation, password recovery).
⁃ Payment Gateway (indicated by the VnPayService) for online deposits and full payments.
⁃ Email services for notifications (see EmailService usage).

##### Relationships and Interactions
- Frontend to Backend:
- The three separate frontends initiate RESTful API calls to the backend’s endpoints (e.g., AuthController, BookingController, HotelController).
- Authentication data flows through JWT (secured by filters such as AuthTokenFilter and handled in JwtUtils).
- Backend Internal Flow:
- Controllers receive external requests and delegate business logic to corresponding Services (BookingService, HotelService, UserService, etc.).
- Services perform operations and interact with the database using Repositories (like BookingRepository, HotelRepository, etc.).
- The Security Layer (WebSecurityConfig, JwtAuthEntryPoint, CorsConfig) governs access, while exceptions and DTOs (e.g., PaymentResDTO) facilitate error handling and data transfer.
- External Integrations:
- Payment integration is handled by the VnPayService where the system decides between deposit and full payment.
- Email confirmations and password resets are processed using an EmailService.

##### Architectural Patterns and Design Principles
- MVC Pattern: The backend naturally segments into controllers, services (business logic), and models/repositories (data persistence).
- Layered Architecture: Separation between presentation (controllers), business logic (services), and data access (repositories).
- Security Best Practices: JWT authentication along with multi-role access control.
- Extensibility: The use of DTOs and clearly defined service interfaces (IUserService, IHotelService, etc.) prepares the system for future enhancements.

## Diagram (Mermaid)

```mermaid
flowchart TD
    subgraph "Frontend"
        admin("Admin UI"):::frontend
        rental("Rental UI"):::frontend
        user("User UI"):::frontend
    end

    subgraph "Backend"
        subgraph "Controllers"
            auth("AuthController"):::backendController
            booking("BookingController"):::backendController
            payment("PaymentController"):::backendController
            hotel("HotelController"):::backendController
        end

        subgraph "Services"
            bookingService("BookingService"):::backendService
            hotelService("HotelService"):::backendService
            userService("UserService"):::backendService
            vnpay("VnPayService"):::backendService
            email("EmailService"):::backendService
        end

        subgraph "Repositories"
            bookingRepo("BookingRepository"):::repository
            hotelRepo("HotelRepository"):::repository
            userRepo("UserRepository"):::repository
        end

        subgraph "Models & DTOs"
            models("Models"):::model
            dtos("DTOs"):::model
        end

        security("Security (JWT,Filters)"):::security
    end

    subgraph "Database"
        db("Relational Database"):::database
    end

    subgraph "External Services"
        pg("Payment Gateway"):::external
        es("Email Server"):::external
    end

    %% Frontend to Controllers
    admin -->|"REST_API"| auth
    rental -->|"REST_API"| booking
    user -->|"REST_API"| hotel

    %% Controllers to Security (JWT Validation)
    auth -->|"JWT_Validate"| security

    %% Controllers to Services
    auth -->|"Delegate"| userService
    booking -->|"Delegate"| bookingService
    hotel -->|"Delegate"| hotelService
    payment -->|"Delegate"| vnpay
    payment -->|"Delegate"| email

    %% Services to Repositories
    bookingService -->|"DataAccess"| bookingRepo
    hotelService -->|"DataAccess"| hotelRepo
    userService -->|"DataAccess"| userRepo

    %% Repositories to Database
    bookingRepo -->|"CRUD"| db
    hotelRepo -->|"CRUD"| db
    userRepo -->|"CRUD"| db

    %% Services using Models & DTOs
    bookingService -- "Uses" --> models
    bookingService -- "Uses" --> dtos
    hotelService -- "Uses" --> models
    hotelService -- "Uses" --> dtos
    userService -- "Uses" --> models
    userService -- "Uses" --> dtos
    auth -- "Binds" --> dtos

    %% External Integrations
    vnpay -->|"Process_Payment"| pg
    email -->|"Send_Email"| es

    %% Click Events for Component Mapping
    click auth "https://github.com/alivrick/web_backend_booking_hotel/tree/main/src/main/java/com/example/hotel_booking_be_v1/controller"
    click booking "https://github.com/alivrick/web_backend_booking_hotel/tree/main/src/main/java/com/example/hotel_booking_be_v1/controller"
    click payment "https://github.com/alivrick/web_backend_booking_hotel/tree/main/src/main/java/com/example/hotel_booking_be_v1/controller"
    click hotel "https://github.com/alivrick/web_backend_booking_hotel/tree/main/src/main/java/com/example/hotel_booking_be_v1/controller"

    click bookingService "https://github.com/alivrick/web_backend_booking_hotel/tree/main/src/main/java/com/example/hotel_booking_be_v1/service"
    click hotelService "https://github.com/alivrick/web_backend_booking_hotel/tree/main/src/main/java/com/example/hotel_booking_be_v1/service"
    click userService "https://github.com/alivrick/web_backend_booking_hotel/tree/main/src/main/java/com/example/hotel_booking_be_v1/service"
    click vnpay "https://github.com/alivrick/web_backend_booking_hotel/blob/main/src/main/java/com/example/hotel_booking_be_v1/service/VnPayService.java"
    click email "https://github.com/alivrick/web_backend_booking_hotel/blob/main/src/main/java/com/example/hotel_booking_be_v1/service/EmailService.java"

    click bookingRepo "https://github.com/alivrick/web_backend_booking_hotel/tree/main/src/main/java/com/example/hotel_booking_be_v1/repository"
    click hotelRepo "https://github.com/alivrick/web_backend_booking_hotel/tree/main/src/main/java/com/example/hotel_booking_be_v1/repository"
    click userRepo "https://github.com/alivrick/web_backend_booking_hotel/tree/main/src/main/java/com/example/hotel_booking_be_v1/repository"

    click models "https://github.com/alivrick/web_backend_booking_hotel/tree/main/src/main/java/com/example/hotel_booking_be_v1/model"
    click dtos "https://github.com/alivrick/web_backend_booking_hotel/tree/main/src/main/java/com/example/hotel_booking_be_v1/DTO"
    click security "https://github.com/alivrick/web_backend_booking_hotel/tree/main/src/main/java/com/example/hotel_booking_be_v1/security"

    %% Styles
    classDef frontend fill:#a1cbe8,stroke:#31708E,stroke-width:2px
    classDef backendController fill:#f2dede,stroke:#a94442,stroke-width:2px
    classDef backendService fill:#d9edf7,stroke:#31708E,stroke-width:2px
    classDef repository fill:#fcf8e3,stroke:#8a6d3b,stroke-width:2px
    classDef model fill:#dff0d8,stroke:#3c763d,stroke-width:2px
    classDef security fill:#f9e79f,stroke:#f39c12,stroke-width:2px
    classDef external fill:#fad7a0,stroke:#e67e22,stroke-width:2px
    classDef database fill:#ffe0b3,stroke:#d35400,stroke-width:2px
```
