POST /v1/menu/price: price a configured item
You build POST /v1/menu/price to price an item, size, chosen options and quantity, returning a quote with every selection error. Invalid selections return HTTP 200 with zero prices, while malformed requests and missing items use HTTP errors.
Ticket
You build POST /v1/menu/price. It accepts an item ID, size, chosen options and quantity as JSON, then returns a quote containing description, unit_paise, total_paise and errors.
The request uses option group IDs as object keys, with each value containing the selected option IDs:
{
"item_id": 1,
"size": "M",
"chosen": {
"1": [2],
"2": [3, 5]
},
"qty": 2
}For the Farmhouse pizza fixture, this selects Cheese burst in the Crust group and Olives and Paneer in the Toppings group. The response must have a description starting with Farmhouse pizza, unit_paise of 55800, total_paise of 111600 and an empty errors array.
An existing item with an invalid selection is still a successful evaluation. Return HTTP 200 with every selection problem in errors, and both price fields set to 0. A missing item is different: return HTTP 404 with item_not_found. Week 3 uses this configurator to price orders, and the Week 4 app displays each selection error beside its option group.
Endpoint
Validate a configured item and price it
item_idintegerrequiredsizestringrequiredchosenmap of integer[]requiredOption group id, as a string, to the chosen option ids.
qtyintegerrequired1 to 20
descriptionstringrequiredHuman readable, starting with the item's name.
unit_paiseintegerrequiredtotal_paiseintegerrequirederrorsSelectionError[]requiredEvery problem with the selection, one entry each. Never null.
code"size_not_offered" | "unknown_group" | "selection_count" | "duplicate_option" | "unknown_option" | "option_unavailable"requiredsize_not_offered: the item has no price for the size. unknown_group: a chosen key is not one of the item's groups. selection_count: a group's number of chosen options is below min_select or above max_select (a group left out counts as 0). duplicate_option: an option is chosen twice in a group; reported once per repeated option. unknown_option: an option id is not in that group. option_unavailable: the option is out of stock, or has neither a price for the size nor a * price. Not reported when the size itself is not offered.
group_idintegerSet for every code except size_not_offered.
option_idintegerSet for duplicate_option, unknown_option and option_unavailable.
messagestringrequired
invalid_bodyinvalid_parameter
item_not_found
body_too_large
Acceptance criteria
- The worked example above returns HTTP 200 with
unit_paise55800,total_paise111600, an emptyerrorsarray and a description starting withFarmhouse pizza. - Only the cheapest selected option in the Toppings group (
group_id2) is free. With sizeL, Classic crust and Paneer, Mushroom and Paprika, the unit price is70400. With sizeR, Classic crust and Mushroom as the only topping, it is19900. With sizeM, Cheese burst and no toppings, it is49800. The Crust group is not discounted. - An option with a
*price is available in every size. Selecting Olives and Paneer with Classic crust returns unit prices of25900forR,45900forMand65900forL, with no selection errors. - An exact-size option price takes precedence over its
*price. When anLprice of5000paise is added for Olives, selecting Olives and Jalapeno with Classic crust in sizeLreturns64900. In sizeM, selecting Olives and Paneer with Classic crust still returns45900using the wildcard price. - Quantity multiplies the unit price. With size
Rand Classic crust, quantity3returns unit price19900and total59700; quantity20returns unit price19900and total398000. - Every applicable selection problem appears in
errors, with a non-emptymessageand the required identifiers:size_not_offeredhas nogroup_idoroption_id.unknown_groupandselection_countincludegroup_id.duplicate_option,unknown_optionandoption_unavailableinclude bothgroup_idandoption_id.- A group left out of
chosencounts as having zero selections. An option selected in the wrong group isunknown_option. Duplicate options are reported, and multiple problems in one request are all returned. - When the size itself is not offered, do not also report option availability errors.
- An out-of-stock Paneer option (
option_id5) returnsoption_unavailable, even when price records exist. The response is HTTP 200 with one selection error and both price fields set to0. - Requesting item
999999999returns HTTP 404 with error codeitem_not_found. - A body that is not exactly one valid JSON object of the expected shape returns HTTP 400 with error code
invalid_body. This includes malformed JSON, an array, unknown fields, incorrect field types, multiple JSON objects and truncated JSON. - A missing or out-of-range
item_id, an empty or missingsize, aqtyoutside1to20, or achosenkey that is not a positive integer returns HTTP 400 with error codeinvalid_parameterand the relevantfield:item_id,size,qtyorchosen. - A request body larger than 64 KiB returns HTTP 413 with error code
body_too_large.
Constraints
- Accept quantities from
1through20. - Limit request bodies to
64 KiB. Reject larger bodies with HTTP 413.
Design direction
Keep selection validation and pricing independent of the database and HTTP layers. A useful separation puts the pricing rules and selection evaluation in internal/menu/, menu data retrieval and conversion in internal/restaurant/, and JSON decoding and HTTP error mapping in the handler.
Use the existing batch queries to retrieve the item, sizes, option groups, options and option prices. Add the item lookup query and generated sqlc code needed by the pricing path. Wire the pricing service, promotion and route in cmd/api/main.go. Any layout that meets the HTTP contract and observable behaviour passes.
Consider these trade-offs:
- Validation and operational failures: An invalid selection belongs in the quote's
errorsarray. A database lookup failure is an operational error. Keep those outcomes separate so a selection problem does not become an HTTP 400 or 500. - Price and availability: Resolve an exact-size price before falling back to
*. A wildcard price allows every size, but it does not make an out-of-stock option available. - Promotion timing: Validate the complete selection before applying a promotion. Only a valid selection can produce a payable price, and the promotion must not change which selections are valid.
How to check your work
Run these commands from the repository root:
cd backend && make up
cd backend && make test W=W2B6Read the first failing assertion to identify whether the problem is price calculation, selection validation, data loading or HTTP 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.
Menu pricing endpoint
2 items
- 2.13Error classification in Gonot completed
- 2.14POST /v1/menu/price: price a configured itemnot completed
Up next · Module 6 · 3 items · about 2 h 30 min
Implementation
Run locally
Write the code in your editor inside zomato-prep, then work through this list.
- Implement
POST /v1/menu/priceto return a quote for an item, size, chosen options and quantity. - Collect every selection error with its required identifiers and a non-empty message.
- Return HTTP 200 with zero prices for invalid selections, and apply promotions only to valid selections.
- Resolve exact-size prices before wildcard prices, check stock, and make only the cheapest selected Toppings option free.
- Return the required HTTP errors for missing items, malformed requests, invalid parameters and bodies over 64 KiB.
- Verify the worked example, quantity calculations, pricing cases and selection-error cases.
Commands
cd backend && make upcd backend && make test W=W2B6Tests
The seeded Farmhouse pizza produces the expected unit price, quantity total and empty error array for the worked example.
POST/v1/menu/priceBody raw JSON{ "item_id": 1, "size": "M", "chosen": { "1": [2], "2": [3, 5] }, "qty": 2 }Response 200 OK{ "description": "Farmhouse pizza …", "unit_paise": 55800, "total_paise": 111600, "errors": [] }Input
POST /v1/menu/pricewithitem_id: 1,size: "M",chosen: {"1":[2],"2":[3,5]}andqty: 2.Expected HTTP 200;
unit_paiseis55800,total_paiseis111600,errorsis[], anddescriptionstarts withFarmhouse pizza.TestW2B6_TheWorkedExamplePricesCorrectlyinbackend/e2e/w2_price_test.go