11package com.patika.getir_lite.fake
22
3- import com.patika.getir_lite.data.DataSyncResult
43import com.patika.getir_lite.data.ProductRepository
54import com.patika.getir_lite.data.local.model.ItemEntity
65import com.patika.getir_lite.data.local.model.ItemWithProduct
@@ -10,63 +9,53 @@ import com.patika.getir_lite.data.local.model.ProductEntity
109import com.patika.getir_lite.data.local.model.toDomainModel
1110import com.patika.getir_lite.fake.data.fakeProductWithCounts
1211import com.patika.getir_lite.model.BaseResponse
13- import com.patika.getir_lite.model.BasketWithProducts
12+ import com.patika.getir_lite.data.local.model.BasketWithProducts
13+ import com.patika.getir_lite.data.local.model.StatusEntity
1414import com.patika.getir_lite.model.CountType
1515import com.patika.getir_lite.model.Order
1616import com.patika.getir_lite.model.ProductType
1717import com.patika.getir_lite.model.ProductWithCount
1818import kotlinx.coroutines.flow.Flow
19- import kotlinx.coroutines.flow.MutableStateFlow
20- import kotlinx.coroutines.flow.StateFlow
2119import kotlinx.coroutines.flow.flow
2220import java.math.BigDecimal
2321
22+ /* *
23+ * A mock implementation of [ProductRepository] for use in tests or development environments.
24+ * This class simulates the behavior of a real product repository by storing and managing in-memory lists of products and orders.
25+ */
2426class FakeProductDataSource : ProductRepository {
2527 private val localProducts = fakeProductWithCounts.toMutableList()
2628 val orders = mutableListOf<OrderEntity >()
2729 private val items = mutableListOf<ItemEntity >()
2830
29- private val _dataSyncResult = MutableStateFlow <List <DataSyncResult >>(emptyList())
30- override val dataSyncResult: StateFlow <List <DataSyncResult >> = _dataSyncResult
31-
32- override suspend fun syncWithRemote (): BaseResponse <Unit > {
31+ override suspend fun fetchDataFromRemote (): BaseResponse <Unit > {
3332 return BaseResponse .Success (Unit )
3433 }
3534
3635 override fun getProductsAsFlow (): Flow <List <ProductWithCount >> = flow {
37- emit(localProducts.filter { it.productType == ProductType .PRODUCT })
36+ emit(localProducts.filter { it.type == ProductType .PRODUCT })
3837 }
3938
4039 override fun getSuggestedProductsAsFlow (): Flow <List <ProductWithCount >> = flow {
41- emit(localProducts.filter { it.productType == ProductType .SUGGESTED_PRODUCT })
40+ emit(localProducts.filter { it.type == ProductType .SUGGESTED_PRODUCT })
4241 }
4342
4443 override fun getBasketAsFlow (): Flow <Order ?> = flow {
4544 emit(orders.firstOrNull { it.orderStatus == OrderStatus .ON_BASKET }?.toDomainModel())
4645 }
4746
48- fun ProductWithCount.toProductEntity (): ProductEntity {
49- return ProductEntity (
50- id = productId,
51- productId = " " ,
52- name = name,
53- price = price,
54- attribute = attribute ? : " " ,
55- imageURL = imageURL,
56- productType = productType,
57- )
58- }
59-
6047 override fun getBasketWithProductsAsFlow (): Flow <BasketWithProducts ?> = flow {
6148 val order = orders.firstOrNull { it.orderStatus == OrderStatus .ON_BASKET }
6249 ? : return @flow emit(null )
50+
6351 val items = items.filter { it.orderId == order.id }
6452 val products = localProducts.filter { product ->
6553 items.any { it.productId == product.productId }
6654 }
55+
6756 val itemWithProducts = items.map { item ->
6857 val product = products.find { it.productId == item.productId }
69- ? : throw Exception (" Product not found" )
58+ ? : throw Exception (" Product not found: ${item.productId} " )
7059 ItemWithProduct (item, product.toProductEntity())
7160 }
7261 emit(BasketWithProducts (order, itemWithProducts))
@@ -96,22 +85,24 @@ class FakeProductDataSource : ProductRepository {
9685 items.add(item)
9786 }
9887
99- val order = orders.find { it.id == orderId } ? : throw Exception (" Order not found" )
88+ val order = orders.find { it.id == orderId } ? : throw Exception (" Order not found: $orderId " )
10089 val updatedOrder =
10190 order.copy(totalPrice = (order.totalPrice.toDouble() + price.toDouble()).toBigDecimal())
10291 val orderIndex = orders.indexOf(order)
10392 orders[orderIndex] = updatedOrder
10493
10594 val product =
106- localProducts.find { it.productId == productId } ? : throw Exception (" Product not found" )
95+ localProducts.find { it.productId == productId }
96+ ? : throw Exception (" Product not found $productId " )
10797 val updatedProduct = product.copy(count = product.count + 1 )
10898 val productIndex = localProducts.indexOf(product)
10999 localProducts[productIndex] = updatedProduct
110100 }
111101
112102 private fun decrementItemCount (productId : Long , orderId : Long , price : BigDecimal ) {
113103 val itemEntity =
114- getItemByProductAndOrder(productId, orderId) ? : throw Exception (" Item not found" )
104+ getItemByProductAndOrder(productId, orderId)
105+ ? : throw Exception (" Item not found: productId=$productId , orderId=$orderId " )
115106 val c = itemEntity.count - 1
116107 val updatedItem = itemEntity.copy(count = c)
117108 val index = items.indexOf(itemEntity)
@@ -121,7 +112,7 @@ class FakeProductDataSource : ProductRepository {
121112 items.removeAt(index)
122113 }
123114
124- val order = orders.find { it.id == orderId } ? : throw Exception (" Order not found" )
115+ val order = orders.find { it.id == orderId } ? : throw Exception (" Order not found: $orderId " )
125116 val updatedOrder =
126117 order.copy(totalPrice = (order.totalPrice.toDouble() - price.toDouble()).toBigDecimal())
127118 val orderIndex = orders.indexOf(order)
@@ -134,10 +125,10 @@ class FakeProductDataSource : ProductRepository {
134125 localProducts[productIndex] = updatedProduct
135126 }
136127
137- override suspend fun updateItemCount (productId : Long , countType : CountType ) {
128+ override suspend fun updateItemCount (productId : Long , countType : CountType ) = runCatching {
138129 val getActiveOrder = orders.firstOrNull { it.orderStatus == OrderStatus .ON_BASKET }
139130 val productPrice = localProducts.find { it.productId == productId }?.price
140- ? : throw Exception (" Product not found" )
131+ ? : throw Exception (" Product not found: $productId " )
141132 val orderId = getActiveOrder?.id ? : run {
142133 val id = (0 .. 100000 ).random().toLong()
143134 val orderEntity = OrderEntity (id = id, OrderStatus .ON_BASKET )
@@ -171,4 +162,20 @@ class FakeProductDataSource : ProductRepository {
171162
172163 return true
173164 }
165+
166+ override suspend fun getStatus (): List <StatusEntity ?> {
167+ return emptyList()
168+ }
169+
170+ private fun ProductWithCount.toProductEntity (): ProductEntity {
171+ return ProductEntity (
172+ id = productId,
173+ productId = " " ,
174+ name = name,
175+ price = price,
176+ attribute = attribute ? : " " ,
177+ imageURL = imageURL,
178+ productType = type,
179+ )
180+ }
174181}
0 commit comments