GET /v1/restaurants/{id}/menu in five queries

Load a whole menu in at most five batched queries and assemble the nested JSON in Go.

Ticket

Build GET /v1/restaurants/{id}/menu so a client can retrieve a restaurant's complete menu in one request. The response must include items, their sizes and prices, option groups, available options, and each option's size-dependent prices. This gives the customer app the information it needs to display a configurable menu without making separate requests for each level.

This builds on W2B4's handler and service split and its shared error response format. W2B6 will use these same database tables to calculate item prices from customer selections, so the menu data must preserve the correct relationships between items, groups, options and prices.

Endpoint

GET/v1/restaurants/{id}/menu

The full menu of one restaurant

Path parameters
  • idintegerrequired

    at least 1

Response 200 OK
  • restaurant_idintegerrequired
  • itemsMenuItem[]required
    • idintegerrequired
    • namestringrequired
    • sizesobject[]required
      • sizestringrequired
      • price_paiseintegerrequired
    • groupsobject[]required
      • idintegerrequired
      • namestringrequired
      • min_selectintegerrequired
      • max_selectintegerrequired
      • optionsobject[]required
        • idintegerrequired
        • namestringrequired
        • in_stockbooleanrequired
        • pricesmap of integerrequired

          Size to price in paise. Key * means every size.

Response 400 Bad Request

invalid_parameter

Response 404 Not Found

restaurant_not_found

Acceptance criteria

  • A public GET /v1/restaurants/1/menu returns HTTP 200 with restaurant_id: 1 and the seeded Farmhouse pizza (id: 1). Its sizes are R at 19900 paise, M at 39900 paise and L at 59900 paise. Its Crust group (id: 1, min_select: 1, max_select: 1) contains Classic hand tossed (id: 1, in stock, prices {"*": 0}) and Cheese burst (id: 2, in stock, prices {"M": 9900, "L": 12900}). Its Toppings group (id: 2, min_select: 0, max_select: 5) contains Olives (id: 3, {"*": 3500}), Jalapeno (id: 4, {"*": 3500}), Paneer (id: 5, {"*": 6000}), Mushroom (id: 6, {"*": 4500}) and Red paprika (id: 7, {"*": 4000}), all in stock.
  • Items are ordered by sort ascending, then id ascending. Sizes are ordered by price ascending. Groups and their options are ordered by id ascending, with every size, group, option and price attached to its correct parent.
  • A menu containing 6 items, 18 groups and 36 options is returned with its complete nested data using at most 5 SQL statements.
  • An active restaurant without menu items returns HTTP 200 with restaurant_id and items: [], not items: null.
  • A missing or inactive restaurant returns HTTP 404 with error code restaurant_not_found. This also applies when an inactive restaurant has menu items.
  • An ID that is not a positive integer returns HTTP 400 with error code invalid_parameter and field id. The tested invalid IDs are abc, 0, -4 and 1.5.

Constraints

  • Use at most 5 SQL statements for a menu request, regardless of the number of items, groups or options. The test exercises this with 6 items, 18 groups and 36 options.

Design direction

A layout that works well is to keep menu retrieval and response assembly alongside the existing restaurant service in backend/internal/restaurant/service.go. Keep path parsing, service calls and HTTP error mapping in backend/internal/restaurant/handler.go. Register the public route in backend/cmd/api/main.go. Any layout passes the tests as long as the HTTP contract and observable behaviour remain correct.

Batch-load one level at a time using the IDs collected from its parents. Maps from parent IDs to positions in the response slices make it straightforward to attach each child without repeatedly scanning the response. Store indexes rather than pointers, and append records in SQL result order rather than constructing the response by iterating over maps.

Think about these trade-offs:

  • Batching by IDs uses a fixed number of database round trips instead of allowing query count to grow with the number of parents.
  • Index maps make parent lookups efficient, while preserving SQL ordering keeps the returned menu deterministic.
  • Checking restaurant existence only when no active items are returned avoids an unnecessary lookup on populated menus. Make sure an inactive restaurant with existing items is not treated as a valid menu. An active restaurant with no items must still return an empty array.

How to check your work

Run make up, then run make test W=W2B5. A passing run reports successful W2B5 tests; a failure identifies the assertion and the actual result that did not match the expected behaviour.

Finished Exercise 2.12?

Response construction

2 items

  • 2.11N+1 queries and how to batch themnot completed
  • 2.12GET /v1/restaurants/{id}/menu in five queriesnot completed

Menu pricing endpoint

Up next · Module 5 · 2 items · about 2 h

Start module 5

Implementation

Run locally

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

  1. Register the public menu endpoint.
  2. Return the complete nested menu with correct parent relationships.
  3. Preserve the ordering specified by the contract.
  4. Load populated menus using at most five SQL statements.
  5. Return the correct responses for empty, missing, inactive and invalid-ID cases.

Commands

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

Tests

  1. The seeded restaurant returns the Farmhouse pizza with its exact sizes, groups, options, stock states and prices nested under the correct parents.

    GET/v1/restaurants/1/menu
    Response 200 OK
    {
      "restaurant_id": 1,
      "items": [
        {
          "id": 1,
          "name": "Farmhouse pizza",
          "sizes": [ { "size": "R", "price_paise": 19900 }, … ],
          "groups": [
            {
              "id": 1, "name": "Crust", "min_select": 1, "max_select": 1,
              "options": [ { "id": 1, "name": "Classic hand tossed", "in_stock": true, "prices": { "*": 0 } }, … ]
            },
            …
          ]
        }
      ]
    }

    Input GET /v1/restaurants/1/menu against the seeded database.

    Expected HTTP 200, restaurant_id: 1. Item 1 is Farmhouse pizza, with sizes R: 19900, M: 39900 and L: 59900. Group 1 is Crust with selection limits 1 and 1: option 1 Classic hand tossed is in stock with prices {"*": 0}, and option 2 Cheese burst is in stock with prices {"M": 9900, "L": 12900}. Group 2 is Toppings with selection limits 0 and 5: option 3 Olives has {"*": 3500}, option 4 Jalapeno has {"*": 3500}, option 5 Paneer has {"*": 6000}, option 6 Mushroom has {"*": 4500}, and option 7 Red paprika has {"*": 4000}. All five options are in stock.

    TestW2B5_TheSeededMenuIsNestedSizesGroupsOptionsAndPrices in backend/e2e/w2_menu_test.go