Last active
November 5, 2020 13:09
-
-
Save profburke/2e951f48542a9a1ff47051572d77584c to your computer and use it in GitHub Desktop.
Files for Serverless Swift article
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public enum Style: String, Codable { | |
case croissant | |
case naan | |
case pumpernickel | |
case rye | |
} | |
public struct Item: Codable { | |
public let amount: Int | |
public let style: Style | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Foundation | |
import BreadsRUsModels | |
let inputData = FileHandle.standardInput.readDataToEndOfFile() | |
let decoder = JSONDecoder() | |
func jsonify(response: String, payload: String) -> String { | |
let result = "{ \"response\" : \"\(response)\", \"payload\" : \"\(payload)\"}" | |
return result | |
} | |
do { | |
let order = try decoder.decode(Order.self, from: inputData) | |
let receipt = order.receipt() | |
print(jsonify(response: "success", payload: receipt.description)) | |
} catch { | |
print(jsonify(response: "error", payload: "In a real app, this would have useful information.")) | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public struct Order: Codable { | |
public private(set) var items: [Item] | |
public init() { | |
items = [] | |
} | |
public mutating func add(item: Item) { | |
items.append(item) | |
} | |
public func receipt() -> Receipt { | |
return Receipt.receipt(for: items) | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// swift-tools-version:4.0 | |
import PackageDescription | |
let package = Package( | |
name: "BreadsRUs", | |
dependencies: [ | |
], | |
targets: [ | |
.target( | |
name: "BreadsRUsModels", | |
dependencies: []), | |
.target( | |
name: "app", | |
dependencies: ["BreadsRUsModels"]), | |
] | |
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Foundation | |
// Dummy. Replace with some sort of database access or similar | |
// to fetch the current price. | |
func Price(for style: Style) -> Double { | |
switch style { | |
case .croissant: | |
return 1.23 | |
case .naan: | |
return 0.87 | |
case .pumpernickel: | |
return 0.64 | |
case .rye: | |
return 0.62 | |
} | |
} | |
public struct Receipt: Codable, CustomStringConvertible { | |
public private(set) var items: [Item] | |
public let orderDate: Date | |
func lineItem(for item: Item) -> Double { | |
return Double(item.amount) * Price(for: item.style) | |
} | |
var total: Double { | |
return items.reduce(0.0) { return $0 + lineItem(for: $1) } | |
} | |
public var description: String { | |
func line(for item: Item) -> String { | |
return "\(item.amount) \(item.style.rawValue.uppercased()) @ \(Price(for: item.style)) = \(lineItem(for: item))" | |
} | |
var result = "Receipt for Order on \(orderDate)\n" | |
result += "---------\n" | |
_ = items.map { result += line(for: $0) + "\n"} | |
result += "---------\n" | |
result += "Total: \(total)\n\n" | |
return result | |
} | |
public init(with items: [Item] = []) { | |
self.items = items | |
orderDate = Date() | |
} | |
public mutating func add(item: Item) { | |
items.append(item) | |
} | |
static func receipt(for items: [Item]) -> Receipt { | |
return Receipt(with: items) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment