StaffCoder
Spring Boot
Spring BootSpring Boot 4.1Java 25
12 modules · 48 challengesSign up
Module 5 · Querying and Filtering

Hotel Room Availability Query

easy100%1 of 1 solved

Problem statement

Build a REST API for finding hotel rooms that are free across a requested date range.

Requirements

RoomRepository

Add one query method taking the stay start, the stay end, and type that returns the available rooms:

  • A room is available when none of its bookings overlaps the stay, and a booking overlaps when it starts before the stay end and ends after the stay start
  • A null type applies no restriction; otherwise the room type must equal it, ignoring case
  • Results come back ordered by roomNumber ascending

AvailabilityController

Implement the single REST endpoint. It must be reachable at exactly the verb and path listed below and accepts the query parameters from, to, and type.

GET /rooms/available

  • from and to are required and hold ISO-formatted dates, for example 2026-08-10; the requested stay runs from the from night up to a checkout on the to day
  • If from or to is missing, return 400 Bad Request
  • If from or to is not a valid ISO date, return 400 Bad Request
  • If from is the same day as to or a later day, return 400 Bad Request with error message: "from must be before to"; a one-night stay, with to one day after from, is valid
  • Return 200 OK with every room that has no booking overlapping the requested range. A booking overlaps the range when it starts before to and ends after from. A booking that ends exactly on from does not block the room, and neither does a booking that starts exactly on to, because a room frees up on its checkout day. A booking that shares even a single night with the range blocks the room, whether it starts before the range and ends inside it, starts inside and ends after it, lies fully inside it, or covers it completely
  • When type is supplied and is not blank, return only the available rooms whose type equals it, ignoring case; a value that merely starts a stored type matches nothing
  • When type is omitted or blank, return available rooms of every type
  • Order the rooms by roomNumber ascending; each carries its id, roomNumber, and type
  • When no room is available, return 200 OK with an empty array

Sample Requests & Responses

Expand to view details on sample requests and responses.

GET /rooms/available?from=2026-08-10&to=2026-08-12

Response (200 OK):

[
    {
        "id": 2,
        "roomNumber": 102,
        "type": "double"
    },
    {
        "id": 3,
        "roomNumber": 201,
        "type": "double"
    },
    {
        "id": 4,
        "roomNumber": 202,
        "type": "suite"
    },
    {
        "id": 5,
        "roomNumber": 301,
        "type": "suite"
    }
]

GET /rooms/available?from=2026-08-10&to=2026-08-12&type=suite

Response (200 OK):

[
    {
        "id": 4,
        "roomNumber": 202,
        "type": "suite"
    },
    {
        "id": 5,
        "roomNumber": 301,
        "type": "suite"
    }
]

GET /rooms/available?from=2026-08-14&to=2026-08-16

Response (200 OK):

[
    {
        "id": 1,
        "roomNumber": 101,
        "type": "single"
    },
    {
        "id": 2,
        "roomNumber": 102,
        "type": "double"
    },
    {
        "id": 3,
        "roomNumber": 201,
        "type": "double"
    },
    {
        "id": 4,
        "roomNumber": 202,
        "type": "suite"
    },
    {
        "id": 5,
        "roomNumber": 301,
        "type": "suite"
    }
]

GET /rooms/available?from=2026-08-01&to=2026-08-03&type=suite

Response (200 OK):

[
    {
        "id": 4,
        "roomNumber": 202,
        "type": "suite"
    }
]

GET /rooms/available?from=2026-08-10&to=2026-08-12&type=penthouse

Response (200 OK):

[]

GET /rooms/available?from=2026-08-12&to=2026-08-12

Response (400 Bad Request):

{
    "error": "from must be before to",
    "timestamp": "2026-07-30T10:46:41.305859",
    "path": "/rooms/available"
}

GET /rooms/available?from=2026-08-10

Response (400 Bad Request):

{
    "timestamp": "2026-07-30T05:01:41.562+00:00",
    "status": 400,
    "error": "Bad Request",
    "path": "/rooms/available"
}

GET /rooms/available?from=aug-10&to=2026-08-12

Response (400 Bad Request):

{
    "timestamp": "2026-07-30T05:01:41.832+00:00",
    "status": 400,
    "error": "Bad Request",
    "path": "/rooms/available"
}

More in Querying and Filtering

See the module
Farmers Market Vendor SearchThis problem covers searching vendors by several optional filters that can be combined in any way.100%1 of 1 solvedeasyUsed Textbook Listings FilterThis problem covers filtering listings by a price range and other options, with the results always ordered newest first.100%1 of 1 solvedeasyPodcast Episode BrowserThis problem covers returning results one page at a time, with a capped page size and a consistent page envelope.100%1 of 1 solvedeasy

We use cookies that are necessary to run Staffcoder, and optional analytics cookies to see which pages get used. Analytics stays off unless you accept. Read our Cookie Policy.