CRUD продуктов
This commit is contained in:
parent
12d67bba56
commit
32c72e0efa
@ -10,9 +10,11 @@ import okhttp3.MediaType.Companion.toMediaType
|
||||
import okhttp3.logging.HttpLoggingInterceptor
|
||||
import retrofit2.Retrofit
|
||||
import retrofit2.http.Body
|
||||
import retrofit2.http.DELETE
|
||||
import retrofit2.http.GET
|
||||
import retrofit2.http.Header
|
||||
import retrofit2.http.POST
|
||||
import retrofit2.http.PUT
|
||||
import retrofit2.http.Path
|
||||
|
||||
|
||||
@ -43,9 +45,19 @@ interface MyServerService {
|
||||
|
||||
@POST("product")
|
||||
suspend fun insertProduct(
|
||||
@Body product: ProductModelRemote
|
||||
@Body product: ProductModelRemote,
|
||||
@Header("Authorization") token: String
|
||||
) : ProductModelRemote
|
||||
|
||||
@PUT("product")
|
||||
suspend fun updateProduct(
|
||||
@Body product: ProductModelRemote,
|
||||
@Header("Authorization") token: String
|
||||
): ProductModelRemote
|
||||
|
||||
@DELETE("product/{id}")
|
||||
suspend fun deleteProduct(@Path("id") id: Int, @Header("Authorization") token: String)
|
||||
|
||||
|
||||
|
||||
|
||||
|
@ -5,10 +5,11 @@ import kotlinx.serialization.Serializable
|
||||
|
||||
@Serializable
|
||||
data class ProductModelRemote(
|
||||
val id: Int = 0,
|
||||
val id: Int? = null,
|
||||
val title: String = "",
|
||||
val price: Int = 0,
|
||||
val old_price: Int? = null
|
||||
val old_price: Int? = null,
|
||||
|
||||
)
|
||||
|
||||
@Serializable
|
||||
@ -22,5 +23,5 @@ fun ProductModelRemote.toProductModel(): ProductModel = ProductModel(
|
||||
)
|
||||
|
||||
fun ProductModel.toProductModelRemote(): ProductModelRemote = ProductModelRemote(
|
||||
title = title, price = price, old_price = oldPrice
|
||||
title = title, price = price, old_price = oldPrice, id = id
|
||||
)
|
||||
|
@ -8,7 +8,15 @@ import javax.inject.Inject
|
||||
class RestProductRepository @Inject constructor(
|
||||
private val service: MyServerService
|
||||
) {
|
||||
suspend fun insert(product: ProductModel) {
|
||||
service.insertProduct(product.toProductModelRemote())
|
||||
suspend fun insert(product: ProductModel, token: String) {
|
||||
service.insertProduct(product.toProductModelRemote(), token)
|
||||
}
|
||||
|
||||
suspend fun update(product: ProductModel, token: String) {
|
||||
service.updateProduct((product.toProductModelRemote()), token)
|
||||
}
|
||||
|
||||
suspend fun delete(id: Int, token: String) {
|
||||
service.deleteProduct(id, token)
|
||||
}
|
||||
}
|
@ -20,15 +20,16 @@ class ProductRepository @Inject constructor(
|
||||
private val orderProductDao: OrderProductDao,
|
||||
private val restRepository: RestProductRepository
|
||||
) {
|
||||
suspend fun insert(product: ProductModel) {
|
||||
return restRepository.insert(product)
|
||||
suspend fun insert(product: ProductModel, token: String) {
|
||||
return restRepository.insert(product, token)
|
||||
}
|
||||
suspend fun update(product: ProductModel) {
|
||||
return productDao.update(product)
|
||||
suspend fun update(product: ProductModel, token: String) {
|
||||
return restRepository.update(product, token)
|
||||
}
|
||||
suspend fun delete(product: ProductModel) {
|
||||
orderProductDao.deleteByProductId(product.id!!)
|
||||
return productDao.delete(product)
|
||||
suspend fun delete(product: ProductModel, token: String) {
|
||||
//orderProductDao.deleteByProductId(product.id!!)
|
||||
productDao.delete(product)
|
||||
return restRepository.delete(product.id!!, token)
|
||||
}
|
||||
fun getById(id: Int): Flow<ProductModel> {
|
||||
return productDao.getById(id)
|
||||
|
@ -20,6 +20,7 @@ import androidx.compose.runtime.remember
|
||||
import androidx.compose.ui.Alignment
|
||||
import androidx.compose.ui.Modifier
|
||||
import androidx.compose.ui.draw.clip
|
||||
import androidx.compose.ui.platform.LocalContext
|
||||
import androidx.compose.ui.text.font.FontWeight
|
||||
import androidx.compose.ui.text.input.TextFieldValue
|
||||
import androidx.compose.ui.unit.dp
|
||||
@ -27,6 +28,7 @@ import androidx.compose.ui.unit.sp
|
||||
import androidx.compose.ui.zIndex
|
||||
import androidx.hilt.navigation.compose.hiltViewModel
|
||||
import androidx.navigation.NavHostController
|
||||
import com.example.shawarma.data.sharedpref.PreferencesManager
|
||||
import com.example.shawarma.ui.theme.MarckFamily
|
||||
import com.example.shawarma.ui.theme.MyLightYellow
|
||||
import com.example.shawarma.ui.theme.MyMainBackground
|
||||
@ -49,6 +51,9 @@ fun ProductScreen(navHostController: NavHostController, productId: Int?) {
|
||||
|
||||
@Composable
|
||||
fun ProductWidget(navHostController: NavHostController, productId: Int?) {
|
||||
val preferencesManager = PreferencesManager(LocalContext.current)
|
||||
val searchToken = preferencesManager.getData("token", "")
|
||||
|
||||
val title = remember { mutableStateOf(TextFieldValue(""))}
|
||||
val price = remember { mutableStateOf(TextFieldValue(""))}
|
||||
val oldPrice = remember { mutableStateOf(TextFieldValue(""))}
|
||||
@ -71,7 +76,9 @@ fun ProductWidget(navHostController: NavHostController, productId: Int?) {
|
||||
if (product != null) {
|
||||
title.value = TextFieldValue(text = product.title)
|
||||
price.value = TextFieldValue(text = product.price.toString())
|
||||
oldPrice.value = TextFieldValue(text = product.oldPrice.toString())
|
||||
if (product.oldPrice != null) {
|
||||
oldPrice.value = TextFieldValue(text = product.oldPrice.toString())
|
||||
}
|
||||
}
|
||||
|
||||
Box(
|
||||
@ -140,9 +147,9 @@ fun ProductWidget(navHostController: NavHostController, productId: Int?) {
|
||||
shape = RoundedCornerShape(20.dp),
|
||||
onClick = {
|
||||
if (product == null)
|
||||
productViewModel.addProduct(title.value.text, price.value.text.toInt(), if (!oldPrice.value.text.isNullOrEmpty()) oldPrice.value.text.toInt() else null)
|
||||
productViewModel.addProduct(title.value.text, price.value.text.toInt(), if (!oldPrice.value.text.isNullOrEmpty()) oldPrice.value.text.toInt() else null, searchToken)
|
||||
else
|
||||
productViewModel.updateProduct(title.value.text, price.value.text.toInt(), if (!oldPrice.value.text.isNullOrEmpty()) oldPrice.value.text.toInt() else null)
|
||||
productViewModel.updateProduct(title.value.text, price.value.text.toInt(), if (!oldPrice.value.text.isNullOrEmpty()) oldPrice.value.text.toInt() else null, searchToken)
|
||||
}
|
||||
) {
|
||||
Text(
|
||||
|
@ -113,7 +113,7 @@ fun ProductsList(navHostController: NavHostController){
|
||||
products.itemCount,
|
||||
key = products.itemKey()
|
||||
) { index ->
|
||||
ProductItem(products[index]!!, navHostController, productsViewModel)
|
||||
ProductItem(products[index]!!, navHostController, productsViewModel, searchToken)
|
||||
Spacer(modifier = Modifier.height(20.dp))
|
||||
if (index == products.itemCount - 1) {
|
||||
Spacer(modifier = Modifier.height(70.dp))
|
||||
@ -125,7 +125,7 @@ fun ProductsList(navHostController: NavHostController){
|
||||
}
|
||||
|
||||
@Composable
|
||||
fun ProductItem(product: ProductModel, navHostController: NavHostController, productsViewModel: ProductsViewModel){
|
||||
fun ProductItem(product: ProductModel, navHostController: NavHostController, productsViewModel: ProductsViewModel, token: String){
|
||||
Card(
|
||||
border = BorderStroke(width = 2.dp, color = MyOrange),
|
||||
shape = RoundedCornerShape(size = 20.dp),
|
||||
@ -192,7 +192,7 @@ fun ProductItem(product: ProductModel, navHostController: NavHostController, pro
|
||||
backgroundColor = MyLightRed
|
||||
),
|
||||
onClick = {
|
||||
productsViewModel.deleteProduct(product)
|
||||
productsViewModel.deleteProduct(product, token)
|
||||
}
|
||||
) {
|
||||
Icon(
|
||||
|
@ -22,9 +22,9 @@ class ProductViewModel @Inject constructor(
|
||||
val product: LiveData<ProductModel>
|
||||
get() = _product
|
||||
|
||||
fun addProduct(name: String, price: Int, oldPrice: Int? = null) {
|
||||
fun addProduct(name: String, price: Int, oldPrice: Int? = null, token: String) {
|
||||
viewModelScope.launch {
|
||||
productRepository.insert(ProductModel(null, name, price, oldPrice))
|
||||
productRepository.insert(ProductModel(null, name, price, oldPrice), token)
|
||||
_addingProductState.postValue(true)
|
||||
}
|
||||
}
|
||||
@ -37,9 +37,9 @@ class ProductViewModel @Inject constructor(
|
||||
}
|
||||
}
|
||||
|
||||
fun updateProduct(name: String, price: Int, oldPrice: Int? = null) {
|
||||
fun updateProduct(name: String, price: Int, oldPrice: Int? = null, token: String) {
|
||||
viewModelScope.launch {
|
||||
productRepository.update(ProductModel(product.value?.id, name, price, oldPrice))
|
||||
productRepository.update(ProductModel(product.value?.id, name, price, oldPrice), token)
|
||||
_addingProductState.postValue(true)
|
||||
}
|
||||
}
|
||||
|
@ -19,9 +19,9 @@ class ProductsViewModel @Inject constructor(
|
||||
return productRepository.getAllItemsPaged(token)
|
||||
}
|
||||
|
||||
fun deleteProduct(product: ProductModel) {
|
||||
fun deleteProduct(product: ProductModel, token: String) {
|
||||
viewModelScope.launch {
|
||||
productRepository.delete(product)
|
||||
productRepository.delete(product, token)
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user