From 3d1b0ffa8d372fc74148d63d046b06dbafe978e7 Mon Sep 17 00:00:00 2001 From: twof Date: Mon, 1 Oct 2018 14:15:53 -0700 Subject: [PATCH] Parent and Children now have default routes --- .../CrudRouter/CrudChildrenController.swift | 7 +++- Sources/CrudRouter/CrudController.swift | 36 +++++++++---------- Sources/CrudRouter/CrudParentController.swift | 7 +++- 3 files changed, 30 insertions(+), 20 deletions(-) diff --git a/Sources/CrudRouter/CrudChildrenController.swift b/Sources/CrudRouter/CrudChildrenController.swift index 2d2cabe..43cef79 100644 --- a/Sources/CrudRouter/CrudChildrenController.swift +++ b/Sources/CrudRouter/CrudChildrenController.swift @@ -90,7 +90,7 @@ public extension CrudChildrenControllerProtocol { .unwrap(or: Abort(.notFound)) .delete(on: req) .transform(to: HTTPStatus.ok) - } + } } } @@ -111,6 +111,11 @@ public struct CrudChildrenController>, 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 diff --git a/Sources/CrudRouter/CrudController.swift b/Sources/CrudRouter/CrudController.swift index a9bfb12..30a74cc 100644 --- a/Sources/CrudRouter/CrudController.swift +++ b/Sources/CrudRouter/CrudController.swift @@ -77,7 +77,7 @@ public struct CrudController: CrudControllerProtocol wh public func crudRouterCollection( at path: PathComponentsRepresentable..., forParent relation: KeyPath> - ) -> CrudParentController where + ) -> CrudParentController where ParentType: Model & Content, ModelType.Database == ParentType.Database, ParentType.ID: Parameter { @@ -89,7 +89,7 @@ public struct CrudController: CrudControllerProtocol wh public func crudRouterCollection( at path: PathComponentsRepresentable..., forChildren relation: KeyPath> - ) -> CrudChildrenController where + ) -> CrudChildrenController where ChildType: Model & Content, ModelType.Database == ChildType.Database, ChildType.ID: Parameter { @@ -102,7 +102,7 @@ public struct CrudController: CrudControllerProtocol wh at path: PathComponentsRepresentable..., forParent relation: KeyPath>, relationConfiguration: ((CrudParentController) throws -> Void)?=nil - ) throws where + ) throws where ParentType: Model & Content, ModelType.Database == ParentType.Database, ParentType.ID: Parameter { @@ -117,7 +117,7 @@ public struct CrudController: CrudControllerProtocol wh at path: PathComponentsRepresentable..., forChildren relation: KeyPath>, relationConfiguration: ((CrudChildrenController) throws -> Void)?=nil - ) throws where + ) throws where ChildType: Model & Content, ModelType.Database == ChildType.Database, ChildType.ID: Parameter { @@ -128,20 +128,20 @@ public struct CrudController: CrudControllerProtocol wh try controller.boot(router: self.router) } -// public func crudRouterCollection( -// forSiblings relation: KeyPath>, -// at path: [PathComponentsRepresentable] -// ) -> CrudSiblingsController 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( + // forSiblings relation: KeyPath>, + // at path: [PathComponentsRepresentable] + // ) -> CrudSiblingsController 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 { diff --git a/Sources/CrudRouter/CrudParentController.swift b/Sources/CrudRouter/CrudParentController.swift index 4651c06..a94bff9 100644 --- a/Sources/CrudRouter/CrudParentController.swift +++ b/Sources/CrudRouter/CrudParentController.swift @@ -31,7 +31,7 @@ public extension CrudParentControllerProtocol { .unwrap(or: Abort(.notFound)) .flatMap { child in return child[keyPath: self.relation].get(on: req) - } + } } } @@ -52,6 +52,11 @@ public struct CrudParentController>, 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