Skip to content

Instantly share code, notes, and snippets.

@isaac-weisberg
Last active June 14, 2019 07:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save isaac-weisberg/8625901d7ea143a7c1fea19535eb8df5 to your computer and use it in GitHub Desktop.
Save isaac-weisberg/8625901d7ea143a7c1fea19535eb8df5 to your computer and use it in GitHub Desktop.
import Foundation
import RxSwift
protocol ModelsStorageServiceProtocol {
}
enum ModelsStorageServiceError: Error {
case db(DBAccessServiceError)
case deserialization(Error)
}
class ModelsStorageService {
let dbAccessService: DBAccessServiceProtocol
init(dbAccessService: DBAccessServiceProtocol) {
self.dbAccessService = dbAccessService
}
func getModels(by sql: String) -> Single<Result<Model, ModelsStorageServiceError>> {
return dbAccessService.select(sql: sql)
.map { result in
result
.mapError { error in
.db(error)
}
.flatMap { data in
let model: Model
do {
model = try BSONDecoder().decode(data)
} catch {
return .failure(ModelsStorageServiceError.deserialization(error))
}
return .success(model)
}
}
}
}
extension ModelsStorageService: ModelsStorageServiceProtocol {
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment