Skip to content

Commit

Permalink
Add findInModel to relationship models
Browse files Browse the repository at this point in the history
  • Loading branch information
Koray Koska committed May 11, 2017
1 parent 558cd20 commit d6ac527
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 3 deletions.
8 changes: 7 additions & 1 deletion Sources/VaporJsonApi/Resources/JsonApiChildrenModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,16 @@ public struct JsonApiChildrenModel {
public let adder: JsonApiChildrenAdder?
public let replacer: JsonApiChildrenReplacer?

public init(childrenType: JsonApiResourceModel.Type, getter: @escaping JsonApiChildrenGetter, adder: JsonApiChildrenAdder? = nil, replacer: JsonApiChildrenReplacer? = nil) {
public let findInModel: (_ id: NodeRepresentable) throws -> JsonApiResourceModel?

public init<T: JsonApiResourceModel>(childrenType: T.Type, getter: @escaping JsonApiChildrenGetter, adder: JsonApiChildrenAdder? = nil, replacer: JsonApiChildrenReplacer? = nil) {
self.type = childrenType
self.getter = getter
self.adder = adder
self.replacer = replacer

self.findInModel = { id in
return try T.find(id)
}
}
}
8 changes: 7 additions & 1 deletion Sources/VaporJsonApi/Resources/JsonApiParentModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,15 @@ public struct JsonApiParentModel {
public let getter: JsonApiParentGetter
public let setter: JsonApiParentSetter?

public init(parentType: JsonApiResourceModel.Type, getter: @escaping JsonApiParentGetter, setter: JsonApiParentSetter? = nil) {
public let findInModel: (_ id: NodeRepresentable) throws -> JsonApiResourceModel?

public init<T: JsonApiResourceModel>(parentType: T.Type, getter: @escaping JsonApiParentGetter, setter: JsonApiParentSetter? = nil) {
self.getter = getter
self.setter = setter
self.type = parentType

self.findInModel = { id in
return try T.find(id)
}
}
}
12 changes: 11 additions & 1 deletion Sources/VaporJsonApi/Resources/JsonApiSiblingsModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,17 @@ public struct JsonApiSiblingsModel {
public let adder: JsonApiSiblingsAdder?
public let replacer: JsonApiSiblingsReplacer?

public init(siblingType: JsonApiResourceModel.Type, getter: @escaping JsonApiSiblingsGetter, adder: JsonApiSiblingsAdder? = nil, replacer: JsonApiSiblingsReplacer? = nil) {
public let findInModel: (_ id: NodeRepresentable) throws -> JsonApiResourceModel?

public init<T: JsonApiResourceModel>(siblingType: T.Type, getter: @escaping JsonApiSiblingsGetter, adder: JsonApiSiblingsAdder? = nil, replacer: JsonApiSiblingsReplacer? = nil) {
self.getter = getter
self.adder = adder
self.replacer = replacer
self.type = siblingType

self.findInModel = { id in
return try T.find(id)
}
}

public init<S: JsonApiResourceModel, T: JsonApiResourceModel>(model: S, siblingType: T.Type, localKey: String? = nil, foreignKey: String? = nil) {
Expand All @@ -51,5 +57,9 @@ public struct JsonApiSiblingsModel {
}

self.type = siblingType

self.findInModel = { id in
return try T.find(id)
}
}
}

0 comments on commit d6ac527

Please sign in to comment.