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 物件會剛好包含兩個欄位:一個字串 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 欄位的值小於 1application/json
order_id,內含隨機產生的訂單 ID 字串。這應該是某個有效數值的字串表示。total 欄位,內含訂單的總金額(整數),也就是每種蛋糕的價格乘以數量後的總和{
"type": "object",
"properties": {
"order_id": {
"type": "string"
},
"total": {
"type": "integer"
}
},
"required": [
"order_id",
"total"
]
}
取得訂單狀態
GET
/order/{orderId}
orderId 的訂單content-type - application/json
一個包含兩個字串欄位 order_id 和 status 的 JSON 物件,分別代表訂單 ID 與狀態(pending、in progress 或 completed 其中之一)
{
"type": "object",
"properties": {
"order_id": {
"type": "string"
},
"status": {
"type": "string"
}
},
"required": [
"order_id",
"status"
]
}
更新訂單。只有在狀態仍是 pending 時才允許。
方法 - PUT
路徑 - /order/{orderId}
請求
請求的 payload 會是一個只含單一欄位 order_items 的 JSON 物件。
order_items - 一個代表訂單品項的 JSON 物件陣列。每個 JSON 物件會剛好包含兩個欄位:一個字串 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 的訂單pending(也就是 in progress 或 completed)order_items 陣列不符合下訂單(2)的相同要求content-type - application/json
一個包含兩個欄位的 JSON 物件
order_id,內含與原始訂單相同的訂單 ID 字串total 欄位,內含訂單的總金額(也就是每種蛋糕的價格乘以數量後的總和){
"type": "object",
"properties": {
"order_id": {
"type": "string"
},
"total": {
"type": "integer"
}
},
"required": [
"order_id",
"total"
]
}
注意:如果更新嘗試失敗,不應該對現有訂單做任何變更。
刪除訂單。只有在狀態仍是 pending 時才允許。
DELETE
/order/{orderId}
orderId 的訂單pending(也就是 in progress 或 completed)產生的訂單 ID 應該獨一無二、應為字串,且應為某個有效數字的字串表示(例如 "1"、"1234")。
configurable 變數 port,預設值為 8080,用來指定服務所監聽的連接埠。orderStatus map 來維護各訂單的狀態,其中以隨機產生的訂單 ID 字串對應到的值是 "pending"、"in progress" 或 "completed"。
"pending"、"in progress" 或 "completed")"pending" 更新 orderStatus map。取得菜單。
$ curl -v http://localhost:8080/menu
< HTTP/1.1 200 OK
< content-type: application/json
{"Butter Cake":15, "Chocolate Cake":20, "Tres Leches":25}
嘗試以有效的 payload 下訂單。
$ 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" 應該/將會是隨機產生的唯一字串。
嘗試以不符合預期格式的 payload 下訂單。
$ 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(它是一個關鍵字)當作路徑使用。