Last active
December 22, 2018 13:33
-
-
Save emarashliev/941dd65719b87c6f640945a055b5e4e2 to your computer and use it in GitHub Desktop.
Repository Case 1
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
// The Model | |
final class User: Model { | |
typealias Database = MySQLDatabase /// the problem is this line | |
typealias ID = Int | |
static var idKey: IDKey { return \.id } | |
var id: Int? | |
var name: String | |
var email: String | |
} | |
// The Repository | |
protocol UserRepository: ServiceType { | |
func all() -> Future<[User]> | |
} | |
final class MySQLUserRepository: UserRepository { | |
let db: MySQLDatabase.ConnectionPool | |
init(_ db: MySQLDatabase.ConnectionPool) { | |
self.db = db | |
} | |
func all() -> Future<[User]> { | |
return db.withConnection { conn in | |
return User.query(on: conn).all() | |
} | |
} | |
} | |
// The Controller | |
struct UsersController { | |
func getAllHandler(_ req: Request) throws -> Future<[User]> { | |
let repository = try req.make(UsersRepository.self) | |
return repository.all() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment