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

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.

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

    Empty on the last page.

Response 400 Bad Request

invalid_parameterinvalid_cursor

Acceptance criteria

  • GET /v1/restaurants returns HTTP 200 and supports following next_cursor through all pages. For the test location (28.46, 77.03), radius 5 km and limit 7, every active restaurant at least 0.01 km inside the bounding box appears exactly once. Inactive restaurants and restaurants at least 0.01 km outside the box are not returned.
  • With a limit of 7, no page contains more than 7 items, and every page except the last contains exactly 7 items.
  • The limit defaults to 20. Parsed integer limits are clamped to 1 through 50: the tested values 0, -3, and 1 produce one item, while 50 and 500 produce 50 items. A missing limit produces 20 items. Non-integer values are rejected.
  • The radius_km parameter defaults to 5. Omitting it produces the same complete list of restaurant IDs as explicitly setting radius_km=5.
  • For a request at (28.46, 77.03) with radius_km=2 and limit=50, the endpoint returns HTTP 200 with at least one item. Each item has a positive id, a non-empty name, and lat and lng coordinates inside the requested bounding box, allowing the test's 0.01 km rounding margin.
  • Inactive restaurants are excluded even when located at the centre. An active restaurant placed 8 km north of the centre is excluded when radius_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-empty message, and field naming the offending parameter. This includes missing lat or lng, non-numeric lat, lat=NaN, lng=Inf, lat=91, lat=-90.5, and lng=180.1.
  • Malformed limits such as limit=abc and limit=2.5 return HTTP 400 with error: "invalid_parameter", a non-empty message, and field: "limit".
  • Invalid radii, including radius_km=-1, radius_km=0, radius_km=50.5 and radius_km=far, return HTTP 400 with error: "invalid_parameter", a non-empty message, and field: "radius_km".
  • A cursor the API did not issue returns HTTP 400 with error: "invalid_cursor" and a non-empty message.
  • A request for an area with no nearby restaurants, using (0, 0) and radius_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

  • lat is required and must be between -90 and 90, inclusive.
  • lng is required and must be between -180 and 180, inclusive.
  • radius_km defaults to 5, must be greater than 0, and cannot exceed 50.
  • limit defaults to 20 and is clamped to the range 1 through 50. 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:

bash
make up
make test W=W2B4

Read the test summary: ok indicates the package passed, while a FAIL identifies a test and assertion that needs investigation.

Finished Exercise 2.10?

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

Response construction

Up next · Module 4 · 2 items · about 1 h 45 min

Start module 4

Implementation

Run locally

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

  1. Expose the public restaurant listing endpoint.
  2. Validate location, radius and limit parameters.
  3. Return consistent JSON responses and error bodies.
  4. Implement cursor-based pages with no duplicate results.
  5. Filter inactive and out-of-area restaurants.
  6. Verify that the listing query can use an index.

Commands

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

Tests

  1. 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=7
    Params
    lat28.46
    lng77.03
    radius_km5
    limit7
    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}
    Params
    lat28.46
    lng77.03
    radius_km5
    limit7
    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 returned next_cursor until it is empty.

    Expected Every active restaurant at least 0.01 km inside the 5 km bounding box appears once. No inactive restaurant or restaurant at least 0.01 km outside the box appears.

    TestW2B4_WalkingEveryPageReturnsEachNearbyRestaurantOnce in backend/e2e/w2_list_test.go