Немного рефакторим user repository
This commit is contained in:
parent
95189032c2
commit
32991b2e14
@ -6,7 +6,6 @@ import com.example.shawarma.data.api.MyServerService
|
||||
import com.example.shawarma.data.api.repos.RestOrderProductRepository
|
||||
import com.example.shawarma.data.api.repos.RestOrderRepository
|
||||
import com.example.shawarma.data.api.repos.RestProductRepository
|
||||
import com.example.shawarma.data.api.repos.RestUserRepository
|
||||
import com.example.shawarma.data.db.AppDatabase
|
||||
import com.example.shawarma.data.repos.OrderProductRepository
|
||||
import com.example.shawarma.data.repos.OrderRepository
|
||||
@ -16,7 +15,6 @@ import dagger.Module
|
||||
import dagger.Provides
|
||||
import dagger.hilt.InstallIn
|
||||
import dagger.hilt.components.SingletonComponent
|
||||
|
||||
import javax.inject.Singleton
|
||||
@Module
|
||||
@InstallIn(SingletonComponent::class)
|
||||
@ -39,14 +37,8 @@ object AppModule {
|
||||
|
||||
@Provides
|
||||
@Singleton
|
||||
fun provideUserRepository(db: AppDatabase) : UserRepository {
|
||||
return UserRepository(db.userDao())
|
||||
}
|
||||
|
||||
@Provides
|
||||
@Singleton
|
||||
fun provideRestUserRepository(service: MyServerService) : RestUserRepository {
|
||||
return RestUserRepository(service)
|
||||
fun provideUserRepository(service: MyServerService) : UserRepository {
|
||||
return UserRepository(service)
|
||||
}
|
||||
|
||||
@Provides
|
||||
@ -64,7 +56,7 @@ object AppModule {
|
||||
@Provides
|
||||
@Singleton
|
||||
fun provideOrderRepository(db: AppDatabase, restOrderRepository: RestOrderRepository) : OrderRepository {
|
||||
return OrderRepository(db, db.orderDao(), db.productDao(), db.orderProductDao(), restOrderRepository)
|
||||
return OrderRepository(db, db.orderDao(), db.orderProductDao(), restOrderRepository)
|
||||
}
|
||||
|
||||
@Provides
|
||||
|
@ -1,24 +0,0 @@
|
||||
package com.example.shawarma.data.api.repos
|
||||
|
||||
import com.example.shawarma.data.api.MyServerService
|
||||
import com.example.shawarma.data.api.models.TokenModelRemote
|
||||
import com.example.shawarma.data.api.models.UserModelRemote
|
||||
import com.example.shawarma.data.api.models.toUserModelRemote
|
||||
import com.example.shawarma.data.db.models.UserModel
|
||||
import javax.inject.Inject
|
||||
|
||||
class RestUserRepository @Inject constructor(
|
||||
private val service: MyServerService
|
||||
) {
|
||||
suspend fun insert(user: UserModel) {
|
||||
service.createUser(user.toUserModelRemote())
|
||||
}
|
||||
|
||||
suspend fun getToken(user: UserModel): TokenModelRemote {
|
||||
return service.getToken(user.toUserModelRemote())
|
||||
}
|
||||
|
||||
suspend fun checkLogin(user: UserModel): UserModelRemote? {
|
||||
return service.checkLogin(user.toUserModelRemote())
|
||||
}
|
||||
}
|
@ -13,7 +13,6 @@ import com.example.shawarma.data.api.repos.RestOrderRepository
|
||||
import com.example.shawarma.data.db.AppDatabase
|
||||
import com.example.shawarma.data.db.dao.OrderDao
|
||||
import com.example.shawarma.data.db.dao.OrderProductDao
|
||||
import com.example.shawarma.data.db.dao.ProductDao
|
||||
import com.example.shawarma.data.db.models.OrderModel
|
||||
import com.example.shawarma.data.db.models.OrderWithProducts
|
||||
import kotlinx.coroutines.flow.Flow
|
||||
@ -23,7 +22,6 @@ import javax.inject.Inject
|
||||
class OrderRepository @Inject constructor(
|
||||
private val database: AppDatabase,
|
||||
private val orderDao: OrderDao,
|
||||
private val productDao: ProductDao,
|
||||
private val orderProductDao: OrderProductDao,
|
||||
private val restRepository: RestOrderRepository
|
||||
){
|
||||
|
@ -1,26 +1,24 @@
|
||||
package com.example.shawarma.data.repos
|
||||
|
||||
import com.example.shawarma.data.db.dao.UserDao
|
||||
import com.example.shawarma.data.api.MyServerService
|
||||
import com.example.shawarma.data.api.models.TokenModelRemote
|
||||
import com.example.shawarma.data.api.models.UserModelRemote
|
||||
import com.example.shawarma.data.api.models.toUserModelRemote
|
||||
import com.example.shawarma.data.db.models.UserModel
|
||||
import kotlinx.coroutines.flow.Flow
|
||||
import javax.inject.Inject
|
||||
|
||||
class UserRepository @Inject constructor(
|
||||
private val userDao: UserDao
|
||||
) {
|
||||
private val service: MyServerService
|
||||
) {
|
||||
suspend fun insert(user: UserModel) {
|
||||
return userDao.insert(user)
|
||||
service.createUser(user.toUserModelRemote())
|
||||
}
|
||||
suspend fun update (user: UserModel) {
|
||||
return userDao.update(user)
|
||||
|
||||
suspend fun getToken(user: UserModel): TokenModelRemote {
|
||||
return service.getToken(user.toUserModelRemote())
|
||||
}
|
||||
suspend fun delete (user: UserModel) {
|
||||
return userDao.delete(user)
|
||||
}
|
||||
fun getById(id: Int): Flow<UserModel> {
|
||||
return userDao.getById(id)
|
||||
}
|
||||
fun login(login: String, password: String): Flow<UserModel?> {
|
||||
return userDao.login(login, password)
|
||||
|
||||
suspend fun checkLogin(user: UserModel): UserModelRemote? {
|
||||
return service.checkLogin(user.toUserModelRemote())
|
||||
}
|
||||
}
|
@ -4,7 +4,7 @@ import androidx.lifecycle.LiveData
|
||||
import androidx.lifecycle.MutableLiveData
|
||||
import androidx.lifecycle.ViewModel
|
||||
import androidx.lifecycle.viewModelScope
|
||||
import com.example.shawarma.data.api.repos.RestUserRepository
|
||||
import com.example.shawarma.data.repos.UserRepository
|
||||
import com.example.shawarma.data.db.models.UserModel
|
||||
import dagger.hilt.android.lifecycle.HiltViewModel
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
@ -14,7 +14,7 @@ import javax.inject.Inject
|
||||
|
||||
@HiltViewModel
|
||||
class UserViewModel @Inject constructor(
|
||||
private val restUserRepository: RestUserRepository
|
||||
private val userRepository: UserRepository
|
||||
) : ViewModel() {
|
||||
private val _token = MutableLiveData<String?>()
|
||||
val token: LiveData<String?>
|
||||
@ -27,7 +27,7 @@ class UserViewModel @Inject constructor(
|
||||
fun login(login: String, password: String){
|
||||
viewModelScope.launch {
|
||||
withContext(Dispatchers.Main) {
|
||||
val token_response = restUserRepository.getToken(UserModel(id = null, login = login, password = password, role = ""))
|
||||
val token_response = userRepository.getToken(UserModel(id = null, login = login, password = password, role = ""))
|
||||
if (token_response.access_token.isNotEmpty()) {
|
||||
_token.postValue(token_response.access_token)
|
||||
_authorizationState.postValue(true)
|
||||
@ -64,14 +64,14 @@ class UserViewModel @Inject constructor(
|
||||
}
|
||||
viewModelScope.launch {
|
||||
withContext(Dispatchers.Main) {
|
||||
val user_response = restUserRepository.checkLogin(UserModel(null, login, password, ""))
|
||||
val user_response = userRepository.checkLogin(UserModel(null, login, password, ""))
|
||||
if (user_response != null) {
|
||||
if (user_response.login.isNullOrEmpty()) {
|
||||
if (login == "admin" && password == "admin") {
|
||||
restUserRepository.insert(UserModel(null, login, password, "ADMIN"))
|
||||
userRepository.insert(UserModel(null, login, password, "ADMIN"))
|
||||
_registrationState.postValue(true)
|
||||
} else {
|
||||
restUserRepository.insert(UserModel(null, login, password, "USER"))
|
||||
userRepository.insert(UserModel(null, login, password, "USER"))
|
||||
_registrationState.postValue(true)
|
||||
}
|
||||
} else {
|
||||
|
Loading…
Reference in New Issue
Block a user