#8 用RESTful API來訂杯珍奶喝吧!Part-5-訂單修改及刪除

Tai
彼得潘的 Swift iOS / Flutter App 開發教室
8 min readNov 17, 2022

點完飲料才發現冰塊、甜度、杯數不對,或是根本選錯取餐分店,想要改一下訂的內容,或是低頭看見走樣的身材,覺得良心不安後悔不想喝了~就默默的按一下訂單,進入修改刪除的頁面處理吧~

先來看看修改訂單是如何建置出來的:

  1. 在Storyboard中設定好修改訂單頁面,並將訂單列表的Cell連線到訂單修改頁面,訂單資訊透過Segue傳送到此頁面顯示

2. 設定好上述的事情後,接著就是解析要做為上傳修改的JSON,並透過RESTful API修改Airtable上OrderList的訂單內容,那要怎麼知道要修改哪筆資料呢?
在Airtable創建每筆record時,每筆record帶有一個唯一的id,在上頁訂單列表時就透過API一起下載,Segue一併將id傳到修改頁,在執行修改時帶入id就可以找到要修改的訂單record

上頁訂單列表的JSON型別:

      import Foundation

struct OrderInfo: Codable {
var id: String //每筆record創建時同時產生的唯一 id,隨Segue傳到下一頁
var createdTime: Date
var fields: Fields

struct Fields: Codable {
var name: String
var price: Double
var iceDegree: String
var sugarDegree: String
var cupAmount: Int
var comment: String
var userName: String
var userPhone: String
var totalAmount: Int
}
}

上傳修改的JSON型別及程式碼:

      //JSON解析型別
import Foundation

struct UpdateOrderInfo: Codable {
var fields: Fields

struct Fields: Codable {
var name: String
var price: Int
var iceDegree: String
var sugarDegree: String
var cupAmount: Int
var comment: String
var userName: String
var userPhone: String
var totalAmount: Int
var updateTime: String
var storeName: String
}
}

//修改函數
func fetchUpdateOrderInfo(name: String, price: Int, iceDegree: String, sugarDegree: String, cupAmount: Int, comment: String, userName: String, userPhone: String, updateTime: String, id: String, storeName: String) {

//計算總金額
let totalAmount = price * cupAmount

//API最後代入的id即為每筆record的唯一識別碼,有它才可以正確的修改到對的record
let url = URL(string: "https://api.airtable.com/v0/appHy2q9FOUrGGhqS/OrderList/\(id)")!
let apiKey = "Your API_KEY"
let httpHeader = "Authorization"
var request = URLRequest(url: url)
request.httpMethod = "PUT" //修改
request.setValue(apiKey, forHTTPHeaderField: httpHeader)
request.setValue("application/json", forHTTPHeaderField: "Content-Type")
let encoder = JSONEncoder()
let fieldsData = UpdateOrderInfo.Fields(name: name, price: price, iceDegree: iceDegree, sugarDegree: sugarDegree, cupAmount: cupAmount, comment: comment, userName: userName, userPhone: userPhone, totalAmount: totalAmount, updateTime: updateTime, storeName: storeName)
let uploadData = UpdateOrderInfo(fields: fieldsData)
let data = try? encoder.encode(uploadData)
request.httpBody = data
URLSession.shared.dataTask(with: request) { data, response, error in
if error == nil {
print("修改成功")
} else {
print(error ?? "")
}
}.resume()

let controller = UIAlertController(title: "修改通知", message: "己修改完成", preferredStyle: .alert)
let okAction = UIAlertAction(title: "好的", style: .default)
controller.addAction(okAction)
self.present(controller, animated: true)
}

上述設定就可以輕鬆的修改訂單內容囉~

刪除的訂單的原理同修改功能,差別在於因為沒有資料要與Airtable溝通,故不需要JSON的解析型別,僅需在API代入record的唯一 id,且request.httpMethod = “DELETE” 即可刪除

API程式碼:

      func fetchDeleteOrderInfo(id: String) {

//API最後代入的id即為每筆record的唯一識別碼,有它才可以正確的刪除到對的record
let url = URL(string: "https://api.airtable.com/v0/appHy2q9FOUrGGhqS/OrderList/\(id)")!
let apiKey = "Your API_KEY"
let httpHeader = "Authorization"
var request = URLRequest(url: url)
request.httpMethod = "DELETE" //刪除
request.setValue(apiKey, forHTTPHeaderField: httpHeader)
request.setValue("application/json", forHTTPHeaderField: "Content-Type")
URLSession.shared.dataTask(with: request) { data, response, error in
if error == nil {
print("刪除成功")
} else {
print(error ?? "")
}
}.resume()

let controller = UIAlertController(title: "刪除通知", message: "己刪除訂單", preferredStyle: .alert)
let okAction = UIAlertAction(title: "好的", style: .default)
controller.addAction(okAction)
self.present(controller, animated: true)
}

以上就輕鬆的做到修改及刪除訂單的功能了~

打完收工~訂單功的部份全部介紹完畢,下篇就是透過Google Map API取得分店資訊及使用者定位的功能,人客啊~欲知詳情~下回分解~~~

關聯文章:

#4 用RESTful API來訂杯珍奶喝吧!Part-1-畫面及功能簡介 — 重來 就從 APPLE APP 開始吧 — Medium

#5 用RESTful API來訂杯珍奶喝吧!Part-2-Web官網整合介紹 — 重來 就從 APPLE APP 開始吧 — Medium

#6 用RESTful API來訂杯珍奶喝吧!Part-3-menu呈現及banner輪播 — 重來 就從 APPLE APP 開始吧 — Medium

#7 用RESTful API來訂杯珍奶喝吧!Part-4-訂單上傳及查詢 — 重來 就從 APPLE APP 開始吧 — Medium

--

--

Tai
彼得潘的 Swift iOS / Flutter App 開發教室

跌跌撞撞走了編碼人生的前半段,一切又得重來~但勇敢站起來,決定從 IOS APP 作為下一個人生段的起點,好好的再走他一段編碼人生!