Created
June 21, 2017 18:48
-
-
Save khanlou/133c3cf65d434ec2e66a28a519df3372 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Foundation | |
import HTTP | |
import Vapor | |
public protocol SideEffect { | |
func perform() throws | |
} | |
public protocol Command: ResponseRepresentable { | |
init(request: Request, droplet: Droplet) throws | |
var status: Status { get } | |
func validate() throws | |
var sideEffects: [SideEffect] { get } | |
func execute() throws -> JSON | |
} | |
extension Command { | |
public var sideEffects: [SideEffect] { | |
return [] | |
} | |
public func validate() throws { | |
} | |
public func makeResponse() throws -> Response { | |
let response = Response(status: self.status) | |
try validate() | |
response.json = try execute() | |
try sideEffects.forEach({ try $0.perform() }) | |
return response | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment