Skip to content

pushpak90/STAYEASE

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

3 Commits
Β 
Β 
Β 
Β 

Repository files navigation

🏨 StayEase – Hotel Room Booking REST API

StayEase is a backend service built with Java and Spring Boot that provides REST APIs for managing hotels and room bookings in a hotel aggregator platform.

The application supports JWT-based authentication, role-based access control (RBAC), and uses MySQL for persistent storage.

Tech Stack

  • Java
  • Spring Boot
  • Spring Security
  • JWT Authentication
  • MySQL
  • JPA / Hibernate
  • Gradle

Key Features

  • User registration and login with JWT authentication
  • Role-based access control (ADMIN, HOTEL_MANAGER, USER)
  • Hotel listing and management APIs
  • Room booking creation and cancellation
  • Layered architecture (Controller β†’ Service β†’ Repository)

πŸ“Œ High-Level Architecture

Client (Postman / Curl / Frontend)
        |
        v
Controller Layer  β†’  Service Layer  β†’  Repository Layer  β†’  MySQL DB
        |
        v
   Security Layer (JWT + Roles)

🧩 Folder Structure (Detailed)

src/main/java/com/takehome/stayease
β”‚
β”œβ”€β”€ controller
β”‚   β”œβ”€β”€ AuthController.java
β”‚   β”œβ”€β”€ HotelController.java
β”‚   └── BookingController.java
β”‚
β”œβ”€β”€ service
β”‚   β”œβ”€β”€ UserService.java
β”‚   β”œβ”€β”€ HotelService.java
β”‚   β”œβ”€β”€ BookingService.java
β”‚   └── Impl
β”‚       β”œβ”€β”€ UserServiceImpl.java
β”‚       β”œβ”€β”€ HotelServiceImpl.java
β”‚       └── BookingServiceImpl.java
β”‚
β”œβ”€β”€ repository
β”‚   β”œβ”€β”€ UserRepository.java
β”‚   β”œβ”€β”€ HotelRepository.java
β”‚   └── BookingRepository.java
β”‚
β”œβ”€β”€ entity
β”‚   β”œβ”€β”€ User.java
β”‚   β”œβ”€β”€ Hotel.java
β”‚   └── Booking.java
β”‚
β”œβ”€β”€ dto
β”‚   β”œβ”€β”€ auth
β”‚   β”‚   β”œβ”€β”€ SignupRequest.java
β”‚   β”‚   β”œβ”€β”€ LoginRequest.java
β”‚   β”‚   └── AuthResponse.java
β”‚   β”‚
β”‚   β”œβ”€β”€ hotel
β”‚   β”‚   β”œβ”€β”€ CreateHotelRequest.java
β”‚   β”‚   β”œβ”€β”€ UpdateHotelRequest.java
β”‚   β”‚   └── HotelResponse.java
β”‚   β”‚
β”‚   └── booking
β”‚       β”œβ”€β”€ CreateBookingRequest.java
β”‚       └── BookingResponse.java
β”‚
β”œβ”€β”€ security
β”‚   β”œβ”€β”€ SecurityConfig.java
β”‚   β”œβ”€β”€ JwtUtil.java
β”‚   β”œβ”€β”€ JwtAuthenticationFilter.java
β”‚   β”œβ”€β”€ CustomUserDetails.java
β”‚   └── CustomUserDetailsService.java
β”‚
β”œβ”€β”€ exception
β”‚   └── GlobalExceptionHandler.java
β”‚
└── StayEaseApplication.java

πŸ” Role-Based Access Control (RBAC)

Role Permissions
USER View hotels, create booking
HOTEL_MANAGER Update hotels, cancel bookings
ADMIN Create hotels, delete hotels

Role Enforcement

Implemented using:

@PreAuthorize("hasRole('ADMIN')")
@PreAuthorize("hasRole('HOTEL_MANAGER')")
@PreAuthorize("hasRole('USER')")

JWT token contains role information and is validated on every request.


πŸ”‘ Authentication Flow (JWT)

  1. User registers or logs in
  2. Server validates credentials
  3. JWT token is generated
  4. Client sends token in header:
    Authorization: Bearer <JWT_TOKEN>
    
  5. JWT filter validates token and sets security context

πŸ“‘ API Endpoints (Detailed)

πŸ‘€ User APIs

Register User (Public)

POST /api/users/register
{
  "email": "user@test.com",
  "password": "Test@1234",
  "firstName": "John",
  "lastName": "Doe",
  "role": "USER"
}

Response:

{ "token": "jwt-token" }

Login User (Public)

POST /api/users/login
{
  "email": "user@test.com",
  "password": "Test@1234"
}

Response:

{ "token": "jwt-token" }

🏨 Hotel APIs

Get All Hotels (Public)

GET /api/hotels

Create Hotel (Admin)

POST /api/hotels
Authorization: Bearer <ADMIN_TOKEN>
{
  "name": "StayEase Hotel",
  "location": "Pune",
  "description": "Business Hotel",
  "totalRooms": 10,
  "availableRooms": 10
}

Update Hotel (Hotel Manager)

PUT /api/hotels/{hotelId}
Authorization: Bearer <MANAGER_TOKEN>
{
  "availableRooms": 15
}

Delete Hotel (Admin)

DELETE /api/hotels/{hotelId}
Authorization: Bearer <ADMIN_TOKEN>

πŸ“… Booking APIs

Create Booking (User)

POST /api/bookings/{hotelId}
Authorization: Bearer <USER_TOKEN>
{
  "checkInDate": "2026-02-20",
  "checkOutDate": "2026-02-22"
}

Response:

{
  "bookingId": 1,
  "hotelId": 2,
  "checkInDate": "2026-02-20",
  "checkOutDate": "2026-02-22"
}

Get Booking Details

GET /api/bookings/{bookingId}
Authorization: Bearer <USER_TOKEN>

Cancel Booking (Hotel Manager)

DELETE /api/bookings/{bookingId}
Authorization: Bearer <MANAGER_TOKEN>

⚠️ Business Rules Enforced

  • Check-in date must be future date
  • Check-out date must be after check-in
  • No overbooking allowed
  • Customers cannot cancel bookings
  • Only managers can cancel bookings

πŸ§ͺ Testing Strategy

  • Controller-level unit tests
  • MockMvc + Mockito
  • Security filters disabled during tests
  • No real DB used in tests

Run tests:

./gradlew test

▢️ Run the Application

./gradlew clean bootRun

App runs on:

http://localhost:8081

πŸ“¦ Build JAR

./gradlew clean bootJar
java -jar build/libs/stayease-0.0.1-SNAPSHOT.jar

About

StayEase is a Spring Boot REST API for hotel listing and room booking, featuring JWT authentication, role-based access control, MySQL persistence, and a clean layered architecture.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors