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
Active restaurants near a point, one page at a time
latnumberrequired-90 to 90
lngnumberrequired-180 to 180
radius_kmnumberabove 0, at most 50, default 5
limitintegerdefault 20. Integers below 1 count as 1 and above 50 as 50.
cursorstringnext_cursor from the previous page of the same query.
atstring (date-time)RFC 3339 time that is_open is evaluated at. Defaults to now.
itemsRestaurantSummary[]requiredidintegerrequirednamestringrequiredlatnumberrequiredlngnumberrequiredis_openbooleanrequiredWhether 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_cursorstringrequiredEmpty on the last page.
invalid_parameterinvalid_cursor
Acceptance criteria
- Every restaurant returned by
GET /v1/restaurantsincludes an explicit booleanis_openfield, 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
atparameter is interpreted as an absolute RFC 3339 timestamp and converted toAsia/Kolkatabefore evaluating slots. For a slot from 11:00 to 12:00,2026-01-05T05:30:00Zis open and2026-01-05T05:29:00Zis closed.2026-01-05T14:45:00+09:00is open and2026-01-05T11:15:00+09:00is 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_slotscannot 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
atvalue that is not RFC 3339 returns HTTP 400 witherror: "invalid_parameter"andfield: "at". This includesyesterday,2026-13-01T00:00:00Z,2026-01-05 10:00and1767600000. - When
atis omitted,is_openis evaluated using the current time inAsia/Kolkata.
Constraints
- Store
open_minas a minute from 0 through 1439 andclose_minas a minute from 0 through 1440. HOURS_REFRESH_INTERVALis a Go duration. Its default is1m.- Until the hours have been loaded successfully for the first time,
is_openisfalse.
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 populateis_openwithout loading opening-hours records during a list request.db/migrations/anddb/queries/can defineopen_slotsand retrieve its records.internal/seed/can populate opening-hours records alongside the existing restaurant data.cmd/api/can loadAsia/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:
cd backend && make up
cd backend && make test W=W2B7Read 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.
The reference solution passes every check for this exercise. Try the hints first; reading the answer before you have struggled with the problem teaches much less.
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
Up next · Module 7 · 2 items · about 1 h 30 min
Implementation
Run locally
Write the code in your editor inside zomato-prep, then work through this list.
- Add the
open_slotsmigration and opening-hours seed data. - Build the open-hours index from restaurant slots.
- Handle local time, overnight slots and multiple slots.
- Publish refreshed snapshots without racing with readers.
- Add
is_openand RFC 3339athandling to the restaurant list. - Pass the W2B7 end-to-end tests.
Commands
cd backend && make upcd backend && make test W=W2B7Tests
Every restaurant returned by the listing endpoint includes a boolean
is_openfield.GET/v1/restaurants?lat=28.46&lng=77.03&radius_km=5&limit=50Paramslat 28.46 lng 77.03 radius_km 5 limit 50 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_EveryListedRestaurantHasABooleanIsOpeninbackend/e2e/w2_hours_test.go