From e15f8af4804537d36e25f9e34a437b53be931e7e Mon Sep 17 00:00:00 2001 From: tanner0101 Date: Thu, 19 Apr 2018 21:47:52 -0400 Subject: [PATCH] add new routing static method --- Sources/Fluent/Migration/Migration.swift | 13 ++++--------- Sources/Fluent/Model/Model.swift | 12 +++++++++++- 2 files changed, 15 insertions(+), 10 deletions(-) diff --git a/Sources/Fluent/Migration/Migration.swift b/Sources/Fluent/Migration/Migration.swift index d9328c8f..d1d3d907 100644 --- a/Sources/Fluent/Migration/Migration.swift +++ b/Sources/Fluent/Migration/Migration.swift @@ -24,7 +24,9 @@ public protocol Migration { extension Model where Database: SchemaSupporting { /// Automatically adds `SchemaField`s for each of this `Model`s properties. public static func addProperties(to builder: SchemaCreator) throws { - let idProperty = try Self.reflectProperty(forKey: idKey) + guard let idProperty = try Self.reflectProperty(forKey: idKey) else { + throw FluentError(identifier: "idProperty", reason: "Unable to reflect ID property for \(Self.self).", source: .capture()) + } let properties = try Self.reflectProperties() for property in properties { @@ -32,13 +34,6 @@ extension Model where Database: SchemaSupporting { continue } - let isID: Bool - if let id = idProperty { - isID = property.path == id.path - } else { - isID = (property.path == ["id"] || property.path == ["_id"]) - } - let type: Any.Type let isOptional: Bool if let o = property.type as? AnyOptionalType.Type { @@ -53,7 +48,7 @@ extension Model where Database: SchemaSupporting { name: property.path.first ?? "", type: Database.fieldType(for: type), isOptional: isOptional, - isIdentifier: isID + isIdentifier: property.path == idProperty.path ) builder.schema.addFields.append(field) } diff --git a/Sources/Fluent/Model/Model.swift b/Sources/Fluent/Model/Model.swift index c2916bcb..ec884fb7 100644 --- a/Sources/Fluent/Model/Model.swift +++ b/Sources/Fluent/Model/Model.swift @@ -240,7 +240,7 @@ extension Model { // MARK: Routing extension Model where Database: QuerySupporting { - /// See `Parameter.make` + /// See `Parameter`. public static func make(for parameter: String, using container: Container) throws -> Future { guard let idType = ID.self as? LosslessStringConvertible.Type else { throw FluentError( @@ -276,4 +276,14 @@ extension Model where Database: QuerySupporting { return container.withPooledConnection(to: dbid, closure: findModel) } } + + /// See `Parameter`. + public static var routingSlug: String { + return "\(entity)_id" + } + + /// See `Parameter`. + public static func resolveParameter(_ parameter: String, on container: Container) throws -> Future { + return try make(for: parameter, using: container) + } }