Skip to content

Instantly share code, notes, and snippets.

@AlexGladkov
Created December 5, 2018 08:16
Show Gist options
  • Save AlexGladkov/dc351c8716885fef9931d1ae9f469e32 to your computer and use it in GitHub Desktop.
Save AlexGladkov/dc351c8716885fef9931d1ae9f469e32 to your computer and use it in GitHub Desktop.
import bsh.StringUtil
import io.reactivex.Observable
import io.reactivex.Single
import ru.sddhelp.data.providers.LoginProvider
import ru.sddhelp.data.providers.LoginProviderImpl
import ru.sddhelp.data.providers.ProfileProvider
import ru.sddhelp.data.providers.ProfileProviderImpl
import ru.sddhelp.data.providers.models.*
import ru.sddhelp.domain.contracts.RemoteContract
import ru.sddhelp.domain.repositories.AuthRepository
import javax.inject.Inject
import javax.inject.Singleton
/**
* Created by Alex Gladkov on 21.02.18.
* Repository for auth operations (implementation)
*/
class AuthRepositoryImpl(private val loginProvider: LoginProvider,
private val profileProvider: ProfileProvider) : AuthRepository {
private val TAG: String = AuthRepositoryImpl::class.java.simpleName
override fun addVendor(token: String, vendor: String): Single<Boolean> {
return profileProvider.updateVendors(token = token, vendor = vendor)
.flatMap {
if (it.isSuccess) {
Single.just(true)
} else {
Single.error<Boolean>(Throwable(it.error.description))
}
}
}
override fun deleteVendor(token: String, code: String): Single<Boolean> {
return profileProvider.deleteVendor(token = token, code = code)
.flatMap {
if (it.isSuccess) {
Single.just(true)
} else {
Single.error<Boolean>(Throwable(it.error.description))
}
}
}
override fun checkPhone(phone: String, campaign: String): Observable<ApiPhone> {
return loginProvider.sendPhone(phone = phone, campaign = campaign)
.flatMap {
if (it.isSuccess) {
Observable.just(it.data!!)
} else {
Observable.error<ApiPhone>(Throwable(it.error.description))
}
}
}
override fun checkCode(phone: String, code: String): Observable<ApiLogin> {
return loginProvider.getToken(login = phone, password = code, type = RemoteContract.TYPE_ANDROID)
.flatMap {
if (it.isSuccess) {
Observable.just(it.data!!)
} else {
Observable.error<ApiLogin>(Throwable(it.error.description))
}
}
}
override fun updateProfile(token: String, name: String, surname: String, email: String): Observable<ApiProfile> {
return profileProvider.updateProfile(token = token, name = name, surname = surname, email = email)
.flatMap {
if (it.isSuccess) {
Observable.just(it.data!!)
} else {
Observable.error<ApiProfile>(Throwable(it.error.description))
}
}
}
override fun getProfile(token: String): Observable<ApiUser> {
return profileProvider.getProfile(token = token)
.flatMap {
if (it.isSuccess) {
Observable.just(it.data!!)
} else {
Observable.error<ApiUser>(Throwable(it.error.description))
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment