Skip to content

Commit

Permalink
Make Children and Siblings more consistent with Parent model
Browse files Browse the repository at this point in the history
  • Loading branch information
Ybrin committed May 10, 2017
1 parent 9356e91 commit 5f8278e
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 49 deletions.
24 changes: 7 additions & 17 deletions Sources/VaporJsonApi/Resources/JsonApiChildrenModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,32 +9,22 @@
import Vapor
import Fluent

public struct JsonApiChildrenModel<T: JsonApiResourceModel> {
public struct JsonApiChildrenModel {

public typealias JsonApiChildrenGetter = () throws -> Children<T>
public typealias JsonApiChildrenAdder = ([T]) throws -> ()
public typealias JsonApiChildrenReplacer = ([T]) throws -> ()
public typealias JsonApiChildrenGetter = () throws -> [JsonApiResourceModel]
public typealias JsonApiChildrenAdder = ([JsonApiResourceModel]) throws -> ()
public typealias JsonApiChildrenReplacer = ([JsonApiResourceModel]) throws -> ()

public var type: JsonApiResourceModel.Type {
return T.self
}
public var type: JsonApiResourceModel.Type

public let getter: JsonApiChildrenGetter
public let adder: JsonApiChildrenAdder?
public let replacer: JsonApiChildrenReplacer?

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

public init(parent: JsonApiResourceModel, foreignKey: String?, adder: JsonApiChildrenAdder? = nil, replacer: JsonApiChildrenReplacer? = nil) {
getter = {
return parent.children(foreignKey, T.self)
}

self.adder = adder
self.replacer = replacer
}
}
13 changes: 2 additions & 11 deletions Sources/VaporJsonApi/Resources/JsonApiParentModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,9 @@ public struct JsonApiParentModel {
public let getter: JsonApiParentGetter
public let setter: JsonApiParentSetter?

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

public init(type: JsonApiResourceModel.Type, child: JsonApiResourceModel, parentId: Node, foreignKey: String? = nil, setter: JsonApiParentSetter? = nil) {
getter = {
return try child.parent(parentId, foreignKey, type).get()
}

self.setter = setter
self.type = type
self.type = parentType
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,9 @@ public extension JsonApiResourceController {
let pageNumber = page.pageNumber
let pageCount = page.pageCount

let resources = try children.limit(pageCount, withOffset: (pageNumber * pageCount) - pageCount).all()
// TODO: Pagination
// let resources = try children.limit(pageCount, withOffset: (pageNumber * pageCount) - pageCount).all()
let resources = children
let jsonDocument = try document(forResources: resources, baseUrl: req.uri)

return JsonApiResponse(status: .ok, document: jsonDocument)
Expand All @@ -127,7 +129,9 @@ public extension JsonApiResourceController {
let pageNumber = page.pageNumber
let pageCount = page.pageCount

let resources = try siblings.limit(pageCount, withOffset: (pageNumber * pageCount) - pageCount).all()
// TODO: Pagination
// let resources = try siblings.limit(pageCount, withOffset: (pageNumber * pageCount) - pageCount).all()
let resources = siblings
let jsonDocument = try document(forResources: resources, baseUrl: req.uri)

return JsonApiResponse(status: .ok, document: jsonDocument)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ public protocol JsonApiResourceRepresentable {

typealias JsonApiParentRelationships = [String: JsonApiParentModel]

typealias JsonApiChildrenRelationships = [String: JsonApiChildrenModel<JsonApiResourceModel>]
typealias JsonApiChildrenRelationships = [String: JsonApiChildrenModel]

typealias JsonApiSiblingsRelationships = [String: JsonApiSiblingsModel<JsonApiResourceModel>]
typealias JsonApiSiblingsRelationships = [String: JsonApiSiblingsModel]

static var resourceType: JsonApiResourceType { get }

Expand Down Expand Up @@ -143,7 +143,7 @@ public extension JsonApiResourceRepresentable {
public func makeChildrenRelationshipObject (
name: String,
type: JsonApiResourceModel.Type,
getter: (() throws -> Children<JsonApiResourceModel>)? = nil,
getter: (() throws -> [JsonApiResourceModel])? = nil,
baseUrl: URI,
resourcePath: String,
meta: JsonApiMeta? = nil,
Expand All @@ -154,7 +154,7 @@ public extension JsonApiResourceRepresentable {
// TODO: Pagination
var resourceLinkage: JsonApiResourceLinkage? = nil
if data {
if let children = try getter?().all() {
if let children = try getter?() {
var resourceIdentifierObjects = [JsonApiResourceIdentifierObject]()
for c in children {
guard let id = c.id?.string ?? c.id?.int?.string else {
Expand All @@ -173,7 +173,7 @@ public extension JsonApiResourceRepresentable {
public func makeSiblingsRelationshipObject (
name: String,
type: JsonApiResourceModel.Type,
getter: (() throws -> Siblings<JsonApiResourceModel>)? = nil,
getter: (() throws -> [JsonApiResourceModel])? = nil,
baseUrl: URI,
resourcePath: String,
meta: JsonApiMeta? = nil,
Expand All @@ -184,7 +184,7 @@ public extension JsonApiResourceRepresentable {
// TODO: Pagination
var resourceLinkage: JsonApiResourceLinkage? = nil
if data {
if let siblings = try getter?().all() {
if let siblings = try getter?() {
var resourceIdentifierObjects = [JsonApiResourceIdentifierObject]()
for s in siblings {
guard let id = s.id?.string ?? s.id?.int?.string else {
Expand Down
28 changes: 15 additions & 13 deletions Sources/VaporJsonApi/Resources/JsonApiSiblingsModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,45 +9,47 @@
import Vapor
import Fluent

public struct JsonApiSiblingsModel<T: JsonApiResourceModel> {
public struct JsonApiSiblingsModel {

public typealias JsonApiSiblingsGetter = () throws -> Siblings<T>
public typealias JsonApiSiblingsAdder = ([T]) throws -> ()
public typealias JsonApiSiblingsReplacer = ([T]) throws -> ()
public typealias JsonApiSiblingsGetter = () throws -> [JsonApiResourceModel]
public typealias JsonApiSiblingsAdder = ([JsonApiResourceModel]) throws -> ()
public typealias JsonApiSiblingsReplacer = ([JsonApiResourceModel]) throws -> ()

public var type: JsonApiResourceModel.Type {
return T.self
}
public var type: JsonApiResourceModel.Type

public let getter: JsonApiSiblingsGetter
public let adder: JsonApiSiblingsAdder?
public let replacer: JsonApiSiblingsReplacer?

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

public init<S: JsonApiResourceModel>(toSibling: S, localKey: String? = nil, foreignKey: String? = nil) {
public init<S: JsonApiResourceModel, T: JsonApiResourceModel>(model: S, siblingType: T.Type, localKey: String? = nil, foreignKey: String? = nil) {
getter = {
return try toSibling.siblings(localKey, foreignKey)
let elements: [S] = try model.siblings(localKey, foreignKey).all()
return elements
}

// TODO: Don't allow duplicate linkage
self.adder = { siblings in
for s in siblings {
var p = Pivot<S, T>(toSibling, s)
var p = Pivot<S, T>(model, s)
try p.save()
}
}
self.replacer = { siblings in
let oldSiblings: Siblings<T> = try toSibling.siblings(localKey, foreignKey)
let oldSiblings: Siblings<T> = try model.siblings(localKey, foreignKey)
try oldSiblings.delete()
for s in siblings {
var p = Pivot<S, T>(toSibling, s)
var p = Pivot<S, T>(model, s)
try p.save()
}
}

self.type = siblingType
}
}

0 comments on commit 5f8278e

Please sign in to comment.