Skip to content

Commit 01bd819

Browse files
authored
Remove delegates (#18)
- Completely remove the delegates and move their methods to `PassDataModel` and `OrderDataModel` - Provide a default implementation for the model middleware of `PassDataModel` and `OrderDataModel` - Update DocC
1 parent 61371c3 commit 01bd819

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

49 files changed

+562
-933
lines changed

Package.swift

+3-4
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,14 @@ import PackageDescription
44
let package = Package(
55
name: "PassKit",
66
platforms: [
7-
.macOS(.v14)
7+
.macOS(.v13)
88
],
99
products: [
1010
.library(name: "Passes", targets: ["Passes"]),
1111
.library(name: "Orders", targets: ["Orders"]),
1212
],
1313
dependencies: [
14-
.package(url: "https://github.com/vapor/vapor.git", from: "4.106.1"),
14+
.package(url: "https://github.com/vapor/vapor.git", from: "4.108.0"),
1515
.package(url: "https://github.com/vapor/fluent.git", from: "4.12.0"),
1616
.package(url: "https://github.com/vapor/apns.git", from: "4.2.0"),
1717
.package(url: "https://github.com/vapor-community/Zip.git", from: "2.2.4"),
@@ -76,7 +76,6 @@ let package = Package(
7676

7777
var swiftSettings: [SwiftSetting] {
7878
[
79-
.enableUpcomingFeature("ExistentialAny"),
80-
.enableUpcomingFeature("FullTypedThrows"),
79+
.enableUpcomingFeature("ExistentialAny")
8180
]
8281
}

Sources/Orders/DTOs/OrderJSON.swift

-7
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,3 @@
1-
//
2-
// OrderJSON.swift
3-
// PassKit
4-
//
5-
// Created by Francesco Paolo Severino on 02/07/24.
6-
//
7-
81
/// The structure of a `order.json` file.
92
public struct OrderJSON {
103
/// A protocol that defines the structure of a `order.json` file.

Sources/Orders/DTOs/OrdersForDeviceDTO.swift

-7
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,3 @@
1-
//
2-
// OrdersForDeviceDTO.swift
3-
// PassKit
4-
//
5-
// Created by Francesco Paolo Severino on 30/06/24.
6-
//
7-
81
import Vapor
92

103
struct OrdersForDeviceDTO: Content {

Sources/Orders/Middleware/AppleOrderMiddleware.swift

-7
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,3 @@
1-
//
2-
// AppleOrderMiddleware.swift
3-
// PassKit
4-
//
5-
// Created by Francesco Paolo Severino on 30/06/24.
6-
//
7-
81
import FluentKit
92
import Vapor
103

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import FluentKit
2+
import Foundation
3+
4+
extension OrdersService: AsyncModelMiddleware {
5+
public func create(model: OD, on db: any Database, next: any AnyAsyncModelResponder) async throws {
6+
let order = Order(
7+
typeIdentifier: OD.typeIdentifier,
8+
authenticationToken: Data([UInt8].random(count: 12)).base64EncodedString()
9+
)
10+
try await order.save(on: db)
11+
model._$order.id = try order.requireID()
12+
try await next.create(model, on: db)
13+
}
14+
15+
public func update(model: OD, on db: any Database, next: any AnyAsyncModelResponder) async throws {
16+
let order = try await model._$order.get(on: db)
17+
order.updatedAt = Date.now
18+
try await order.save(on: db)
19+
try await next.update(model, on: db)
20+
try await self.sendPushNotifications(for: model, on: db)
21+
}
22+
}
23+
24+
extension OrdersServiceCustom: AsyncModelMiddleware {
25+
public func create(model: OD, on db: any Database, next: any AnyAsyncModelResponder) async throws {
26+
let order = O(
27+
typeIdentifier: OD.typeIdentifier,
28+
authenticationToken: Data([UInt8].random(count: 12)).base64EncodedString()
29+
)
30+
try await order.save(on: db)
31+
model._$order.id = try order.requireID()
32+
try await next.create(model, on: db)
33+
}
34+
35+
public func update(model: OD, on db: any Database, next: any AnyAsyncModelResponder) async throws {
36+
let order = try await model._$order.get(on: db)
37+
order.updatedAt = Date.now
38+
try await order.save(on: db)
39+
try await next.update(model, on: db)
40+
try await self.sendPushNotifications(for: model, on: db)
41+
}
42+
}

Sources/Orders/Models/Concrete Models/Order.swift

-7
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,3 @@
1-
//
2-
// Order.swift
3-
// PassKit
4-
//
5-
// Created by Francesco Paolo Severino on 30/06/24.
6-
//
7-
81
import FluentKit
92
import Foundation
103

Sources/Orders/Models/Concrete Models/OrdersDevice.swift

-7
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,3 @@
1-
//
2-
// OrdersDevice.swift
3-
// PassKit
4-
//
5-
// Created by Francesco Paolo Severino on 30/06/24.
6-
//
7-
81
import FluentKit
92
import PassKit
103

Sources/Orders/Models/Concrete Models/OrdersErrorLog.swift

-7
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,3 @@
1-
//
2-
// OrdersErrorLog.swift
3-
// PassKit
4-
//
5-
// Created by Francesco Paolo Severino on 30/06/24.
6-
//
7-
81
import FluentKit
92
import PassKit
103

Sources/Orders/Models/Concrete Models/OrdersRegistration.swift

-7
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,3 @@
1-
//
2-
// OrdersRegistration.swift
3-
// PassKit
4-
//
5-
// Created by Francesco Paolo Severino on 30/06/24.
6-
//
7-
81
import FluentKit
92

103
/// The `Model` that stores orders registrations.

Sources/Orders/Models/OrderDataModel.swift

+27-7
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,38 @@
1-
//
2-
// OrderDataModel.swift
3-
// PassKit
4-
//
5-
// Created by Francesco Paolo Severino on 30/06/24.
6-
//
7-
81
import FluentKit
92

103
/// Represents the `Model` that stores custom app data associated to Wallet orders.
114
public protocol OrderDataModel: Model {
125
associatedtype OrderType: OrderModel
136

7+
/// An identifier for the order type associated with the order.
8+
static var typeIdentifier: String { get }
9+
1410
/// The foreign key to the order table.
1511
var order: OrderType { get set }
12+
13+
/// Encode the order into JSON.
14+
///
15+
/// This method should generate the entire order JSON.
16+
///
17+
/// - Parameter db: The SQL database to query against.
18+
///
19+
/// - Returns: An object that conforms to ``OrderJSON/Properties``.
20+
///
21+
/// > Tip: See the [`Order`](https://developer.apple.com/documentation/walletorders/order) object to understand the keys.
22+
func orderJSON(on db: any Database) async throws -> any OrderJSON.Properties
23+
24+
/// Should return a URL path which points to the template data for the order.
25+
///
26+
/// The path should point to a directory containing all the images and localizations for the generated `.order` archive
27+
/// but should *not* contain any of these items:
28+
/// - `manifest.json`
29+
/// - `order.json`
30+
/// - `signature`
31+
///
32+
/// - Parameter db: The SQL database to query against.
33+
///
34+
/// - Returns: A URL path which points to the template data for the order.
35+
func template(on db: any Database) async throws -> String
1636
}
1737

1838
extension OrderDataModel {

Sources/Orders/Models/OrderModel.swift

+6-7
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,3 @@
1-
//
2-
// OrderModel.swift
3-
// PassKit
4-
//
5-
// Created by Francesco Paolo Severino on 30/06/24.
6-
//
7-
81
import FluentKit
92
import Foundation
103

@@ -23,6 +16,12 @@ public protocol OrderModel: Model where IDValue == UUID {
2316

2417
/// The authentication token supplied to your web service.
2518
var authenticationToken: String { get set }
19+
20+
/// The designated initializer.
21+
/// - Parameters:
22+
/// - typeIdentifier: The order type identifier that’s registered with Apple.
23+
/// - authenticationToken: The authentication token to use with the web service in the `webServiceURL` key.
24+
init(typeIdentifier: String, authenticationToken: String)
2625
}
2726

2827
extension OrderModel {

Sources/Orders/Models/OrdersRegistrationModel.swift

-7
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,3 @@
1-
//
2-
// OrdersRegistrationModel.swift
3-
// PassKit
4-
//
5-
// Created by Francesco Paolo Severino on 30/06/24.
6-
//
7-
81
import FluentKit
92
import PassKit
103

0 commit comments

Comments
 (0)