GET /v1/restaurants: open status

You build is_open into GET /v1/restaurants and accept an optional RFC 3339 at timestamp to evaluate each restaurant's hours. A background refresh keeps the index current on HOURS_REFRESH_INTERVAL.

Ticket

Build is_open into GET /v1/restaurants. Add an optional at query parameter in RFC 3339 format. When it is present, evaluate each restaurant at that absolute timestamp. Otherwise, use the current time in Asia/Kolkata.

The restaurant list needs this status for the open or closed badge in the Week 4 app. This module follows the menu pricing endpoint from W2B6. The next module has you write integration tests against the restaurant list.

Endpoint

GET/v1/restaurants

Active restaurants near a point, one page at a time

Query parameters
  • latnumberrequired

    -90 to 90

  • lngnumberrequired

    -180 to 180

  • radius_kmnumber

    above 0, at most 50, default 5

  • limitinteger

    default 20. Integers below 1 count as 1 and above 50 as 50.

  • cursorstring

    next_cursor from the previous page of the same query.

  • atstring (date-time)

    RFC 3339 time that is_open is evaluated at. Defaults to now.

Response 200 OK
  • itemsRestaurantSummary[]required
    • idintegerrequired
    • namestringrequired
    • latnumberrequired
    • lngnumberrequired
    • is_openbooleanrequired

      Whether one of the restaurant's open_slots contains the request's at time, in Asia/Kolkata local minutes. A slot covers open_min up to but not including close_min; when close_min is not after open_min the slot runs past midnight. Before the hours are first loaded this is false.

  • next_cursorstringrequired

    Empty on the last page.

Response 400 Bad Request

invalid_parameterinvalid_cursor

Acceptance criteria

  • Every restaurant returned by GET /v1/restaurants includes an explicit boolean is_open field, never null or missing.
  • A restaurant with a slot from 11:00 to 23:00 is closed at 10:59, open at 11:00 and 22:59, and closed at 23:00.
  • The optional at parameter is interpreted as an absolute RFC 3339 timestamp and converted to Asia/Kolkata before evaluating slots. For a slot from 11:00 to 12:00, 2026-01-05T05:30:00Z is open and 2026-01-05T05:29:00Z is closed. 2026-01-05T14:45:00+09:00 is open and 2026-01-05T11:15:00+09:00 is closed.
  • An overnight slot from 18:00 to 03:00 is open at 18:00, 23:59, 00:00 and 02:59. It is closed at 17:59, 03:00 and 12:00.
  • A restaurant with slots from 08:00 to 10:00, 12:00 to 15:00 and 22:00 to 01:00 is open at 09:00, 13:00, 23:00 and 00:30. It is closed at 11:00, 16:00 and 01:00.
  • A restaurant with no slots is always closed. A restaurant with one slot from minute 0 to minute 1440 is always open.
  • When 150 restaurants have different schedules, each restaurant receives its own correct status at 11:30. This includes restaurants whose bit positions are past 64.
  • Changes to opening-hours records become visible through the restaurant list within three configured refresh intervals.
  • If a background refresh fails because open_slots cannot be read, the last successfully loaded snapshot remains available and its opening statuses are still served.
  • Across 20 restaurant list requests, fewer than five SQL statements reference open_slots.
  • An at value that is not RFC 3339 returns HTTP 400 with error: "invalid_parameter" and field: "at". This includes yesterday, 2026-13-01T00:00:00Z, 2026-01-05 10:00 and 1767600000.
  • When at is omitted, is_open is evaluated using the current time in Asia/Kolkata.

Constraints

  • Store open_min as a minute from 0 through 1439 and close_min as a minute from 0 through 1440.
  • HOURS_REFRESH_INTERVAL is a Go duration. Its default is 1m.
  • Until the hours have been loaded successfully for the first time, is_open is false.

Design direction

A useful layout separates index construction from the restaurant list and the database loading process:

  • internal/hours/ can own slot evaluation, the open-hours index and its background refresh lifecycle.
  • internal/restaurant/ can read the current snapshot and populate is_open without loading opening-hours records during a list request.
  • db/migrations/ and db/queries/ can define open_slots and retrieve its records.
  • internal/seed/ can populate opening-hours records alongside the existing restaurant data.
  • cmd/api/ can load Asia/Kolkata, perform the initial load and start the refresh goroutine.

Any package or file layout that meets the contract passes.

Think about these trade-offs:

  • Building versus reading: Build the complete index outside the request path. Listing requests should read an existing snapshot rather than query open_slots.
  • Immutable snapshots versus shared mutation: A complete replacement published with atomic.Pointer keeps readers from observing a partially rebuilt index. This also avoids a Data race.
  • Refresh ownership: A context-owned ticker gives the background refresh goroutine a clear lifecycle. It should stop when the server shuts down.

How to check your work

From the repository root, start the services and run the W2B7 end-to-end tests:

bash
cd backend && make up
cd backend && make test W=W2B7

Read the first failing assertion in the test output. It identifies the observable behaviour that failed. Fix that behaviour before investigating later failures, which may be consequences of the same issue.

Finished Exercise 2.17?

Open-hours index

3 items

  • 2.15Bitsets for opening hoursnot completed
  • 2.16Background refresh with atomic.Pointernot completed
  • 2.17GET /v1/restaurants: open statusnot completed

Restaurant list test suite

Up next · Module 7 · 2 items · about 1 h 30 min

Start module 7

Implementation

Run locally

Write the code in your editor inside zomato-prep, then work through this list.

  1. Add the open_slots migration and opening-hours seed data.
  2. Build the open-hours index from restaurant slots.
  3. Handle local time, overnight slots and multiple slots.
  4. Publish refreshed snapshots without racing with readers.
  5. Add is_open and RFC 3339 at handling to the restaurant list.
  6. Pass the W2B7 end-to-end tests.

Commands

terminal
cd backend && make up
terminal
cd backend && make test W=W2B7

Tests

  1. Every restaurant returned by the listing endpoint includes a boolean is_open field.

    GET/v1/restaurants?lat=28.46&lng=77.03&radius_km=5&limit=50
    Params
    lat28.46
    lng77.03
    radius_km5
    limit50
    Response 200 OK
    {
      "items": [
        { "id": 12, "name": "…", "is_open": true, … },
        …
      ],
      "next_cursor": "…"
    }

    Input Send a restaurant listing request using the test centre coordinates, radius 5 km, and limit 50.

    Expected HTTP 200, at least one restaurant returned, and every restaurant has a non-null boolean is_open.

    TestW2B7_EveryListedRestaurantHasABooleanIsOpen in backend/e2e/w2_hours_test.go