# byte2code/hotel-management-api architecture

> Architecture diagram of the GitHub repository byte2code/hotel-management-api, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-05-31). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/byte2code/hotel-management-api
- Repository: https://github.com/byte2code/hotel-management-api

## Overview

1. This is a Spring Boot 3.3 / Java 17 backend named `HotelDemoApplication`, built with Maven and organized as a classic layered application: API controllers, service logic, JPA repositories, domain models, DTOs, and security/configuration.

2. The main business boundary is the hotel management domain. Core entities are `Hotel`, `Room`, `User`, and `Booking`, with enums for lifecycle/state such as `RoomStatus` and `BookingStatus`. These model the inventory and reservation workflow rather than any UI-specific behavior.

3. The API layer is split by resource: `HotelController`, `RoomController`, `BookingController`, `UserController`, and `LoginController`. Controllers handle HTTP request/response concerns and delegate all business rules to services.

4. The service layer contains the actual application logic: `HotelService`, `RoomService`, `BookingService`, and `UserService`. This is where validation, availability checks, booking confirmation/rejection, and user registration logic belong. The readme indicates booking creation includes date-range availability validation and returns a structured booking response.

5. Persistence is handled through Spring Data JPA repositories: `HotelRepository`, `RoomRepository`, `BookingRepository`, and `UserRepository`. These form the boundary to MySQL, which is the system of record for hotels, rooms, users, and bookings.

6. The DTO layer separates request/response payloads from entity persistence models. `HotelRequest`, `RoomRequest`, `BookingRequest`, `UserRequest`, and `BookingResponse` define the API contract and keep controllers from exposing entity internals directly.

7. Security is a major subsystem. `HotelSecurityConfig` and the readme show the app supports JWT resource-server style access, OAuth2 login, and method-level authorization with `@PreAuthorize`. Access control is role-based, with different permissions for normal users, authenticated hotel users, and admin-style operations.

8. External identity providers are part of the runtime architecture. The app integrates with Keycloak for JWT/OIDC-backed authentication and Google for OAuth2 login. The readme also notes that Google-authenticated users are mapped to local roles stored in the MySQL `user` table, so authorization depends on both external identity and local application data.

9. There is also a browser-facing view boundary. `LoginController` and `src/main/resources/templates/login.html` provide a Thymeleaf-based `/login` page for interactive sign-in, while the rest of the system is a REST API.

10. The main data flows are: user registration into the `User` table, hotel and room CRUD through service/repository layers, and booking creation that reads hotel/room/user records, validates availability, then persists a booking with a status outcome such as confirmed or rejected. Booking queries can be scoped by booking id, user id, or hotel id.

11. The endpoint surface is segmented by concern: `/user/*` for user management, `/hotel/*` for hotel operations, `/hotel/rooms/*` for room inventory, and `/hotel/bookings/*` for reservation workflows. Security rules are applied at endpoint and method level, not just globally.

12. Testing is focused on the service boundary. The repository includes unit/integration-style tests for `BookingService` and `RoomService`, plus a basic application bootstrap test, which suggests the business rules are expected to live primarily in the service layer rather than controllers.

13. For an architecture diagram, the clearest boundaries are: external actors and identity providers at the top, REST controllers as the entry layer, service layer as business orchestration, JPA repositories as persistence access, MySQL as the data store, and Thymeleaf as the only server-rendered UI component.

