StaffCoder
.NET
.NET.NET 10
12 modules · 48 challengesSign up
Module 4 · Model Binding and Validation

Order Submission Endpoint

easy100%1 of 1 solved

Problem statement

Use everything from this module in one endpoint: bind a route id, a validated JSON body, a required header and an optional query value, and answer bad input with validation problem details naming each failing field.

Requirements

The order store, GET /orders and the CreateOrder helper are already provided. Your task has two parts: add validation attributes in OrderIntakeApi/Models/OrderSubmission.cs, and bind and check the request in SubmitOrder in OrderIntakeApi/Controllers/OrdersController.cs, answering with CreateOrder(customerId, idempotencyKey, currency, submission).

Binding and validation failures are recorded in ModelState. In this app the automatic 400 is switched off, so the action decides what to answer.

CreateOrder (provided)

  • It answers 404 for an unknown customer, or stores the order and answers 201 with it and a Location such as /orders/1

OrderSubmission

  • Sku is required and has from 4 to 12 characters
  • Quantity is from 1 to 100
  • UnitPrice is from 0.01 to 10000

OrdersController

  • customerId comes from the route and is a whole number of at least 1, so 0 and abc are rejected
  • idempotencyKey comes from the Idempotency-Key header, is required and has from 8 to 64 characters. A key sent in the query string instead is not used
  • currency comes from the query string and is optional: when it is left out, it is USD
  • submission is the JSON body. A body that cannot be read, such as cut-off JSON or quantity given as "two", is rejected
  • When ModelState is not valid, the action answers 400 with validation problem details (application/problem+json), whose errors name each failing field, and nothing is stored
Sample Requests & Responses

POST /customers/7/orders?currency=EUR with the header Idempotency-Key: order-key-0001

Request body:

{ "sku": "SKU-1001", "quantity": 2, "unitPrice": 19.99 }

Response (201 Created), with the header Location: /orders/1:

{ "orderId": 1, "customerId": 7, "sku": "SKU-1001", "quantity": 2, "unitPrice": 19.99, "idempotencyKey": "order-key-0001", "currency": "EUR" }

POST /customers/0/orders with no header and the body { "sku": "X", "quantity": 0, "unitPrice": 19.99 }

Response (400 Bad Request), as application/problem+json:

{
  "type": "https://tools.ietf.org/html/rfc9110#section-15.5.1",
  "title": "One or more validation errors occurred.",
  "status": 400,
  "errors": {
    "Sku": ["The field Sku must be a string with a minimum length of 4 and a maximum length of 12."],
    "Quantity": ["The field Quantity must be between 1 and 100."],
    "customerId": ["The field customerId must be between 1 and 2147483647."],
    "Idempotency-Key": ["A value for the 'Idempotency-Key' parameter or property was not provided."]
  }
}

More in Model Binding and Validation

See the module
Registration Payload BindingThis problem covers binding a JSON request body to a typed record in a minimal API, so a body that cannot be read is answered with 400 before the handler runs.100%1 of 1 solvedeasyAnnotated Product ValidationThis problem covers adding validation attributes to a model and answering an invalid one with validation problem details that name every failing field.100%1 of 1 solvedeasyHeader And Query Metadata BindingThis problem covers binding a required value from a request header and an optional value with a default from the query string straight into an action's parameters.100%1 of 1 solvedeasy

We use cookies that are necessary to run Staffcoder, and optional analytics cookies to see which pages get used. Analytics stays off unless you accept. Read our Cookie Policy.