DI для репозиториев

This commit is contained in:
Данила Мочалов 2023-11-20 19:34:55 +04:00
parent e839f69d19
commit ff29494a6c
12 changed files with 134 additions and 25 deletions

View File

@ -73,4 +73,8 @@ dependencies {
implementation 'androidx.room:room-runtime:2.5.0' // Библиотека "Room"
kapt "androidx.room:room-compiler:2.5.0" // Кодогенератор
implementation 'androidx.room:room-ktx:2.5.0' // Дополнительно для Kotlin Coroutines, Kotlin Flows
// lab4
implementation "com.google.dagger:hilt-android:2.40.5"
kapt "com.google.dagger:hilt-android-compiler:2.40.5"
}

View File

@ -11,16 +11,14 @@ import com.example.shawarma.screens.authorization.AuthorizationScreen
import com.example.shawarma.screens.registration.RegistrationScreen
import com.example.shawarma.ui.theme.MyLightYellow
import com.example.shawarma.ui.theme.ShawarmaTheme
import dagger.hilt.android.HiltAndroidApp
class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
MainNavBar()
}
}
}

View File

@ -6,10 +6,10 @@ import androidx.room.Room
import androidx.room.RoomDatabase
import androidx.room.TypeConverters
import androidx.sqlite.db.SupportSQLiteDatabase
import com.example.shawarma.data.dao.OrderDao
import com.example.shawarma.data.dao.OrderProductDao
import com.example.shawarma.data.dao.ProductDao
import com.example.shawarma.data.dao.UserDao
import com.example.shawarma.data.interfaces.dao.OrderDao
import com.example.shawarma.data.interfaces.dao.OrderProductDao
import com.example.shawarma.data.interfaces.dao.ProductDao
import com.example.shawarma.data.interfaces.dao.UserDao
import com.example.shawarma.data.models.OrderModel
import com.example.shawarma.data.models.OrderProductModel
import com.example.shawarma.data.models.OrderStatus

View File

@ -1,4 +1,4 @@
package com.example.shawarma.data.dao
package com.example.shawarma.data.interfaces.dao
import androidx.room.Dao
import androidx.room.Delete
@ -8,23 +8,18 @@ import androidx.room.Update
import com.example.shawarma.data.models.OrderModel
import com.example.shawarma.data.models.OrderWithProducts
import kotlinx.coroutines.flow.Flow
@Dao
interface OrderDao {
@Insert
suspend fun insert(order: OrderModel)
@Update
suspend fun update(order: OrderModel)
@Delete
suspend fun delete(order: OrderModel)
@Query("select * from orders")
fun getAll() : Flow<List<OrderWithProducts>>
fun getAll(): Flow<List<OrderWithProducts>>
@Query("select * from orders where orders.id =:id")
fun getById(id: Int): Flow<List<OrderWithProducts>>
@Query("select * from orders where orders.user_id =:userId")
fun getByUserId(userId: Int): Flow<List<OrderWithProducts>>
}

View File

