學習軌道
/
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

    • 請求

      • 請求的 payload 會是一個包含下列欄位的 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,若
        • payload 不符合預期格式,或
        • 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}

    • 請求

      • 請求的 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"
            ]
        }
        
    • 回應

      • 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")。

定義

  • 使用一個 configurable 變數 port,預設值為 8080,用來指定服務所監聽的連接埠。
  • 使用一個模組層級的 orderStatus map 來維護各訂單的狀態,其中以隨機產生的訂單 ID 字串對應到的值是 "pending"、"in progress" 或 "completed"。
    • 鍵 - 隨機產生的訂單 ID 字串
    • 值 - 訂單狀態("pending"、"in progress" 或 "completed")
  • 為每一筆新訂單,以值 "pending" 更新 orderStatus map。

範例 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

嘗試以有效的 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" 應該/將會是隨機產生的唯一字串。

範例 3

嘗試以不符合預期格式的 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

提示

透過 GitHub 編輯 連結會在新視窗或分頁中開啟
Ballerina Exercism

準備好開始 蛋糕是個謊言 了嗎?

註冊 Exercism,透過 60 個練習 和真人引導來學習並精通 Ballerina,全部免費。