Fix automatic detection for Publicable
This commit is contained in:
parent
00e57d659e
commit
d2a08785e8
|
@ -4,22 +4,22 @@ import Fluent
|
|||
public struct CrudController<T: Model & Content>: CrudControllerProtocol where T.IDValue: LosslessStringConvertible {
|
||||
typealias ModelT = T
|
||||
|
||||
static func indexAll(req: Request) -> EventLoopFuture<[T]> {
|
||||
func indexAll(req: Request) -> EventLoopFuture<[T]> {
|
||||
indexAll(on: req.db)
|
||||
}
|
||||
|
||||
static func index(req: Request) -> EventLoopFuture<T> {
|
||||
func index(req: Request) -> EventLoopFuture<T> {
|
||||
let id: T.IDValue? = req.parameters.get("id")
|
||||
return index(id, on: req.db)
|
||||
}
|
||||
}
|
||||
|
||||
extension CrudController where T: Publicable {
|
||||
static func indexAll(req: Request) -> EventLoopFuture<[T.Public]> {
|
||||
func indexAll(req: Request) -> EventLoopFuture<[T.Public]> {
|
||||
indexAll(on: req.db).public()
|
||||
}
|
||||
|
||||
static func index(req: Request) -> EventLoopFuture<T.Public> {
|
||||
func index(req: Request) -> EventLoopFuture<T.Public> {
|
||||
let id: T.IDValue? = req.parameters.get("id")
|
||||
return index(id, on: req.db).public()
|
||||
}
|
||||
|
|
|
@ -6,11 +6,11 @@ protocol CrudControllerProtocol {
|
|||
}
|
||||
|
||||
extension CrudControllerProtocol {
|
||||
internal static func indexAll(on database: Database) -> EventLoopFuture<[ModelT]> {
|
||||
internal func indexAll(on database: Database) -> EventLoopFuture<[ModelT]> {
|
||||
return ModelT.query(on: database).all()
|
||||
}
|
||||
|
||||
internal static func index(_ id: ModelT.IDValue?, on database: Database) -> EventLoopFuture<ModelT> {
|
||||
internal func index(_ id: ModelT.IDValue?, on database: Database) -> EventLoopFuture<ModelT> {
|
||||
return ModelT.find(id, on: database).unwrap(or: Abort(.notFound))
|
||||
}
|
||||
}
|
||||
|
|
|
@ -3,15 +3,14 @@ import Fluent
|
|||
|
||||
extension RoutesBuilder {
|
||||
public func crud<T: Model & Content>(model: T.Type) where T.IDValue: LosslessStringConvertible {
|
||||
registerCrud(self, model: model)
|
||||
let crudController = CrudController<T>()
|
||||
self.get(use: crudController.indexAll)
|
||||
self.get(":id", use: crudController.index)
|
||||
}
|
||||
|
||||
public func crud<T: Model & Content & Publicable>(model: T.Type) where T.IDValue: LosslessStringConvertible {
|
||||
registerCrud(self, model: model)
|
||||
let crudController = CrudController<T>()
|
||||
self.get(use: crudController.indexAll)
|
||||
self.get(":id", use: crudController.index)
|
||||
}
|
||||
}
|
||||
|
||||
internal func registerCrud<T: Model & Content>(_ routes: RoutesBuilder, model: T.Type) where T.IDValue: LosslessStringConvertible {
|
||||
routes.get(use: CrudController<T>.indexAll)
|
||||
routes.get(":id", use: CrudController<T>.index)
|
||||
}
|
||||
|
|
|
@ -17,11 +17,12 @@ final class IndexAllTests: ApplicationXCTestCase {
|
|||
try routes()
|
||||
|
||||
try app.test(.GET, "/todos") { res in
|
||||
XCTAssertContent([Todo].self, res) {
|
||||
XCTAssertContent([Todo.Public].self, res) {
|
||||
XCTAssertGreaterThan($0.count, 0)
|
||||
XCTAssertEqual($0.count, 3)
|
||||
XCTAssertNotEqual($0.count, 2)
|
||||
XCTAssertEqual($0[0].title, Todo(title: "Wash clothes").title)
|
||||
XCTAssertTrue($0[0].isPublic)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -17,9 +17,10 @@ final class IndexTests: ApplicationXCTestCase {
|
|||
|
||||
let id = 1
|
||||
try app.test(.GET, "/todos/\(id)") { res in
|
||||
XCTAssertContent(Todo.self, res) {
|
||||
XCTAssertContent(Todo.Public.self, res) {
|
||||
XCTAssertEqual($0.id, id)
|
||||
XCTAssertEqual($0.title, "Wash clothes")
|
||||
XCTAssertTrue($0.isPublic)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue