Skip to content

Commit

Permalink
Fix CoffeeSize.
Browse files Browse the repository at this point in the history
어떤 유저가 CoffeeSize 앞글자를 대문자로 값 전송함. 내가 정의한 CoffeeSize enum case와 맞지않아서 Codable 조건 불만족. -> singleValueContainer를 통해 Container의 데이터를 받아와서 lowercased()적용시켰음
  • Loading branch information
SHcommit committed Sep 7, 2022
1 parent 6945dbf commit 7829c13
Showing 1 changed file with 32 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
// Created by 양승현 on 2022/09/03.
//

import Foundation

/**
*decodeIfPresnt nil 반환 o
*decode nil 반환 x
Expand All @@ -27,6 +29,12 @@ enum CoffeeSize: String, Codable, CaseIterable {
case small
case medium
case large
init(from decoder: Decoder) throws {
let value = try decoder.singleValueContainer().decode(String.self)
let lowercaseLabel = value.lowercased()
self = CoffeeSize(rawValue: lowercaseLabel) ?? .small

}
}

struct Order: Codable {
Expand Down Expand Up @@ -66,3 +74,27 @@ extension Order {
self.size = selectedSize
}
}


extension Order {
// 유저가 선택한 거 한 개 주문 POST로 전송
static func create(vm: AddCoffeeOrderViewModel) -> Resource<Order?> {
let url :String = "https://warp-wiry-rugby.glitch.me/orders"
let order = Order(vm)

guard let data = try? JSONEncoder().encode(order) else {
fatalError("Error encoding order")
}

var resource = Resource<Order?>(url: url)
resource.httpMethod = .post
resource.body = data

return resource
}

static var all: Resource<[Order]> = {
let url :String = "https://warp-wiry-rugby.glitch.me/orders"
return Resource<[Order]>(url: url)
}()
}

0 comments on commit 7829c13

Please sign in to comment.