Skip to content

Commit

Permalink
Remove delegates (#18)
Browse files Browse the repository at this point in the history
- 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
  • Loading branch information
fpseverino authored Dec 17, 2024
1 parent 61371c3 commit 01bd819
Show file tree
Hide file tree
Showing 49 changed files with 562 additions and 933 deletions.
7 changes: 3 additions & 4 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ import PackageDescription
let package = Package(
name: "PassKit",
platforms: [
.macOS(.v14)
.macOS(.v13)
],
products: [
.library(name: "Passes", targets: ["Passes"]),
.library(name: "Orders", targets: ["Orders"]),
],
dependencies: [
.package(url: "https://github.com/vapor/vapor.git", from: "4.106.1"),
.package(url: "https://github.com/vapor/vapor.git", from: "4.108.0"),
.package(url: "https://github.com/vapor/fluent.git", from: "4.12.0"),
.package(url: "https://github.com/vapor/apns.git", from: "4.2.0"),
.package(url: "https://github.com/vapor-community/Zip.git", from: "2.2.4"),
Expand Down Expand Up @@ -76,7 +76,6 @@ let package = Package(

var swiftSettings: [SwiftSetting] {
[
.enableUpcomingFeature("ExistentialAny"),
.enableUpcomingFeature("FullTypedThrows"),
.enableUpcomingFeature("ExistentialAny")
]
}
7 changes: 0 additions & 7 deletions Sources/Orders/DTOs/OrderJSON.swift
Original file line number Diff line number Diff line change
@@ -1,10 +1,3 @@
//
// OrderJSON.swift
// PassKit
//
// Created by Francesco Paolo Severino on 02/07/24.
//

/// The structure of a `order.json` file.
public struct OrderJSON {
/// A protocol that defines the structure of a `order.json` file.
Expand Down
7 changes: 0 additions & 7 deletions Sources/Orders/DTOs/OrdersForDeviceDTO.swift
Original file line number Diff line number Diff line change
@@ -1,10 +1,3 @@
//
// OrdersForDeviceDTO.swift
// PassKit
//
// Created by Francesco Paolo Severino on 30/06/24.
//

import Vapor

struct OrdersForDeviceDTO: Content {
Expand Down
7 changes: 0 additions & 7 deletions Sources/Orders/Middleware/AppleOrderMiddleware.swift
Original file line number Diff line number Diff line change
@@ -1,10 +1,3 @@
//
// AppleOrderMiddleware.swift
// PassKit
//
// Created by Francesco Paolo Severino on 30/06/24.
//

import FluentKit
import Vapor

Expand Down
42 changes: 42 additions & 0 deletions Sources/Orders/Middleware/OrdersService+AsyncModelMiddleware.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import FluentKit
import Foundation

extension OrdersService: AsyncModelMiddleware {
public func create(model: OD, on db: any Database, next: any AnyAsyncModelResponder) async throws {
let order = Order(
typeIdentifier: OD.typeIdentifier,
authenticationToken: Data([UInt8].random(count: 12)).base64EncodedString()
)
try await order.save(on: db)
model._$order.id = try order.requireID()
try await next.create(model, on: db)
}

public func update(model: OD, on db: any Database, next: any AnyAsyncModelResponder) async throws {
let order = try await model._$order.get(on: db)
order.updatedAt = Date.now
try await order.save(on: db)
try await next.update(model, on: db)
try await self.sendPushNotifications(for: model, on: db)
}
}

extension OrdersServiceCustom: AsyncModelMiddleware {
public func create(model: OD, on db: any Database, next: any AnyAsyncModelResponder) async throws {
let order = O(
typeIdentifier: OD.typeIdentifier,
authenticationToken: Data([UInt8].random(count: 12)).base64EncodedString()
)
try await order.save(on: db)
model._$order.id = try order.requireID()
try await next.create(model, on: db)
}

public func update(model: OD, on db: any Database, next: any AnyAsyncModelResponder) async throws {
let order = try await model._$order.get(on: db)
order.updatedAt = Date.now
try await order.save(on: db)
try await next.update(model, on: db)
try await self.sendPushNotifications(for: model, on: db)
}
}
7 changes: 0 additions & 7 deletions Sources/Orders/Models/Concrete Models/Order.swift
Original file line number Diff line number Diff line change
@@ -1,10 +1,3 @@
//
// Order.swift
// PassKit
//
// Created by Francesco Paolo Severino on 30/06/24.
//

import FluentKit
import Foundation

Expand Down
7 changes: 0 additions & 7 deletions Sources/Orders/Models/Concrete Models/OrdersDevice.swift
Original file line number Diff line number Diff line change
@@ -1,10 +1,3 @@
//
// OrdersDevice.swift
// PassKit
//
// Created by Francesco Paolo Severino on 30/06/24.
//

import FluentKit
import PassKit

Expand Down
7 changes: 0 additions & 7 deletions Sources/Orders/Models/Concrete Models/OrdersErrorLog.swift
Original file line number Diff line number Diff line change
@@ -1,10 +1,3 @@
//
// OrdersErrorLog.swift
// PassKit
//
// Created by Francesco Paolo Severino on 30/06/24.
//

import FluentKit
import PassKit

Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,3 @@
//
// OrdersRegistration.swift
// PassKit
//
// Created by Francesco Paolo Severino on 30/06/24.
//

import FluentKit

/// The `Model` that stores orders registrations.
Expand Down
34 changes: 27 additions & 7 deletions Sources/Orders/Models/OrderDataModel.swift
Original file line number Diff line number Diff line change
@@ -1,18 +1,38 @@
//
// OrderDataModel.swift
// PassKit
//
// Created by Francesco Paolo Severino on 30/06/24.
//

import FluentKit

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

/// An identifier for the order type associated with the order.
static var typeIdentifier: String { get }

/// The foreign key to the order table.
var order: OrderType { get set }

/// Encode the order into JSON.
///
/// This method should generate the entire order JSON.
///
/// - Parameter db: The SQL database to query against.
///
/// - Returns: An object that conforms to ``OrderJSON/Properties``.
///
/// > Tip: See the [`Order`](https://developer.apple.com/documentation/walletorders/order) object to understand the keys.
func orderJSON(on db: any Database) async throws -> any OrderJSON.Properties

/// Should return a URL path which points to the template data for the order.
///
/// The path should point to a directory containing all the images and localizations for the generated `.order` archive
/// but should *not* contain any of these items:
/// - `manifest.json`
/// - `order.json`
/// - `signature`
///
/// - Parameter db: The SQL database to query against.
///
/// - Returns: A URL path which points to the template data for the order.
func template(on db: any Database) async throws -> String
}

extension OrderDataModel {
Expand Down
13 changes: 6 additions & 7 deletions Sources/Orders/Models/OrderModel.swift
Original file line number Diff line number Diff line change
@@ -1,10 +1,3 @@
//
// OrderModel.swift
// PassKit
//
// Created by Francesco Paolo Severino on 30/06/24.
//

import FluentKit
import Foundation

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

/// The authentication token supplied to your web service.
var authenticationToken: String { get set }

/// The designated initializer.
/// - Parameters:
/// - typeIdentifier: The order type identifier that’s registered with Apple.
/// - authenticationToken: The authentication token to use with the web service in the `webServiceURL` key.
init(typeIdentifier: String, authenticationToken: String)
}

extension OrderModel {
Expand Down
7 changes: 0 additions & 7 deletions Sources/Orders/Models/OrdersRegistrationModel.swift
Original file line number Diff line number Diff line change
@@ -1,10 +1,3 @@
//
// OrdersRegistrationModel.swift
// PassKit
//
// Created by Francesco Paolo Severino on 30/06/24.
//

import FluentKit
import PassKit

Expand Down
Loading

0 comments on commit 01bd819

Please sign in to comment.