Skip to content

Instantly share code, notes, and snippets.

@emarashliev
Last active December 22, 2018 13:33
Show Gist options
  • Save emarashliev/941dd65719b87c6f640945a055b5e4e2 to your computer and use it in GitHub Desktop.
Save emarashliev/941dd65719b87c6f640945a055b5e4e2 to your computer and use it in GitHub Desktop.
Repository Case 1
// 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