|
| 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 | +} |
0 commit comments