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:

json
{
  "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

POST/v1/menu/price

Validate a configured item and price it

Request body JSON
  • item_idintegerrequired
  • sizestringrequired
  • chosenmap of integer[]required

    Option group id, as a string, to the chosen option ids.

  • qtyintegerrequired

    1 to 20

Response 200 OK
  • descriptionstringrequired

    Human readable, starting with the item's name.

  • unit_paiseintegerrequired
  • total_paiseintegerrequired
  • errorsSelectionError[]required

    Every problem with the selection, one entry each. Never null.

    • code"size_not_offered" | "unknown_group" | "selection_count" | "duplicate_option" | "unknown_option" | "option_unavailable"required

      size_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_idinteger

      Set for every code except size_not_offered.

    • option_idinteger

      Set for duplicate_option, unknown_option and option_unavailable.

    • messagestringrequired
Response 400 Bad Request

invalid_bodyinvalid_parameter

Response 404 Not Found

item_not_found

Response 413 Content Too Large

body_too_large

Acceptance criteria

  • The worked example above returns HTTP 200 with unit_paise 55800, total_paise 111600, an empty errors array and a description starting with Farmhouse pizza.
  • Only the cheapest selected option in the Toppings group (group_id 2) is free. With size L, Classic crust and Paneer, Mushroom and Paprika, the unit price is 70400. With size R, Classic crust and Mushroom as the only topping, it is 19900. With size M, Cheese burst and no toppings, it is 49800. 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 of 25900 for R, 45900 for M and 65900 for L, with no selection errors.
  • An exact-size option price takes precedence over its * price. When an L price of 5000 paise is added for Olives, selecting Olives and Jalapeno with Classic crust in size L returns 64900. In size M, selecting Olives and Paneer with Classic crust still returns 45900 using the wildcard price.
  • Quantity multiplies the unit price. With size R and Classic crust, quantity 3 returns unit price 19900 and total 59700; quantity 20 returns unit price 19900 and total 398000.
  • Every applicable selection problem appears in errors, with a non-empty message and the required identifiers:
    • size_not_offered has no group_id or option_id.
    • unknown_group and selection_count include group_id.
    • duplicate_option, unknown_option and option_unavailable include both group_id and option_id.
    • A group left out of chosen counts as having zero selections. An option selected in the wrong group is unknown_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_id 5) returns option_unavailable, even when price records exist. The response is HTTP 200 with one selection error and both price fields set to 0.
  • Requesting item 999999999 returns HTTP 404 with error code item_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 missing size, a qty outside 1 to 20, or a chosen key that is not a positive integer returns HTTP 400 with error code invalid_parameter and the relevant field: item_id, size, qty or chosen.
  • A request body larger than 64 KiB returns HTTP 413 with error code body_too_large.

Constraints

  • Accept quantities from 1 through 20.
  • 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 errors array. 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:

bash
cd backend && make up
cd backend && make test W=W2B6

Read the first failing assertion to identify whether the problem is price calculation, selection validation, data loading or HTTP behaviour.

Finished Exercise 2.14?

Menu pricing endpoint

2 items

  • 2.13Error classification in Gonot completed
  • 2.14POST /v1/menu/price: price a configured itemnot completed

Open-hours index

Up next · Module 6 · 3 items · about 2 h 30 min

Start module 6

Implementation

Run locally

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

  1. Implement POST /v1/menu/price to return a quote for an item, size, chosen options and quantity.
  2. Collect every selection error with its required identifiers and a non-empty message.
  3. Return HTTP 200 with zero prices for invalid selections, and apply promotions only to valid selections.
  4. Resolve exact-size prices before wildcard prices, check stock, and make only the cheapest selected Toppings option free.
  5. Return the required HTTP errors for missing items, malformed requests, invalid parameters and bodies over 64 KiB.
  6. Verify the worked example, quantity calculations, pricing cases and selection-error cases.

Commands

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

Tests

  1. The seeded Farmhouse pizza produces the expected unit price, quantity total and empty error array for the worked example.

    POST/v1/menu/price
    Body 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/price with item_id: 1, size: "M", chosen: {"1":[2],"2":[3,5]} and qty: 2.

    Expected HTTP 200; unit_paise is 55800, total_paise is 111600, errors is [], and description starts with Farmhouse pizza.

    TestW2B6_TheWorkedExamplePricesCorrectly in backend/e2e/w2_price_test.go