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}
请求
请求负载是一个 JSON 对象,只包含一个字段 order_items。
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")。
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(它是一个关键字)用作路径。