@ -1,9 +1,8 @@
package com.example.shawarma.data.dao
package com.example.shawarma.data.interfaces.dao
import androidx.room.Dao
import androidx.room.Insert
import com.example.shawarma.data.models.OrderProductModel
@Dao
interface OrderProductDao {
@Insert

View File

@ -1,4 +1,4 @@
package com.example.shawarma.data.dao
package com.example.shawarma.data.interfaces.dao
import androidx.room.Dao
import androidx.room.Delete
@ -7,7 +7,6 @@ import androidx.room.Query
import androidx.room.Update
import com.example.shawarma.data.models.ProductModel
import kotlinx.coroutines.flow.Flow
@Dao
interface ProductDao {
@Insert
@ -30,4 +29,5 @@ interface ProductDao {
@Query("select * from products where products.id = :id")
fun getById(id: Int): Flow<ProductModel>
}

View File

@ -1,4 +1,4 @@
package com.example.shawarma.data.dao
package com.example.shawarma.data.interfaces.dao
import androidx.room.Dao
import androidx.room.Delete
@ -12,16 +12,12 @@ import kotlinx.coroutines.flow.Flow
interface UserDao {
@Insert
suspend fun insert(user: UserModel)
@Update
suspend fun update(user: UserModel)
@Delete
suspend fun delete(user: UserModel)
@Query("select * from users order by user_login collate nocase asc")
fun getAll() : Flow<List<UserModel>>
fun getAll(): Flow<List<UserModel>>
@Query("select * from users where users.id = :id")
fun getById(id: Int): UserModel
}

View File

@ -0,0 +1,16 @@
package com.example.shawarma.data.repos
import androidx.room.Dao
import androidx.room.Insert
import com.example.shawarma.data.interfaces.dao.OrderDao
import com.example.shawarma.data.interfaces.dao.OrderProductDao
import com.example.shawarma.data.models.OrderProductModel
import javax.inject.Inject
class OrderProductRepository @Inject constructor(
private val orderProductDao: OrderProductDao
) {
suspend fun insert(order: OrderProductModel) {
return orderProductDao.insert(order)
}
}

View File

@ -0,0 +1,36 @@
package com.example.shawarma.data.repos
import androidx.room.Dao
import androidx.room.Delete
import androidx.room.Insert
import androidx.room.Query
import androidx.room.Update
import com.example.shawarma.data.interfaces.dao.OrderDao
import com.example.shawarma.data.interfaces.dao.ProductDao
import com.example.shawarma.data.models.OrderModel
import com.example.shawarma.data.models.OrderWithProducts
import kotlinx.coroutines.flow.Flow
import javax.inject.Inject
class OrderRepository @Inject constructor(
private val orderDao: OrderDao
){
suspend fun insert(order: OrderModel) {
return orderDao.insert(order)
}
suspend fun update(order:OrderModel) {
return orderDao.update(order)
}
suspend fun delete(order: OrderModel) {
return orderDao.delete(order)
}
fun getAll(): Flow<List<OrderWithProducts>> {
return orderDao.getAll()
}
fun getById(id: Int): Flow<List<OrderWithProducts>> {
return orderDao.getById(id)
}
fun getByUserId(userId: Int): Flow<List<OrderWithProducts>> {
return orderDao.getByUserId(userId)
}
}

View File

@ -0,0 +1,37 @@
package com.example.shawarma.data.repos
import androidx.room.Delete
import androidx.room.Insert
import androidx.room.Query
import androidx.room.Update
import com.example.shawarma.data.interfaces.dao.ProductDao
import com.example.shawarma.data.interfaces.dao.UserDao
import com.example.shawarma.data.models.ProductModel
import kotlinx.coroutines.flow.Flow
import javax.inject.Inject
class ProductRepository @Inject constructor(
private val productDao: ProductDao
) {
suspend fun insert(product: ProductModel) {
return productDao.insert(product)
}
suspend fun update(product: ProductModel) {
return productDao.update(product)
}
suspend fun delete(product: ProductModel) {
return productDao.delete(product)
}
fun getAll(): Flow<List<ProductModel>> {
return productDao.getAll()
}
fun getDiscounts(): Flow<List<ProductModel>> {
return productDao.getDiscounts()
}
fun getItems(): Flow<List<ProductModel>> {
return productDao.getItems()
}
fun getById(id: Int): Flow<ProductModel> {
return productDao.getById(id)
}
}

View File

@ -0,0 +1,27 @@
package com.example.shawarma.data.repos
import androidx.room.Dao
import com.example.shawarma.data.interfaces.dao.UserDao
import com.example.shawarma.data.models.UserModel
import kotlinx.coroutines.flow.Flow
import javax.inject.Inject
class UserRepository @Inject constructor(
private val userDao: UserDao
) {
suspend fun insert(user: UserModel) {
return userDao.insert(user)
}
suspend fun update (user: UserModel) {
return userDao.update(user)
}
suspend fun delete (user: UserModel) {
return userDao.delete(user)
}
fun getAll(): Flow<List<UserModel>> {
return userDao.getAll()
}
fun getById(id: Int): UserModel {
return userDao.getById(id)
}
}

View File

@ -7,6 +7,7 @@ buildscript {
}
dependencies {
classpath 'com.android.tools.build:gradle:4.0.0'
classpath 'com.google.dagger:hilt-android-gradle-plugin:2.40.5'
}
}// Top-level build file where you can add configuration options common to all sub-projects/modules.
plugins {