轨道
/
Ballerina
Ballerina
/
练习
/
蛋糕是个谎言
蛋糕是个谎言

蛋糕是个谎言

中等

说明

问题描述

CakeStation 是一家很受欢迎的烘焙店,制作各式各样的蛋糕。他们想请你为店里搭建一套在线订单管理系统。

菜单中各项商品的详细信息如下。

商品 价格
Butter Cake 15
Chocolate Cake 20
Tres Leches 25

系统需要支持以下操作。

  • 获取菜单。
  • 下单。
  • 查询订单状态(状态为 pending、in progress 或 completed 之一)。
  • 更新订单。仅当状态仍为 pending 时才允许。
  • 删除订单。仅当状态仍为 pending 时才允许。

约束条件

  1. 获取菜单

    • 方法 - GET
    • 路径 - /menu
    • 响应
      • 200 OK
      • content-type - application/json
      • 一个 JSON 对象,以商品名称为键,价格为值(整数)
        {
           "Butter Cake": 15,
           "Chocolate Cake": 20,
           "Tres Leches": 25
        }
        
  2. 下单

    • 方法 - POST

    • 路径 - /order

    • 请求

      • 请求负载是一个 JSON 对象,包含以下字段
        • 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"
            ]
        }
        
    • 响应

      • 400 Bad Request,如果
        • 负载与预期格式不匹配,或者
        • username 为空,或者
        • order_items 数组为空,或者
        • order_items 数组中,同一种蛋糕对应了不止一个 JSON 对象,或者
        • order_items 数组中某个成员的 item 字段所表示的蛋糕不在菜单上,或者
        • order_items 数组中某个成员的 quantity 字段的值小于 1
      • 201 Created,如果下单成功
        • content-type - application/json
        • 一个包含两个字段的 JSON 对象
          • order_id,包含随机生成的订单 ID 字符串。它应当是某个有效数值的字符串表示。
          • total 字段,包含订单的总金额(整数),即每种蛋糕的价格 × 数量之和
          {
              "type": "object",
              "properties": {
                  "order_id": {
                      "type": "string"
                  },
                  "total": {
                      "type": "integer"
                  }
              },
              "required": [
                  "order_id",
                  "total"
              ]
          }
          
  3. 查询订单状态

    • 方法 - GET
    • 路径 - /order/{orderId}
    • 响应
      • 404 Not Found,如果不存在指定 orderId 的订单
      • 200 OK
        • 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"
              ]
          }
          
  4. 更新订单。仅当状态仍为 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"
            ]
        }
        
    • 响应

      • 404 Not Found,如果不存在指定 orderId 的订单
      • 403 Forbidden,如果订单状态不是 pending(即 in progress 或 completed)
      • 400 Bad Request,如果 order_items 数组不满足下单(第 2 条)所要求的相同条件
      • 200 OK,如果更新成功
        • content-type - application/json

        • 一个包含两个字段的 JSON 对象

          • order_id,包含与原订单相同的订单 ID 字符串
          • total 字段,包含订单的总金额(即每种蛋糕的价格 × 数量之和)
          {
              "type": "object",
              "properties": {
                  "order_id": {
                      "type": "string"
                  },
                  "total": {
                      "type": "integer"
                  }
              },
              "required": [
                  "order_id",
                  "total"
              ]
          }
          

      注意:如果更新失败,不应改动现有的订单。

  5. 删除订单。仅当状态仍为 pending 时才允许。

    • 方法 - DELETE
    • 路径 - /order/{orderId}
    • 响应
      • 404 Not Found,如果不存在指定 orderId 的订单
      • 403 Forbidden,如果订单状态不是 pending(即 in progress 或 completed)
      • 200 OK,如果删除成功
  6. 生成的订单 ID 应当是唯一的,应当是字符串,并且应当是某个有效数字的字符串表示(例如 "1"、"1234")。

定义

  • 使用一个默认值为 8080 的 configurable 变量 port,来指定服务监听的端口。
  • 使用模块级的 orderStatus 映射来维护订单状态,其中以随机生成的订单 ID 字符串为键,值为 "pending"、"in progress" 或 "completed"。
    • 键 - 随机生成的订单 ID 字符串
    • 值 - 订单状态("pending"、"in progress" 或 "completed")
  • 每来一个新订单,就向 orderStatus 映射中写入值 "pending"。

示例 1

获取菜单。

$ curl -v http://localhost:8080/menu

< HTTP/1.1 200 OK
< content-type: application/json
{"Butter Cake":15, "Chocolate Cake":20, "Tres Leches":25}

示例 2

使用有效的负载尝试下单。

$ 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” 应当(也将会)是随机生成的唯一字符串。

示例 3

使用与预期格式不匹配的负载尝试下单。

$ 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

提示

通过 GitHub 编辑 链接将在新窗口或新标签页中打开
Ballerina Exercism

准备好开始 蛋糕是个谎言 了吗?

注册 Exercism,借助 60 个练习 和真人导师指导,学习并掌握 Ballerina,全部免费。