Skip to content

Instantly share code, notes, and snippets.

@khanlou
Created June 21, 2017 18:48
Show Gist options
  • Save khanlou/133c3cf65d434ec2e66a28a519df3372 to your computer and use it in GitHub Desktop.
Save khanlou/133c3cf65d434ec2e66a28a519df3372 to your computer and use it in GitHub Desktop.
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