Last active
June 14, 2019 07:20
-
-
Save isaac-weisberg/8625901d7ea143a7c1fea19535eb8df5 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 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