Skip to content

Commit

Permalink
Parent and Children now have default routes
Browse files Browse the repository at this point in the history
  • Loading branch information
twof committed Oct 1, 2018
1 parent 4adb1d2 commit 3d1b0ff
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 20 deletions.
7 changes: 6 additions & 1 deletion Sources/CrudRouter/CrudChildrenController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ public extension CrudChildrenControllerProtocol {
.unwrap(or: Abort(.notFound))
.delete(on: req)
.transform(to: HTTPStatus.ok)
}
}
}
}

Expand All @@ -111,6 +111,11 @@ public struct CrudChildrenController<ChildT: Model & Content, ParentT: Model & C
let path: [PathComponentsRepresentable]

public init(childrenRelation: KeyPath<ParentT, Children<ParentT, ChildT>>, basePath: [PathComponentsRepresentable], path: [PathComponentsRepresentable]) {
let path
= path.count == 0
? [String(describing: ChildType.self).snakeCased()! as PathComponentsRepresentable]
: path

self.children = childrenRelation
self.basePath = basePath
self.path = path
Expand Down
36 changes: 18 additions & 18 deletions Sources/CrudRouter/CrudController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ public struct CrudController<ModelT: Model & Content>: CrudControllerProtocol wh
public func crudRouterCollection<ParentType>(
at path: PathComponentsRepresentable...,
forParent relation: KeyPath<ModelType, Parent<ModelType, ParentType>>
) -> CrudParentController<ModelType, ParentType> where
) -> CrudParentController<ModelType, ParentType> where
ParentType: Model & Content,
ModelType.Database == ParentType.Database,
ParentType.ID: Parameter {
Expand All @@ -89,7 +89,7 @@ public struct CrudController<ModelT: Model & Content>: CrudControllerProtocol wh
public func crudRouterCollection<ChildType>(
at path: PathComponentsRepresentable...,
forChildren relation: KeyPath<ModelType, Children<ModelType, ChildType>>
) -> CrudChildrenController<ChildType, ModelType> where
) -> CrudChildrenController<ChildType, ModelType> where
ChildType: Model & Content,
ModelType.Database == ChildType.Database,
ChildType.ID: Parameter {
Expand All @@ -102,7 +102,7 @@ public struct CrudController<ModelT: Model & Content>: CrudControllerProtocol wh
at path: PathComponentsRepresentable...,
forParent relation: KeyPath<ModelType, Parent<ModelType, ParentType>>,
relationConfiguration: ((CrudParentController<ModelType, ParentType>) throws -> Void)?=nil
) throws where
) throws where
ParentType: Model & Content,
ModelType.Database == ParentType.Database,
ParentType.ID: Parameter {
Expand All @@ -117,7 +117,7 @@ public struct CrudController<ModelT: Model & Content>: CrudControllerProtocol wh
at path: PathComponentsRepresentable...,
forChildren relation: KeyPath<ModelType, Children<ModelType, ChildType>>,
relationConfiguration: ((CrudChildrenController<ChildType, ModelType>) throws -> Void)?=nil
) throws where
) throws where
ChildType: Model & Content,
ModelType.Database == ChildType.Database,
ChildType.ID: Parameter {
Expand All @@ -128,20 +128,20 @@ public struct CrudController<ModelT: Model & Content>: CrudControllerProtocol wh
try controller.boot(router: self.router)
}

// public func crudRouterCollection<ChildType, ThroughType>(
// forSiblings relation: KeyPath<ModelType, Siblings<ModelType, ChildType, ThroughType>>,
// at path: [PathComponentsRepresentable]
// ) -> CrudSiblingsController<ChildType, ModelType, ThroughType> where
// ChildType: Content,
// ModelType.Database == ThroughType.Database,
// ChildType.ID: Parameter,
// ThroughType: ModifiablePivot,
// ThroughType.Database: JoinSupporting,
// ThroughType.Database == ChildType.Database {
// let baseIdPath = self.path.appending(ModelType.ID.parameter)
//
// return CrudSiblingsController(siblingRelation: relation, basePath: baseIdPath, path: path)
// }
// public func crudRouterCollection<ChildType, ThroughType>(
// forSiblings relation: KeyPath<ModelType, Siblings<ModelType, ChildType, ThroughType>>,
// at path: [PathComponentsRepresentable]
// ) -> CrudSiblingsController<ChildType, ModelType, ThroughType> where
// ChildType: Content,
// ModelType.Database == ThroughType.Database,
// ChildType.ID: Parameter,
// ThroughType: ModifiablePivot,
// ThroughType.Database: JoinSupporting,
// ThroughType.Database == ChildType.Database {
// let baseIdPath = self.path.appending(ModelType.ID.parameter)
//
// return CrudSiblingsController(siblingRelation: relation, basePath: baseIdPath, path: path)
// }
}

extension CrudController: RouteCollection {
Expand Down
7 changes: 6 additions & 1 deletion Sources/CrudRouter/CrudParentController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public extension CrudParentControllerProtocol {
.unwrap(or: Abort(.notFound))
.flatMap { child in
return child[keyPath: self.relation].get(on: req)
}
}
}
}

Expand All @@ -52,6 +52,11 @@ public struct CrudParentController<ChildT: Model & Content, ParentT: Model & Con
let path: [PathComponentsRepresentable]

public init(relation: KeyPath<ChildType, Parent<ChildType, ParentType>>, basePath: [PathComponentsRepresentable], path: [PathComponentsRepresentable]) {
let path
= path.count == 0
? [String(describing: ParentType.self).snakeCased()! as PathComponentsRepresentable]
: path

self.relation = relation
self.basePath = basePath
self.path = path
Expand Down

0 comments on commit 3d1b0ff

Please sign in to comment.