Main entry points: [`src/main/java/com/cn/hotelDemo/service/BookingService.java`](https://github.com/byte2code/hotel-management-api/blob/main/src/main/java/com/cn/hotelDemo/service/BookingService.java) (Booking service), [`src/main/java/com/cn/hotelDemo/config/HotelSecurityConfig.java`](https://github.com/byte2code/hotel-management-api/blob/main/src/main/java/com/cn/hotelDemo/config/HotelSecurityConfig.java) (Security config), [`src/main/java/com/cn/hotelDemo/service/RoomService.java`](https://github.com/byte2code/hotel-management-api/blob/main/src/main/java/com/cn/hotelDemo/service/RoomService.java) (Room service), [`src/main/java/com/cn/hotelDemo/repository/HotelRepository.java`](https://github.com/byte2code/hotel-management-api/blob/main/src/main/java/com/cn/hotelDemo/repository/HotelRepository.java) (Hotel repo)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_entry["Entry"]
  node_app(("Hotel Demo<br/>Spring Boot app"))
end

subgraph group_api["API"]
  node_hotel_ctrl["Hotels<br/>REST controller"]
  node_room_ctrl["Rooms<br/>REST controller"]
  node_booking_ctrl["Bookings<br/>REST controller"]
  node_user_ctrl["Users<br/>REST controller"]
  node_login_ctrl["Login<br/>MVC controller"]
end

subgraph group_business["Business"]
  node_hotel_svc{{"Hotel service<br/>[HotelService.java]"}}
  node_room_svc{{"Room service<br/>[RoomService.java]"}}
  node_booking_svc{{"Booking service"}}
  node_user_svc{{"User service<br/>[UserService.java]"}}
  node_booking_tests["Booking tests<br/>service test"]
  node_room_tests["Room tests<br/>service test"]
end

subgraph group_data["Data"]
  node_hotel_repo[("Hotel repo<br/>JPA repository")]
  node_room_repo[("Room repo<br/>JPA repository")]
  node_booking_repo[("Booking repo<br/>JPA repository")]
  node_user_repo[("User repo<br/>JPA repository")]
end

subgraph group_security["Security"]
  node_hotel_sec{{"Security config"}}
end

subgraph group_ui["UI"]
  node_templates_login["login.html<br/>Thymeleaf template<br/>[login.html]"]
end

subgraph group_external["External"]
  node_keycloak(("Keycloak<br/>OIDC provider"))
  node_google(("Google<br/>OAuth2 provider"))
  node_mysql[("MySQL<br/>database")]
end

node_app -->|"loads"| node_hotel_sec
node_hotel_sec -->|"trusts"| node_keycloak
node_hotel_sec -->|"enables"| node_google
node_hotel_sec -->|"protects"| node_hotel_ctrl
node_hotel_sec -->|"protects"| node_login_ctrl
node_login_ctrl -->|"renders"| node_templates_login
node_hotel_ctrl -->|"delegates"| node_hotel_svc
node_room_ctrl -->|"delegates"| node_room_svc
node_booking_ctrl -->|"delegates"| node_booking_svc
node_user_ctrl -->|"delegates"| node_user_svc
node_booking_svc -->|"reads"| node_hotel_repo
node_booking_svc -->|"reads"| node_room_repo
node_booking_svc -->|"reads"| node_user_repo
node_booking_svc -->|"writes"| node_booking_repo
node_hotel_svc -->|"uses"| node_hotel_repo
node_room_svc -->|"uses"| node_room_repo
node_user_svc -->|"uses"| node_user_repo
node_hotel_repo -->|"stores in"| node_mysql
node_room_repo -->|"stores in"| node_mysql
node_booking_repo -->|"stores in"| node_mysql
node_user_repo -->|"stores in"| node_mysql
node_booking_tests -->|"verifies"| node_booking_svc
node_room_tests -->|"verifies"| node_room_svc

click node_app "https://github.com/byte2code/hotel-management-api/blob/main/src/main/java/com/cn/hotelDemo/HotelDemoApplication.java"
click node_hotel_ctrl "https://github.com/byte2code/hotel-management-api/blob/main/src/main/java/com/cn/hotelDemo/controller/HotelController.java"
click node_room_ctrl "https://github.com/byte2code/hotel-management-api/blob/main/src/main/java/com/cn/hotelDemo/controller/RoomController.java"
click node_booking_ctrl "https://github.com/byte2code/hotel-management-api/blob/main/src/main/java/com/cn/hotelDemo/controller/BookingController.java"
click node_user_ctrl "https://github.com/byte2code/hotel-management-api/blob/main/src/main/java/com/cn/hotelDemo/controller/UserController.java"
click node_login_ctrl "https://github.com/byte2code/hotel-management-api/blob/main/src/main/java/com/cn/hotelDemo/controller/LoginController.java"
click node_hotel_svc "https://github.com/byte2code/hotel-management-api/blob/main/src/main/java/com/cn/hotelDemo/service/HotelService.java"
click node_room_svc "https://github.com/byte2code/hotel-management-api/blob/main/src/main/java/com/cn/hotelDemo/service/RoomService.java"
click node_booking_svc "https://github.com/byte2code/hotel-management-api/blob/main/src/main/java/com/cn/hotelDemo/service/BookingService.java"
click node_user_svc "https://github.com/byte2code/hotel-management-api/blob/main/src/main/java/com/cn/hotelDemo/service/UserService.java"
click node_hotel_repo "https://github.com/byte2code/hotel-management-api/blob/main/src/main/java/com/cn/hotelDemo/repository/HotelRepository.java"
click node_room_repo "https://github.com/byte2code/hotel-management-api/blob/main/src/main/java/com/cn/hotelDemo/repository/RoomRepository.java"
click node_booking_repo "https://github.com/byte2code/hotel-management-api/blob/main/src/main/java/com/cn/hotelDemo/repository/BookingRepository.java"
click node_user_repo "https://github.com/byte2code/hotel-management-api/blob/main/src/main/java/com/cn/hotelDemo/repository/UserRepository.java"
click node_hotel_sec "https://github.com/byte2code/hotel-management-api/blob/main/src/main/java/com/cn/hotelDemo/config/HotelSecurityConfig.java"
click node_templates_login "https://github.com/byte2code/hotel-management-api/blob/main/src/main/resources/templates/login.html"
click node_booking_tests "https://github.com/byte2code/hotel-management-api/blob/main/src/test/java/com/cn/hotelDemo/service/BookingServiceTest.java"
click node_room_tests "https://github.com/byte2code/hotel-management-api/blob/main/src/test/java/com/cn/hotelDemo/service/RoomServiceTest.java"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_app,node_keycloak,node_google,node_mysql toneBlue
class node_hotel_ctrl,node_room_ctrl,node_booking_ctrl,node_user_ctrl,node_login_ctrl toneAmber
class node_hotel_svc,node_room_svc,node_booking_svc,node_user_svc,node_booking_tests,node_room_tests toneMint
class node_hotel_repo,node_room_repo,node_booking_repo,node_user_repo toneRose
class node_hotel_sec toneIndigo
class node_templates_login toneTeal
```

## Components

### Entry

Application bootstrap and startup boundary

- [Hotel Demo](https://github.com/byte2code/hotel-management-api/blob/main/src/main/java/com/cn/hotelDemo/HotelDemoApplication.java): Spring Boot app · Main application bootstrap · `src/main/java/com/cn/hotelDemo/HotelDemoApplication.java`

### API

HTTP controllers and request handling

- [Hotels](https://github.com/byte2code/hotel-management-api/blob/main/src/main/java/com/cn/hotelDemo/controller/HotelController.java): REST controller · Hotel management endpoints · `src/main/java/com/cn/hotelDemo/controller/HotelController.java`
- [Rooms](https://github.com/byte2code/hotel-management-api/blob/main/src/main/java/com/cn/hotelDemo/controller/RoomController.java): REST controller · Room inventory endpoints · `src/main/java/com/cn/hotelDemo/controller/RoomController.java`
- [Bookings](https://github.com/byte2code/hotel-management-api/blob/main/src/main/java/com/cn/hotelDemo/controller/BookingController.java): REST controller · Reservation endpoints · `src/main/java/com/cn/hotelDemo/controller/BookingController.java`
- [Users](https://github.com/byte2code/hotel-management-api/blob/main/src/main/java/com/cn/hotelDemo/controller/UserController.java): REST controller · User management endpoints · `src/main/java/com/cn/hotelDemo/controller/UserController.java`
- [Login](https://github.com/byte2code/hotel-management-api/blob/main/src/main/java/com/cn/hotelDemo/controller/LoginController.java): MVC controller · Serves the login page · `src/main/java/com/cn/hotelDemo/controller/LoginController.java`

### Business

Domain services and orchestration

- [Hotel service](https://github.com/byte2code/hotel-management-api/blob/main/src/main/java/com/cn/hotelDemo/service/HotelService.java): service · Hotel CRUD and coordination · `src/main/java/com/cn/hotelDemo/service/HotelService.java`
- [Room service](https://github.com/byte2code/hotel-management-api/blob/main/src/main/java/com/cn/hotelDemo/service/RoomService.java): service · Room inventory logic · `src/main/java/com/cn/hotelDemo/service/RoomService.java`
- [Booking service](https://github.com/byte2code/hotel-management-api/blob/main/src/main/java/com/cn/hotelDemo/service/BookingService.java): service · Availability checks and booking outcomes · `src/main/java/com/cn/hotelDemo/service/BookingService.java`
- [User service](https://github.com/byte2code/hotel-management-api/blob/main/src/main/java/com/cn/hotelDemo/service/UserService.java): service · Registration and user lookup · `src/main/java/com/cn/hotelDemo/service/UserService.java`
- [Booking tests](https://github.com/byte2code/hotel-management-api/blob/main/src/test/java/com/cn/hotelDemo/service/BookingServiceTest.java): service test · Tests booking workflow behavior · `src/test/java/com/cn/hotelDemo/service/BookingServiceTest.java`
- [Room tests](https://github.com/byte2code/hotel-management-api/blob/main/src/test/java/com/cn/hotelDemo/service/RoomServiceTest.java): service test · Tests room service behavior · `src/test/java/com/cn/hotelDemo/service/RoomServiceTest.java`

### Data

Persistence models and repositories

- [Hotel repo](https://github.com/byte2code/hotel-management-api/blob/main/src/main/java/com/cn/hotelDemo/repository/HotelRepository.java): JPA repository · Hotel persistence access · `src/main/java/com/cn/hotelDemo/repository/HotelRepository.java`
- [Room repo](https://github.com/byte2code/hotel-management-api/blob/main/src/main/java/com/cn/hotelDemo/repository/RoomRepository.java): JPA repository · Room persistence access · `src/main/java/com/cn/hotelDemo/repository/RoomRepository.java`
- [Booking repo](https://github.com/byte2code/hotel-management-api/blob/main/src/main/java/com/cn/hotelDemo/repository/BookingRepository.java): JPA repository · Booking persistence access · `src/main/java/com/cn/hotelDemo/repository/BookingRepository.java`
- [User repo](https://github.com/byte2code/hotel-management-api/blob/main/src/main/java/com/cn/hotelDemo/repository/UserRepository.java): JPA repository · User persistence access · `src/main/java/com/cn/hotelDemo/repository/UserRepository.java`

### Security

Authentication and authorization

- [Security config](https://github.com/byte2code/hotel-management-api/blob/main/src/main/java/com/cn/hotelDemo/config/HotelSecurityConfig.java): security config · JWT, OAuth2, and method security rules · `src/main/java/com/cn/hotelDemo/config/HotelSecurityConfig.java`

### UI

Server-rendered login view

- [login.html](https://github.com/byte2code/hotel-management-api/blob/main/src/main/resources/templates/login.html): Thymeleaf template · Browser login view · `src/main/resources/templates/login.html`

### External

Identity providers and database

- **Keycloak**: OIDC provider · JWT and OIDC authentication source
- **Google**: OAuth2 provider · External OAuth2 login provider
- **MySQL**: database · System of record for hotels, rooms, users, and bookings

## Connections

- Hotel Demo → Security config: loads · Application starts with security configuration
- Security config → Keycloak: trusts · Validates JWT/OIDC tokens from Keycloak
- Security config → Google: enables · Supports OAuth2 login via Google
- Security config → Hotels: protects · Applies endpoint and method authorization
- Security config → Login: protects · Controls access to interactive login flow
- Login → login.html: renders · Serves the Thymeleaf login page
- Hotels → Hotel service: delegates · Controller passes hotel requests to service
- Rooms → Room service: delegates · Controller passes room requests to service
- Bookings → Booking service: delegates · Controller passes booking requests to service
- Users → User service: delegates · Controller passes user requests to service
- Booking service → Hotel repo: reads · Loads hotel data for booking decisions
- Booking service → Room repo: reads · Checks room availability and inventory
- Booking service → User repo: reads · Verifies user data and ownership
- Booking service → Booking repo: writes · Persists booking results and status
- Hotel service → Hotel repo: uses · Persists and retrieves hotels
- Room service → Room repo: uses · Persists and retrieves rooms
- User service → User repo: uses · Persists and retrieves users
- Hotel repo → MySQL: stores in · Hotel records live in MySQL
- Room repo → MySQL: stores in · Room records live in MySQL
- Booking repo → MySQL: stores in · Booking records live in MySQL
- User repo → MySQL: stores in · User records live in MySQL
- Booking tests → Booking service: verifies · Exercises booking workflow rules
- Room tests → Room service: verifies · Exercises room service logic
