diff --git a/Sources/App/Pass.swift b/Sources/App/Pass/Pass.swift similarity index 60% rename from Sources/App/Pass.swift rename to Sources/App/Pass/Pass.swift index 28a8bfe..f57183e 100644 --- a/Sources/App/Pass.swift +++ b/Sources/App/Pass/Pass.swift @@ -8,6 +8,9 @@ struct Pass: ResponseEncodable { return encoder }() + + static let dateFormatStyle = Date.ISO8601FormatStyle.iso8601 + let description: String let formatVersion = 1 let organizationName = "Cocoatype, LLC" @@ -22,33 +25,19 @@ struct Pass: ResponseEncodable { let barcodes: [Pass.Barcode] let storeCard = Pass.StoreCard() + let locations: [Pass.Location] + let relevantDate: String? init(_ request: PassRequest) { self.description = request.title self.logoText = request.title self.barcodes = [Pass.Barcode(request.barcode)] - } -} - -extension Pass { - struct StoreCard: ResponseEncodable {} -} - -extension Pass { - struct Barcode: ResponseEncodable { - let format: String - let message: String - let messageEncoding = "utf-8" + self.locations = request.locations.map(Pass.Location.init) - init(_ request: PassRequest.Barcode) { - switch request { - case .qr(let message): - self.format = "PKBarcodeFormatQR" - self.message = message - case .code128(let message): - self.format = "PKBarcodeFormatCode128" - self.message = message - } - } + self.relevantDate = try? request.dates + .compactMap(Self.dateFormatStyle.parse(_:)) + .sorted() + .first + .map(Self.dateFormatStyle.format(_:)) } } diff --git a/Sources/App/Pass/PassBarcode.swift b/Sources/App/Pass/PassBarcode.swift new file mode 100644 index 0000000..7af7b23 --- /dev/null +++ b/Sources/App/Pass/PassBarcode.swift @@ -0,0 +1,20 @@ +import Hummingbird + +extension Pass { + struct Barcode: ResponseEncodable { + let format: String + let message: String + let messageEncoding = "utf-8" + + init(_ request: PassRequest.Barcode) { + switch request { + case .qr(let message): + self.format = "PKBarcodeFormatQR" + self.message = message + case .code128(let message): + self.format = "PKBarcodeFormatCode128" + self.message = message + } + } + } +} diff --git a/Sources/App/Pass/PassLocation.swift b/Sources/App/Pass/PassLocation.swift new file mode 100644 index 0000000..8e03779 --- /dev/null +++ b/Sources/App/Pass/PassLocation.swift @@ -0,0 +1,15 @@ +import Hummingbird + +extension Pass { + struct Location: ResponseEncodable { + let latitude: Double + let longitude: Double + let relevantText: String? + + init(requestLocation: PassRequest.Location) { + self.latitude = requestLocation.latitude + self.longitude = requestLocation.longitude + self.relevantText = requestLocation.name + } + } +} diff --git a/Sources/App/Pass/PassStoreCard.swift b/Sources/App/Pass/PassStoreCard.swift new file mode 100644 index 0000000..253d35a --- /dev/null +++ b/Sources/App/Pass/PassStoreCard.swift @@ -0,0 +1,5 @@ +import Hummingbird + +extension Pass { + struct StoreCard: ResponseEncodable {} +} diff --git a/Sources/App/Request/PassRequest.swift b/Sources/App/Request/PassRequest.swift new file mode 100644 index 0000000..ba92655 --- /dev/null +++ b/Sources/App/Request/PassRequest.swift @@ -0,0 +1,29 @@ +import Foundation + +struct PassRequest: Decodable { + let title: String + let barcode: PassRequest.Barcode + let locations: [PassRequest.Location] + let dates: [String] +} + +enum PassRequestDecodeError: Error { + case unknownFormat(String) +} + +/* Barc Supports + * EAN-13 + * UPC-A + * Code 39 + * Codabar + */ + +/* Both Support + * QR + * Code 128 + */ + +/* Apple Wallet Supports + * Aztec + * PDF417 + */ diff --git a/Sources/App/PassRequest.swift b/Sources/App/Request/PassRequestBarcode.swift similarity index 71% rename from Sources/App/PassRequest.swift rename to Sources/App/Request/PassRequestBarcode.swift index 1b98961..4e0e3d5 100644 --- a/Sources/App/PassRequest.swift +++ b/Sources/App/Request/PassRequestBarcode.swift @@ -1,9 +1,4 @@ -import Foundation - -struct PassRequest: Decodable { - let title: String - let barcode: Barcode - +extension PassRequest { enum Barcode: Decodable { case qr(String) case code128(String) @@ -28,24 +23,3 @@ struct PassRequest: Decodable { } } } - -enum PassRequestDecodeError: Error { - case unknownFormat(String) -} - -/* Barc Supports - * EAN-13 - * UPC-A - * Code 39 - * Codabar - */ - -/* Both Support - * QR - * Code 128 - */ - -/* Apple Wallet Supports - * Aztec - * PDF417 - */ diff --git a/Sources/App/Request/PassRequestLocation.swift b/Sources/App/Request/PassRequestLocation.swift new file mode 100644 index 0000000..b6584a2 --- /dev/null +++ b/Sources/App/Request/PassRequestLocation.swift @@ -0,0 +1,7 @@ +extension PassRequest { + struct Location: Decodable { + let latitude: Double + let longitude: Double + let name: String? + } +}