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
The full menu of one restaurant
idintegerrequiredat least 1
restaurant_idintegerrequireditemsMenuItem[]requiredidintegerrequirednamestringrequiredsizesobject[]requiredsizestringrequiredprice_paiseintegerrequired
groupsobject[]requiredidintegerrequirednamestringrequiredmin_selectintegerrequiredmax_selectintegerrequiredoptionsobject[]requiredidintegerrequirednamestringrequiredin_stockbooleanrequiredpricesmap of integerrequiredSize to price in paise. Key * means every size.
invalid_parameter
restaurant_not_found
Acceptance criteria
- A public
GET /v1/restaurants/1/menureturns HTTP 200 withrestaurant_id: 1and 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
sortascending, thenidascending. Sizes are ordered by price ascending. Groups and their options are ordered byidascending, 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_idanditems: [], notitems: 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_parameterand fieldid. The tested invalid IDs areabc,0,-4and1.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.
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.
Response construction
2 items
- 2.11N+1 queries and how to batch themnot completed
- 2.12GET /v1/restaurants/{id}/menu in five queriesnot completed
Up next · Module 5 · 2 items · about 2 h
Implementation
Run locally
Write the code in your editor inside zomato-prep, then work through this list.
- Register the public menu endpoint.
- Return the complete nested menu with correct parent relationships.
- Preserve the ordering specified by the contract.
- Load populated menus using at most five SQL statements.
- Return the correct responses for empty, missing, inactive and invalid-ID cases.
Commands
cd backend && make upcd backend && make test W=W2B5Tests
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/menuResponse 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/menuagainst the seeded database.Expected HTTP 200,
restaurant_id: 1. Item 1 isFarmhouse pizza, with sizes R: 19900, M: 39900 and L: 59900. Group 1 isCrustwith selection limits 1 and 1: option 1Classic hand tossedis in stock with prices{"*": 0}, and option 2Cheese burstis in stock with prices{"M": 9900, "L": 12900}. Group 2 isToppingswith selection limits 0 and 5: option 3Oliveshas{"*": 3500}, option 4Jalapenohas{"*": 3500}, option 5Paneerhas{"*": 6000}, option 6Mushroomhas{"*": 4500}, and option 7Red paprikahas{"*": 4000}. All five options are in stock.TestW2B5_TheSeededMenuIsNestedSizesGroupsOptionsAndPricesinbackend/e2e/w2_menu_test.go