Fix automatic detection for Publicable

This commit is contained in:
Simon Edelmann 2020-02-21 23:42:38 +01:00
parent 00e57d659e
commit d2a08785e8
5 changed files with 16 additions and 15 deletions

View File

@ -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()
}

View File

@ -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))
}
}

View File

@ -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)
}

View File

@ -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)
}
}
}

View File

@ -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)
}
}
}