Skip to content

Commit

Permalink
formatting and readme update
Browse files Browse the repository at this point in the history
  • Loading branch information
twof committed Sep 29, 2018
1 parent 9cba123 commit c49b21d
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 11 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,4 @@ DELETE /todo/:id
- query parameter support
- PATCH support
- more fine grained response statuses
- embeded fields ex. `/user/1/todo` to get all `todo`s belonging to user with id 1
30 changes: 19 additions & 11 deletions Sources/CrudRouter/CrudRouter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@ public protocol CrudControllerProtocol {

public extension CrudControllerProtocol {
func indexAll(_ req: Request) throws -> Future<[ModelType]> {
return ModelType.query(on: req).all().map { Array($0) }
return ModelType
.query(on: req)
.all()
.map { Array($0) }
}

func index(_ req: Request) throws -> Future<ModelType> {
Expand All @@ -21,18 +24,23 @@ public extension CrudControllerProtocol {
}

func create(_ req: Request) throws -> Future<ModelType> {
return try req.content.decode(ModelType.self).flatMap { model in
return model.save(on: req)
}
return try req
.content
.decode(ModelType.self)
.flatMap { model in
return model.save(on: req)
}
}

func update(_ req: Request) throws -> Future<ModelType> {
let id: ModelType.ID = try getId(from: req)
return try req.content.decode(ModelType.self).flatMap { model in
var temp = model
temp.fluentID = id
return temp.update(on: req)
}
return try req
.content.decode(ModelType.self)
.flatMap { model in
var temp = model
temp.fluentID = id
return temp.update(on: req)
}
}

func delete(_ req: Request) throws -> Future<HTTPStatus> {
Expand All @@ -42,7 +50,7 @@ public extension CrudControllerProtocol {
.unwrap(or: Abort(.notFound))
.flatMap { model in
return model.delete(on: req).transform(to: HTTPStatus.ok)
}
}
}
}

Expand All @@ -62,7 +70,7 @@ public extension Router {
func crudRegister<ModelType: Model & Content>(
_ path: PathComponentsRepresentable...,
for type: ModelType.Type
) where ModelType.ID: Parameter {
) where ModelType.ID: Parameter {
let controller = CrudController<ModelType>()

self.get(path, CrudController<ModelType>.ModelType.ID.parameter, use: controller.index)
Expand Down

0 comments on commit c49b21d

Please sign in to comment.