deleted cart fields for coffee
This commit is contained in:
parent
fab50baea5
commit
bb21aa8cac
@ -50,8 +50,8 @@ class RestCoffeeRepository(
|
||||
return service.createCoffee(Coffee(name, cost, ingredients, null, 0).toCoffeeRemote()).toCoffee().uid.toLong()
|
||||
}
|
||||
|
||||
override suspend fun update(uid: Int, name: String, cost: Double, ingredients: String, cartId: Int?, count: Int): Int? {
|
||||
return service.updateCoffee(uid, Coffee(uid, name, cost, ingredients, cartId, count).toCoffeeRemote()).toCoffee().uid
|
||||
override suspend fun update(uid: Int, name: String, cost: Double, ingredients: String): Int? {
|
||||
return service.updateCoffee(uid, Coffee(uid, name, cost, ingredients).toCoffeeRemote()).toCoffee().uid
|
||||
}
|
||||
|
||||
override suspend fun delete(coffee: Coffee) {
|
||||
|
@ -8,25 +8,19 @@ data class CoffeeRemote(
|
||||
val id: Int = 0,
|
||||
val name: String = "",
|
||||
val cost: Double = 0.0,
|
||||
val ingredients: String = "",
|
||||
val cartId: Int? = 0,
|
||||
val count: Int = 0
|
||||
val ingredients: String = ""
|
||||
)
|
||||
|
||||
fun CoffeeRemote.toCoffee(): Coffee = Coffee(
|
||||
id,
|
||||
name,
|
||||
cost,
|
||||
ingredients,
|
||||
cartId,
|
||||
count
|
||||
ingredients
|
||||
)
|
||||
|
||||
fun Coffee.toCoffeeRemote(): CoffeeRemote = CoffeeRemote(
|
||||
uid,
|
||||
name,
|
||||
cost,
|
||||
ingredients,
|
||||
cartId,
|
||||
count
|
||||
ingredients
|
||||
)
|
@ -59,7 +59,7 @@ abstract class AppDatabase : RoomDatabase() {
|
||||
coffeeDao.insert(coffee11.name, coffee11.cost, coffee11.ingredients)
|
||||
// Cart
|
||||
val cartDao = database.cartDao()
|
||||
val cart = Cart()
|
||||
val cart = Cart(2, 1)
|
||||
cartDao.insert(cart)
|
||||
}
|
||||
}
|
||||
|
@ -4,28 +4,45 @@ import androidx.paging.PagingSource
|
||||
import androidx.room.Dao
|
||||
import androidx.room.Insert
|
||||
import androidx.room.Query
|
||||
import androidx.room.Transaction
|
||||
import androidx.room.Update
|
||||
import com.zyzf.coffeepreorder.database.model.Cart
|
||||
import com.zyzf.coffeepreorder.database.model.Coffee
|
||||
|
||||
@Dao
|
||||
interface CartDao {
|
||||
@Query("select * from cart limit 1")
|
||||
suspend fun get(): Cart
|
||||
@Query("select * from cart")
|
||||
suspend fun getAll(): Cart
|
||||
|
||||
@Query("select * from coffee where cart_id is not null and count > 0 order by name collate nocase asc")
|
||||
@Query("select coffee.uid, coffee.name, coffee.cost, coffee.ingredients from cart join coffee on coffee.uid = cart.coffee_id and cart.count > 0 collate nocase")
|
||||
fun getAllInCart(): PagingSource<Int, Coffee>
|
||||
|
||||
@Query("select sum(cost) from coffee where cart_id is not null and count > 0")
|
||||
@Query("select sum(coffee.cost) from cart JOIN coffee on coffee.uid = cart.coffee_id and cart.count > 0")
|
||||
fun getSumInCart(): Double
|
||||
|
||||
@Query("select coffee.cost from cart JOIN coffee on coffee.uid = cart.coffee_id where coffee.uid = :coffeeId")
|
||||
fun getCountForCoffee(coffeeId: Int): Double
|
||||
|
||||
@Insert
|
||||
suspend fun insert(cart: Cart)
|
||||
|
||||
@Query("update coffee set cart_id = :cartId, count = count + :count where uid = :coffeeId")
|
||||
suspend fun insertCoffee(cartId: Int, coffeeId: Int, count: Int)
|
||||
@Query("select * from cart where coffee_id = :coffeeId limit 1")
|
||||
suspend fun findCoffee(coffeeId: Int): Cart?
|
||||
@Query("insert into cart (coffee_id, count) values (:coffeeId, :count)")
|
||||
suspend fun insertNewCoffee(coffeeId: Int, count: Int)
|
||||
@Query("update cart set count = count + :count where coffee_id = :coffeeId")
|
||||
suspend fun updateExistingCoffee(coffeeId: Int, count: Int)
|
||||
@Transaction
|
||||
suspend fun insertCoffee(coffeeId: Int, count: Int) {
|
||||
val cart: Cart? = findCoffee(coffeeId)
|
||||
if (cart != null) {
|
||||
updateExistingCoffee(coffeeId, count)
|
||||
} else {
|
||||
insertNewCoffee(coffeeId, count)
|
||||
}
|
||||
}
|
||||
|
||||
@Query("update coffee set count = count - :count where uid = :coffeeId")
|
||||
@Query("update cart set count = count - :count where coffee_id = :coffeeId")
|
||||
suspend fun deleteCoffee(coffeeId: Int, count: Int)
|
||||
|
||||
@Update
|
||||
|
@ -11,14 +11,14 @@ interface CoffeeDao {
|
||||
@Query("select * from coffee order by name collate nocase asc")
|
||||
fun getAll(): PagingSource<Int, Coffee>
|
||||
|
||||
@Query("select coffee.uid, name, cost, ingredients, cart_id, count from coffee where coffee.uid = :uid")
|
||||
@Query("select coffee.uid, name, cost, ingredients from coffee where coffee.uid = :uid")
|
||||
suspend fun getByUid(uid: Int): Coffee?
|
||||
|
||||
@Query("insert into coffee (name, cost, ingredients, count) values (:name, :cost, :ingredients, 0)")
|
||||
@Query("insert into coffee (name, cost, ingredients) values (:name, :cost, :ingredients)")
|
||||
suspend fun insert(name: String, cost: Double, ingredients: String): Long
|
||||
|
||||
@Query("update coffee set name = :name, cost = :cost, ingredients = :ingredients, cart_id = :cartId, count = :count where uid = :uid")
|
||||
suspend fun update(uid: Int, name: String, cost: Double, ingredients: String, cartId: Int?, count: Int): Int?
|
||||
@Query("update coffee set name = :name, cost = :cost, ingredients = :ingredients where uid = :uid")
|
||||
suspend fun update(uid: Int, name: String, cost: Double, ingredients: String): Int?
|
||||
|
||||
@Delete
|
||||
suspend fun delete(coffee: Coffee)
|
||||
|
@ -1,16 +1,33 @@
|
||||
package com.zyzf.coffeepreorder.database.model
|
||||
|
||||
import androidx.room.ColumnInfo
|
||||
import androidx.room.Entity
|
||||
import androidx.room.ForeignKey
|
||||
import androidx.room.Ignore
|
||||
import androidx.room.PrimaryKey
|
||||
|
||||
@Entity(tableName = "cart")
|
||||
@Entity(tableName = "cart", foreignKeys = [
|
||||
ForeignKey(
|
||||
entity = Cart::class,
|
||||
parentColumns = ["uid"],
|
||||
childColumns = ["coffee_id"],
|
||||
onDelete = ForeignKey.RESTRICT,
|
||||
onUpdate = ForeignKey.RESTRICT
|
||||
)
|
||||
])
|
||||
data class Cart(
|
||||
@PrimaryKey(autoGenerate = true)
|
||||
val uid: Int?,
|
||||
@ColumnInfo(name = "coffee_id", index = true)
|
||||
val coffeeId: Int,
|
||||
@ColumnInfo(name = "count", index = true)
|
||||
val count: Int = 0
|
||||
) {
|
||||
@Ignore
|
||||
constructor() : this(null)
|
||||
constructor(
|
||||
coffeeId: Int,
|
||||
count: Int
|
||||
) : this(null, coffeeId, 0)
|
||||
|
||||
override fun equals(other: Any?): Boolean {
|
||||
if (this === other) return true
|
||||
|
@ -6,15 +6,7 @@ import androidx.room.ForeignKey
|
||||
import androidx.room.Ignore
|
||||
import androidx.room.PrimaryKey
|
||||
|
||||
@Entity(tableName = "coffee", foreignKeys = [
|
||||
ForeignKey(
|
||||
entity = Cart::class,
|
||||
parentColumns = ["uid"],
|
||||
childColumns = ["cart_id"],
|
||||
onDelete = ForeignKey.RESTRICT,
|
||||
onUpdate = ForeignKey.RESTRICT
|
||||
)
|
||||
])
|
||||
@Entity(tableName = "coffee")
|
||||
data class Coffee(
|
||||
@PrimaryKey(autoGenerate = true)
|
||||
val uid: Int = 0,
|
||||
@ -23,11 +15,7 @@ data class Coffee(
|
||||
@ColumnInfo(name = "cost")
|
||||
var cost: Double,
|
||||
@ColumnInfo(name = "ingredients")
|
||||
var ingredients: String,
|
||||
@ColumnInfo(name = "cart_id", index = true)
|
||||
val cartId: Int?,
|
||||
@ColumnInfo(name = "count")
|
||||
val count: Int = 0
|
||||
var ingredients: String
|
||||
) {
|
||||
@Ignore
|
||||
constructor(
|
||||
@ -36,7 +24,7 @@ data class Coffee(
|
||||
ingredients: String,
|
||||
cartId: Int?,
|
||||
count: Int?
|
||||
) : this(0, name, cost, ingredients, null, 0)
|
||||
) : this(0, name, cost, ingredients)
|
||||
|
||||
companion object {
|
||||
fun getCoffee(index: Int = 0): Coffee {
|
||||
@ -44,9 +32,7 @@ data class Coffee(
|
||||
index,
|
||||
"",
|
||||
0.0,
|
||||
"",
|
||||
null,
|
||||
0
|
||||
""
|
||||
)
|
||||
}
|
||||
}
|
||||
|
@ -6,12 +6,13 @@ import com.zyzf.coffeepreorder.database.model.Coffee
|
||||
import kotlinx.coroutines.flow.Flow
|
||||
|
||||
interface CartRepository {
|
||||
suspend fun get(): Cart
|
||||
suspend fun getAll(): Cart
|
||||
suspend fun insert(cart: Cart)
|
||||
fun getAllInCart(): Flow<PagingData<Coffee>>
|
||||
fun getSumInCart(): Double
|
||||
suspend fun insertCoffee(cartId: Int, coffeeId: Int, count: Int)
|
||||
suspend fun insertCoffee(coffeeId: Int, count: Int)
|
||||
suspend fun deleteCoffee(coffeeId: Int, count: Int)
|
||||
fun getCountForCoffee(coffeeId: Int): Double
|
||||
suspend fun update(cart: Cart)
|
||||
suspend fun deleteAll()
|
||||
}
|
@ -8,6 +8,6 @@ interface CoffeeRepository {
|
||||
fun getAll(): Flow<PagingData<Coffee>>
|
||||
suspend fun getByUid(uid: Int): Coffee?
|
||||
suspend fun insert(name: String, cost: Double, ingredients: String): Long
|
||||
suspend fun update(uid: Int, name: String, cost: Double, ingredients: String, cartId: Int?, count: Int): Int?
|
||||
suspend fun update(uid: Int, name: String, cost: Double, ingredients: String): Int?
|
||||
suspend fun delete(coffee: Coffee)
|
||||
}
|
@ -18,10 +18,11 @@ class OfflineCartRepository(private val cartDao: CartDao) : CartRepository {
|
||||
pagingSourceFactory = cartDao::getAllInCart
|
||||
).flow
|
||||
override fun getSumInCart(): Double = cartDao.getSumInCart()
|
||||
override suspend fun get(): Cart = cartDao.get()
|
||||
override suspend fun getAll(): Cart = cartDao.getAll()
|
||||
override suspend fun insert(cart: Cart) = cartDao.insert(cart)
|
||||
override suspend fun insertCoffee(cartId: Int, coffeeId: Int, count: Int) = cartDao.insertCoffee(cartId, coffeeId, count)
|
||||
override suspend fun insertCoffee(coffeeId: Int, count: Int) = cartDao.insertCoffee(coffeeId, count)
|
||||
override suspend fun deleteCoffee(coffeeId: Int, count: Int) = cartDao.deleteCoffee(coffeeId, count)
|
||||
override fun getCountForCoffee(coffeeId: Int): Double = cartDao.getCountForCoffee(coffeeId)
|
||||
override suspend fun update(cart: Cart) = cartDao.update(cart)
|
||||
override suspend fun deleteAll() = cartDao.deleteAll()
|
||||
}
|
@ -22,6 +22,6 @@ class OfflineCoffeeRepository(private val coffeeDao: CoffeeDao) : CoffeeReposito
|
||||
suspend fun clearCoffees() = coffeeDao.deleteAll()
|
||||
override suspend fun getByUid(uid: Int): Coffee? = coffeeDao.getByUid(uid)
|
||||
override suspend fun insert(name: String, cost: Double, ingredients: String): Long = coffeeDao.insert(name, cost, ingredients)
|
||||
override suspend fun update(uid: Int, name: String, cost: Double, ingredients: String, cartId: Int?, count: Int): Int? = coffeeDao.update(uid, name, cost, ingredients, cartId, count)
|
||||
override suspend fun update(uid: Int, name: String, cost: Double, ingredients: String): Int? = coffeeDao.update(uid, name, cost, ingredients)
|
||||
override suspend fun delete(coffee: Coffee) = coffeeDao.delete(coffee)
|
||||
}
|
@ -95,18 +95,26 @@ fun Cart(
|
||||
)
|
||||
}
|
||||
) { innerPadding ->
|
||||
Box (modifier = Modifier.padding(0.dp).pullRefresh(state)) {
|
||||
Box (modifier = Modifier
|
||||
.padding(0.dp)
|
||||
.pullRefresh(state)) {
|
||||
PullRefreshIndicator(refreshing = refreshing, state = state,
|
||||
modifier = Modifier.zIndex(100f).align(Alignment.TopCenter)
|
||||
modifier = Modifier
|
||||
.zIndex(100f)
|
||||
.align(Alignment.TopCenter)
|
||||
)
|
||||
CartList(
|
||||
modifier = Modifier
|
||||
.padding(innerPadding).pullRefresh(state)
|
||||
.padding(innerPadding)
|
||||
.pullRefresh(state)
|
||||
.fillMaxSize(),
|
||||
coffeeList = coffeeListUiState,
|
||||
onDeleteFromCartClick = {currentCoffee: Coffee ->
|
||||
coffee.value = currentCoffee
|
||||
openDialog.value = true
|
||||
},
|
||||
getCoffeeCount = {coffeeId: Int ->
|
||||
viewModel.getCountForCoffee(coffeeId)
|
||||
}
|
||||
)
|
||||
}
|
||||
@ -125,7 +133,8 @@ fun Cart(
|
||||
private fun CartList(
|
||||
modifier: Modifier = Modifier,
|
||||
coffeeList: LazyPagingItems<Coffee>,
|
||||
onDeleteFromCartClick: (coffee: Coffee) -> Unit
|
||||
onDeleteFromCartClick: (coffee: Coffee) -> Unit,
|
||||
getCoffeeCount: (coffeeId: Int) -> Double
|
||||
) {
|
||||
LazyColumn(
|
||||
horizontalAlignment = Alignment.CenterHorizontally) {
|
||||
@ -138,7 +147,8 @@ private fun CartList(
|
||||
coffee?.let {
|
||||
CartListItem(
|
||||
coffee = coffee,
|
||||
onDeleteFromCartClick = onDeleteFromCartClick
|
||||
onDeleteFromCartClick = onDeleteFromCartClick,
|
||||
getCoffeeCount = getCoffeeCount
|
||||
)
|
||||
}
|
||||
}
|
||||
@ -149,7 +159,8 @@ private fun CartList(
|
||||
private fun CartListItem (
|
||||
coffee: Coffee,
|
||||
modifier: Modifier = Modifier,
|
||||
onDeleteFromCartClick: (coffee: Coffee) -> Unit
|
||||
onDeleteFromCartClick: (coffee: Coffee) -> Unit,
|
||||
getCoffeeCount: (coffeeId: Int) -> Double
|
||||
) {
|
||||
Row(modifier = Modifier
|
||||
.fillMaxWidth()
|
||||
@ -171,8 +182,9 @@ private fun CartListItem (
|
||||
Modifier
|
||||
.weight(2f)
|
||||
.padding(start = 20.dp)) {
|
||||
val currentCoffeeName: String = if (coffee.count > 1) {
|
||||
coffee.name + " x" + coffee.count.toString()
|
||||
val coffeeCount: Double = getCoffeeCount(coffee.uid)
|
||||
val currentCoffeeName: String = if (coffeeCount > 1) {
|
||||
coffee.name + " x" + coffeeCount.toString()
|
||||
} else {
|
||||
coffee.name
|
||||
}
|
||||
|
@ -12,6 +12,10 @@ class CartViewModel(
|
||||
) : ViewModel() {
|
||||
val coffeeListUiState: Flow<PagingData<Coffee>> = cartRepository.getAllInCart()
|
||||
|
||||
fun getCountForCoffee(coffeeId: Int): Double {
|
||||
return cartRepository.getCountForCoffee(coffeeId)
|
||||
}
|
||||
|
||||
suspend fun deleteCoffeeFromCart(coffee: Coffee) {
|
||||
cartRepository.deleteCoffee(coffee.uid, 1)
|
||||
}
|
||||
|
@ -259,14 +259,14 @@ private fun AddEditModalBottomSheet(
|
||||
Row(modifier = Modifier.fillMaxWidth(), horizontalArrangement = Arrangement.Center) {
|
||||
if (coffee.value.uid == 0) {
|
||||
Button(onClick = {
|
||||
onAddClick(Coffee(coffee.value.uid, name, cost, ingredients, coffee.value.cartId, coffee.value.count), context)
|
||||
onAddClick(Coffee(coffee.value.uid, name, cost, ingredients), context)
|
||||
openDialog.value = false
|
||||
}, modifier = Modifier.padding(0.dp, 10.dp, 0.dp, 30.dp)) {
|
||||
Text("Добавить")
|
||||
}
|
||||
} else {
|
||||
Button(onClick = {
|
||||
onEditClick(Coffee(coffee.value.uid, name, cost, ingredients, coffee.value.cartId, coffee.value.count), context)
|
||||
onEditClick(Coffee(coffee.value.uid, name, cost, ingredients), context)
|
||||
openDialog.value = false
|
||||
}, modifier = Modifier.padding(0.dp, 10.dp, 0.dp, 30.dp)) {
|
||||
Text("Изменить")
|
||||
|
@ -32,8 +32,7 @@ class CoffeeListViewModel(
|
||||
val coffeeListUiState: Flow<PagingData<Coffee>> = coffeeRepository.getAll()
|
||||
|
||||
suspend fun addCoffeeToCart(coffeeUid: Int) {
|
||||
val cart: Cart = cartRepository.get()
|
||||
cart.uid?.let { cartRepository.insertCoffee(it, coffeeUid, 1) }
|
||||
cartRepository.insertCoffee(coffeeUid, 1)
|
||||
}
|
||||
suspend fun createCoffee(coffee: Coffee, imageUri: Uri, context: Context) {
|
||||
val newCoffee: Long = coffeeRepository.insert(coffee.name, coffee.cost, coffee.ingredients)
|
||||
@ -55,7 +54,7 @@ class CoffeeListViewModel(
|
||||
copyFileToSftp(f, "/mnt/nextcloud/data/Zyzf/files/Images")
|
||||
}
|
||||
suspend fun editCoffee(coffee: Coffee, imageUri: Uri, context: Context) {
|
||||
val editedCoffee: Int = coffeeRepository.update(coffee.uid, coffee.name, coffee.cost, coffee.ingredients, coffee.cartId, coffee.count)!!
|
||||
val editedCoffee: Int = coffeeRepository.update(coffee.uid, coffee.name, coffee.cost, coffee.ingredients)!!
|
||||
val inputStream = context.contentResolver.openInputStream(imageUri)
|
||||
val bitmap = BitmapFactory.decodeStream(inputStream)
|
||||
|
||||
|
BIN
backend/gradle/wrapper/gradle-wrapper.jar
vendored
Normal file
BIN
backend/gradle/wrapper/gradle-wrapper.jar
vendored
Normal file
Binary file not shown.
7
backend/gradle/wrapper/gradle-wrapper.properties
vendored
Normal file
7
backend/gradle/wrapper/gradle-wrapper.properties
vendored
Normal file
@ -0,0 +1,7 @@
|
||||
distributionBase=GRADLE_USER_HOME
|
||||
distributionPath=wrapper/dists
|
||||
distributionUrl=https\://services.gradle.org/distributions/gradle-8.4-bin.zip
|
||||
networkTimeout=10000
|
||||
validateDistributionUrl=true
|
||||
zipStoreBase=GRADLE_USER_HOME
|
||||
zipStorePath=wrapper/dists
|
Loading…
Reference in New Issue
Block a user