いろいろなケーキを焼く人気のベーカリー、CakeStationから、オンライン注文管理システムの構築を頼まれました。
メニューに載っている商品の詳細は次のとおりです。
| 商品 | 価格 |
|---|---|
| Butter Cake | 15 |
| Chocolate Cake | 20 |
| Tres Leches | 25 |
次のことができるようにします。
pending、in progress、completedのいずれか)を取得する。pendingのときだけ可能です。pendingのときだけ可能です。メニューの取得
GET
/menu
application/json
{
"Butter Cake": 15,
"Chocolate Cake": 20,
"Tres Leches": 25
}
注文
メソッド - POST
パス - /order
リクエスト
username - ユーザー名(string)order_items - 注文商品を表すJSONオブジェクトの配列。各JSONオブジェクトはちょうど2つのフィールドを持ちます。商品名を表す文字列のitemフィールドと、その商品の注文数量を表す整数のquantityフィールドです。{
"type": "object",
"properties": {
"username": {
"type": "string"
},
"order_items": {
"type": "array",
"items": {
"type": "object",
"properties": {
"item": {
"type": "string"
},
"quantity": {
"type": "integer"
}
},
"required": [
"item",
"quantity"
]
}
}
},
"required": [
"username",
"order_items"
]
}
レスポンス
usernameが空である、またはorder_items配列が空である、またはorder_items配列に、同じ種類のケーキについて複数のJSONオブジェクトが含まれている、またはorder_items配列の要素が持つitemフィールドのケーキの種類がメニューにない、またはorder_items配列の要素が持つquantityフィールドの値が1未満であるapplication/json
order_id。これは有効な数値の文字列表現である必要があります。totalフィールド(つまり、ケーキの種類ごとの価格×数量の合計){
"type": "object",
"properties": {
"order_id": {
"type": "string"
},
"total": {
"type": "integer"
}
},
"required": [
"order_id",
"total"
]
}
注文ステータスの取得
GET
/order/{orderId}
orderIdの注文が存在しない場合は404 Not Foundcontent-type - application/json
注文IDとステータス(pending、in progress、completedのいずれか)をそれぞれ表す、2つの文字列フィールドorder_idとstatusを持つJSONオブジェクト
{
"type": "object",
"properties": {
"order_id": {
"type": "string"
},
"status": {
"type": "string"
}
},
"required": [
"order_id",
"status"
]
}
注文の更新。ステータスがまだpendingのときだけ可能です。
メソッド - PUT
パス - /order/{orderId}
リクエスト
リクエストのペイロードは、単一のフィールドorder_itemsを持つJSONオブジェクトです。
order_items - 注文商品を表すJSONオブジェクトの配列。各JSONオブジェクトはちょうど2つのフィールドを持ちます。商品名を表す文字列のitemフィールドと、その商品の注文数量を表す整数のquantityフィールドです。{
"type": "object",
"properties": {
"order_items": {
"type": "array",
"items": {
"type": "object",
"properties": {
"item": {
"type": "string"
},
"quantity": {
"type": "integer"
}
},
"required": [
"item",
"quantity"
]
}
}
},
"required": [
"order_items"
]
}
レスポンス
orderIdの注文が存在しない場合は404 Not Foundpendingでない場合(つまりin progressまたはcompleted)は403 Forbiddenorder_items配列が、注文時(2)と同じ要件を満たしていない場合は400 Bad Requestcontent-type - application/json
2つのフィールドを持つJSONオブジェクト
order_id
totalフィールド(つまり、ケーキの種類ごとの価格×数量の合計){
"type": "object",
"properties": {
"order_id": {
"type": "string"
},
"total": {
"type": "integer"
}
},
"required": [
"order_id",
"total"
]
}
注意: 更新に失敗した場合、既存の注文には何も変更を加えないでください。
注文の削除。ステータスがまだpendingのときだけ可能です。
DELETE
/order/{orderId}
orderIdの注文が存在しない場合は404 Not Foundpendingでない場合(つまりin progressまたはcompleted)は403 Forbidden生成される注文IDは一意で、文字列であり、かつ有効な数値の文字列表現である必要があります(例:"1"、"1234")。
8080のconfigurable変数portを使います。orderStatusマップを使います。このマップでは、ランダムに生成された注文ID文字列に対して、値が"pending"、"in progress"、"completed"のいずれかになります。
"pending"、"in progress"、"completed"のいずれか)orderStatusマップを値"pending"で更新します。メニューを取得します。
$ curl -v http://localhost:8080/menu
< HTTP/1.1 200 OK
< content-type: application/json
{"Butter Cake":15, "Chocolate Cake":20, "Tres Leches":25}
有効なペイロードで注文しようとする場合。
$ curl -v http://localhost:8080/order -d '{"username": "mary", "order_items": [{"item": "Tres Leches", "quantity": 1}, {"item": "Chocolate Cake", "quantity": 2}]}' -H 'Content-Type: application/json'
< HTTP/1.1 201 Created
< content-type: application/json
{"order_id":"7", "total":65}
注意: "order_id"は、ランダムに生成される一意な文字列である必要があります(そしてそのようになります)。
期待される形式と一致しないペイロードで注文しようとする場合。
$ curl -v http://localhost:8080/order -d '{"username": "mary", "order_items": [{"Tres Leches": 1}, {"Chocolate Cake": 2}]}' -H 'Content-Type: application/json'
< HTTP/1.1 400 Bad Request
'order)を使うと、キーワードであるorderをパスとして使えます。