GET /v1/restaurants with a cursor
Build the cursor package and the first real endpoint: nearby restaurants, validated input, keyset pages and an opaque next_cursor.
Ticket
Build the public GET /v1/restaurants endpoint. A customer supplies a location and optionally a search radius and page size, then follows next_cursor to browse active restaurants in that area. You need to make the listing reliable when the restaurant data changes between requests: customers must not miss restaurants or see the same restaurant twice while moving through the pages.
This is the week's first HTTP endpoint, building on the restaurant data and MySQL query work from the preceding steps. It also introduces the shared error response body, {error, message, field}, which W2B5 and W2B6 reuse. W2B7 adds is_open to the listing, W2B8 tests it, and the optional steps extend the endpoint further.
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.
itemsRestaurantSummary[]requiredidintegerrequirednamestringrequiredlatnumberrequiredlngnumberrequired
next_cursorstringrequiredEmpty on the last page.
invalid_parameterinvalid_cursor
Acceptance criteria
GET /v1/restaurantsreturns HTTP 200 and supports followingnext_cursorthrough all pages. For the test location(28.46, 77.03), radius5km and limit7, every active restaurant at least0.01km inside the bounding box appears exactly once. Inactive restaurants and restaurants at least0.01km outside the box are not returned.- With a limit of
7, no page contains more than7items, and every page except the last contains exactly7items. - The
limitdefaults to20. Parsed integer limits are clamped to1through50: the tested values0,-3, and1produce one item, while50and500produce 50 items. A missing limit produces 20 items. Non-integer values are rejected. - The
radius_kmparameter defaults to5. Omitting it produces the same complete list of restaurant IDs as explicitly settingradius_km=5. - For a request at
(28.46, 77.03)withradius_km=2andlimit=50, the endpoint returns HTTP 200 with at least one item. Each item has a positiveid, a non-emptyname, andlatandlngcoordinates inside the requested bounding box, allowing the test's0.01km rounding margin. - Inactive restaurants are excluded even when located at the centre. An active restaurant placed
8km north of the centre is excluded whenradius_km=5. - Pagination remains stable when data changes between requests. In the tested walk with limit
5, deactivating a restaurant after page 1 and inserting a new restaurant during the walk must not cause duplicate results or skip any restaurant ID returned by the initial walk. - Missing, malformed, non-finite or out-of-range coordinates return HTTP 400 with
error: "invalid_parameter", a non-emptymessage, andfieldnaming the offending parameter. This includes missinglatorlng, non-numericlat,lat=NaN,lng=Inf,lat=91,lat=-90.5, andlng=180.1. - Malformed limits such as
limit=abcandlimit=2.5return HTTP 400 witherror: "invalid_parameter", a non-emptymessage, andfield: "limit". - Invalid radii, including
radius_km=-1,radius_km=0,radius_km=50.5andradius_km=far, return HTTP 400 witherror: "invalid_parameter", a non-emptymessage, andfield: "radius_km". - A cursor the API did not issue returns HTTP 400 with
error: "invalid_cursor"and a non-emptymessage. - A request for an area with no nearby restaurants, using
(0, 0)andradius_km=1, returns HTTP 200 with exactly{"items":[],"next_cursor":""}. - The list request executes a query against
restaurants, and EXPLAIN identifies a usable index for that query.
Constraints
latis required and must be between-90and90, inclusive.lngis required and must be between-180and180, inclusive.radius_kmdefaults to5, must be greater than0, and cannot exceed50.limitdefaults to20and is clamped to the range1through50. A value that cannot be parsed as an integer is rejected.- Each page contains at most the requested number of items. Only the final page may be shorter than the requested limit.
Design direction
A layout that works well is a shared httpx package for JSON responses and consistent error bodies, an internal/restaurant package with the HTTP handler separated from the service, and an internal/cursor package that owns cursor encoding and decoding. Keep HTTP parsing and status codes in the handler. Keep geographic calculations, database queries, row conversion and pagination in the service. The service should not need to know about HTTP.
Fetch limit + 1 rows so you can determine whether another page exists without a separate count query. Keep the extra row out of the response and create the next cursor from the last row you actually returned.
Prefer keyset pagination over offset pagination. An offset can shift when an earlier row is deactivated or a new row is inserted, causing later requests to skip or repeat restaurants. A keyset cursor continues from a stable ordering position.
The geographic filter is a rectangular bounding box, not an exact circular distance check. This keeps the query straightforward, but restaurants near the corners of the box can be farther away than the requested radius when measured as a straight-line distance. Keep the query compatible with an available index on restaurants, and use EXPLAIN to check the result rather than forcing a particular index choice.
Any layout passes the tests.
How to check your work
Start the local services and run the W2B4 tests:
make up
make test W=W2B4Read the test summary: ok indicates the package passed, while a FAIL identifies a test and assertion that needs investigation.
No hints for this exercise.
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.
Listing API and pagination
3 items
- 2.8JSON HTTP handlers in Gonot completed
- 2.9Keyset (cursor) paginationnot completed
- 2.10GET /v1/restaurants with a cursornot completed
Up next · Module 4 · 2 items · about 1 h 45 min
Implementation
Run locally
Write the code in your editor inside zomato-prep, then work through this list.
- Expose the public restaurant listing endpoint.
- Validate location, radius and limit parameters.
- Return consistent JSON responses and error bodies.
- Implement cursor-based pages with no duplicate results.
- Filter inactive and out-of-area restaurants.
- Verify that the listing query can use an index.
Commands
cd backend && make upcd backend && make test W=W2B4Tests
Following every cursor returns each active restaurant inside the tested area exactly once and excludes inactive or sufficiently distant restaurants.
GET/v1/restaurants?lat=28.46&lng=77.03&radius_km=5&limit=7Paramslat 28.46 lng 77.03 radius_km 5 limit 7 Response 200 OK{ "items": [ { "id": 12, "name": "…", "lat": 28.4612, "lng": 77.0291 }, … ], "next_cursor": "eyJ…" }GET/v1/restaurants?lat=28.46&lng=77.03&radius_km=5&limit=7&cursor={next_cursor}Paramslat 28.46 lng 77.03 radius_km 5 limit 7 cursor {next_cursor} Response 200 OK{ "items": [ { "id": 12, "name": "…", "lat": 28.4612, "lng": 77.0291 }, … ], "next_cursor": "eyJ…" }Input
GET /v1/restaurants?lat=28.46&lng=77.03&radius_km=5&limit=7, followed by each returnednext_cursoruntil it is empty.Expected Every active restaurant at least
0.01km inside the 5 km bounding box appears once. No inactive restaurant or restaurant at least0.01km outside the box appears.TestW2B4_WalkingEveryPageReturnsEachNearbyRestaurantOnceinbackend/e2e/w2_list_test.go