Advertisement
Guest User

Untitled

a guest
Jun 16th, 2019
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.14 KB | None | 0 0
  1. import Foundation
  2. import RxSwift
  3.  
  4. protocol ModelsStorageServiceProtocol {
  5.  
  6. }
  7.  
  8. enum ModelsStorageServiceError: Error {
  9. case db(DBAccessServiceError)
  10. case deserialization(Error)
  11. }
  12.  
  13. class ModelsStorageService {
  14. let dbAccessService: DBAccessServiceProtocol
  15.  
  16. init(dbAccessService: DBAccessServiceProtocol) {
  17. self.dbAccessService = dbAccessService
  18. }
  19.  
  20. func getModels(by sql: String) -> Single<Result<Model, ModelsStorageServiceError>> {
  21. return dbAccessService.select(sql: sql)
  22. .map { result in
  23. result
  24. .mapError { error in
  25. .db(error)
  26. }
  27. .flatMap { data in
  28. let model: Model
  29. do {
  30. model = try BSONDecoder().decode(data)
  31. } catch {
  32. return .failure(ModelsStorageServiceError.deserialization(error))
  33. }
  34. return .success(model)
  35. }
  36. }
  37. }
  38. }
  39.  
  40. extension ModelsStorageService: ModelsStorageServiceProtocol {
  41.  
  42. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement