Skip to content

Commit

Permalink
add new routing static method
Browse files Browse the repository at this point in the history
  • Loading branch information
tanner0101 committed Apr 20, 2018
1 parent eb63485 commit e15f8af
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 10 deletions.
13 changes: 4 additions & 9 deletions Sources/Fluent/Migration/Migration.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,21 +24,16 @@ 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<Self>) 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 {
guard property.path.count == 1 else {
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 {
Expand All @@ -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)
}
Expand Down
12 changes: 11 additions & 1 deletion Sources/Fluent/Model/Model.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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<Self> {
guard let idType = ID.self as? LosslessStringConvertible.Type else {
throw FluentError(
Expand Down Expand Up @@ -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<Self> {
return try make(for: parameter, using: container)
}
}

0 comments on commit e15f8af

Please sign in to comment.