HBRouteHandler

public protocol HBRouteHandler

Object for handling requests.

Instead of passing a closure to the router you can provide an object it should try and create before handling the request. This allows you to separate the extraction of data from the request and the processing of the request. For example

struct UpdateReminder: HBRouteHandler {
    struct Request: Codable {
        let description: String
        let date: Date
    }
    let update: Request
    let id: String

    init(from request: HBRequest) throws {
        self.update = try request.decode(as: Request.self)
        self.id = try request.parameters.require("id")
    }
    func handle(request: HBRequest) -> EventLoopFuture<HTTPResponseStatus> {
        let reminder = Reminder(id: id, update: update)
        return reminder.update(on: request.db)
            .map { _ in .ok }
    }
}