refactor: move lesson 7 checkout orchestration into viewmodel
This commit is contained in:
1 parent
a773ac7867
commit
0e5eff673b
3 files changed
+60
-150
No files matched your search
@@ -6,7 +6,6 @@ import androidx.compose.runtime.setValue
|
|||||||
import androidx.lifecycle.ViewModel
|
import androidx.lifecycle.ViewModel
|
||||||
import com.example.minishop.cart.CartItem
|
import com.example.minishop.cart.CartItem
|
||||||
import com.example.minishop.cart.CartRepository
|
import com.example.minishop.cart.CartRepository
|
||||||
import com.example.minishop.checkout.CheckoutService
|
|
||||||
import com.example.minishop.db.createShopDatabase
|
import com.example.minishop.db.createShopDatabase
|
||||||
import com.example.minishop.model.Product
|
import com.example.minishop.model.Product
|
||||||
import com.example.minishop.network.ProductApi
|
import com.example.minishop.network.ProductApi
|
||||||
@@ -55,7 +54,6 @@ class ShopViewModel : ViewModel() {
|
|||||||
private val database = createShopDatabase()
|
private val database = createShopDatabase()
|
||||||
private val cartRepository = CartRepository(database)
|
private val cartRepository = CartRepository(database)
|
||||||
private val orderRepository = OrderRepository(database)
|
private val orderRepository = OrderRepository(database)
|
||||||
private val checkoutService = CheckoutService(cartRepository, orderRepository)
|
|
||||||
|
|
||||||
var uiState by mutableStateOf(ShopUiState())
|
var uiState by mutableStateOf(ShopUiState())
|
||||||
private set
|
private set
|
||||||
@@ -167,13 +165,19 @@ class ShopViewModel : ViewModel() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fun checkout(): String {
|
fun checkout(): String {
|
||||||
val result = checkoutService.checkout()
|
val cartItems = cartRepository.items()
|
||||||
|
if (cartItems.isEmpty()) {
|
||||||
|
return "购物车为空"
|
||||||
|
}
|
||||||
|
|
||||||
|
val order = orderRepository.createOrder(cartItems)
|
||||||
|
?: return "创建订单失败"
|
||||||
|
|
||||||
|
cartRepository.clear()
|
||||||
syncCart()
|
syncCart()
|
||||||
syncOrders()
|
syncOrders()
|
||||||
if (result.success) {
|
|
||||||
uiState = uiState.copy(currentTab = Tab.Orders)
|
uiState = uiState.copy(currentTab = Tab.Orders)
|
||||||
}
|
return "下单成功,订单号:${order.orderId}"
|
||||||
return result.message
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun syncCart() {
|
private fun syncCart() {
|
||||||
@@ -181,7 +185,7 @@ class ShopViewModel : ViewModel() {
|
|||||||
uiState = uiState.copy(
|
uiState = uiState.copy(
|
||||||
cartItems = items,
|
cartItems = items,
|
||||||
totalCount = items.sumOf { it.quantity },
|
totalCount = items.sumOf { it.quantity },
|
||||||
totalPrice = checkoutService.totalPrice()
|
totalPrice = items.sumOf { it.price * it.quantity }
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+47
-114
@@ -777,10 +777,9 @@ ordersQueries
|
|||||||
5. `shared/src/commonMain/kotlin/com/example/minishop/cart/CartItem.kt`
|
5. `shared/src/commonMain/kotlin/com/example/minishop/cart/CartItem.kt`
|
||||||
6. `shared/src/commonMain/kotlin/com/example/minishop/cart/CartRepository.kt`
|
6. `shared/src/commonMain/kotlin/com/example/minishop/cart/CartRepository.kt`
|
||||||
7. `shared/src/commonMain/kotlin/com/example/minishop/cart/CartStorage.kt`
|
7. `shared/src/commonMain/kotlin/com/example/minishop/cart/CartStorage.kt`
|
||||||
8. `shared/src/commonMain/kotlin/com/example/minishop/checkout/CheckoutService.kt`
|
8. `composeApp/src/androidMain/kotlin/com/example/minishop/MainActivity.kt`
|
||||||
9. `composeApp/src/androidMain/kotlin/com/example/minishop/MainActivity.kt`
|
9. `composeApp/src/androidMain/kotlin/com/example/minishop/ShopViewModel.kt`
|
||||||
10. `composeApp/src/androidMain/kotlin/com/example/minishop/ShopViewModel.kt`
|
10. `composeApp/src/androidMain/kotlin/com/example/minishop/App.kt`
|
||||||
11. `composeApp/src/androidMain/kotlin/com/example/minishop/App.kt`
|
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
@@ -1075,59 +1074,24 @@ class CartStorage
|
|||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
## Step 4.5:把 `CheckoutService` 改成适配 SQL 版购物车
|
## Step 4.5:把总价计算收回 `ShopViewModel`
|
||||||
|
|
||||||
### 为什么这里也要改
|
### 为什么这里也要改
|
||||||
第6节 `CheckoutService` 的总价计算,是通过:
|
当前这节课里,下单逻辑和总价计算都只服务于当前页面流程,
|
||||||
- `productMap[productId]`
|
如果单独抽一个 `CheckoutService`,反而会让分层显得有点虚。
|
||||||
- 再去拿商品价格
|
|
||||||
|
|
||||||
但现在 `CartItem` 本身已经有:
|
所以第7节这里更自然的做法是:
|
||||||
- `price`
|
- `Repository` 只负责数据访问
|
||||||
- `title`
|
- `ShopViewModel` 直接负责编排总价计算和结算动作
|
||||||
- `imageUrl`
|
|
||||||
|
|
||||||
所以总价计算就没必要再绕回商品列表。
|
这样对学生来说更直观,也更符合“教学最小闭环”。
|
||||||
|
|
||||||
### 文件路径
|
### 课堂说明
|
||||||
`shared/src/commonMain/kotlin/com/example/minishop/checkout/CheckoutService.kt`
|
你可以直接告诉学生:
|
||||||
|
- 现在业务还很轻
|
||||||
### 完整代码
|
- 只有一个页面在用结算逻辑
|
||||||
|
- 所以先不额外抽 Service / UseCase
|
||||||
```kotlin
|
- 等以后有库存、优惠券、支付等更复杂逻辑时,再考虑上移成独立 UseCase
|
||||||
package com.example.minishop.checkout
|
|
||||||
|
|
||||||
import com.example.minishop.cart.CartRepository
|
|
||||||
import kotlin.random.Random
|
|
||||||
|
|
||||||
data class CheckoutResult(
|
|
||||||
val success: Boolean,
|
|
||||||
val message: String
|
|
||||||
)
|
|
||||||
|
|
||||||
class CheckoutService(
|
|
||||||
private val cartRepository: CartRepository
|
|
||||||
) {
|
|
||||||
fun totalPrice(): Double {
|
|
||||||
return cartRepository.items().sumOf { item ->
|
|
||||||
item.price * item.quantity
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fun checkout(): CheckoutResult {
|
|
||||||
val total = totalPrice()
|
|
||||||
if (total <= 0.0) return CheckoutResult(false, "购物车为空")
|
|
||||||
|
|
||||||
val success = Random.nextBoolean()
|
|
||||||
return if (success) {
|
|
||||||
cartRepository.clear()
|
|
||||||
CheckoutResult(true, "结算成功(模拟),金额:¥${"%.2f".format(total)}")
|
|
||||||
} else {
|
|
||||||
CheckoutResult(false, "结算失败(模拟),请重试")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
```
|
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
@@ -1168,7 +1132,7 @@ class MainActivity : ComponentActivity() {
|
|||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
## Step 4.7:让 `ShopViewModel` 接 SQL 版购物车
|
## Step 4.7:让 `ShopViewModel` 接 SQL 版购物车并自己计算总价
|
||||||
|
|
||||||
### 为什么 ViewModel 也得改
|
### 为什么 ViewModel 也得改
|
||||||
因为 `CartRepository` 的构造方式已经变了:
|
因为 `CartRepository` 的构造方式已经变了:
|
||||||
@@ -1193,7 +1157,6 @@ import androidx.compose.runtime.setValue
|
|||||||
import androidx.lifecycle.ViewModel
|
import androidx.lifecycle.ViewModel
|
||||||
import com.example.minishop.cart.CartItem
|
import com.example.minishop.cart.CartItem
|
||||||
import com.example.minishop.cart.CartRepository
|
import com.example.minishop.cart.CartRepository
|
||||||
import com.example.minishop.checkout.CheckoutService
|
|
||||||
import com.example.minishop.db.createShopDatabase
|
import com.example.minishop.db.createShopDatabase
|
||||||
import com.example.minishop.model.Product
|
import com.example.minishop.model.Product
|
||||||
import com.example.minishop.network.ProductApi
|
import com.example.minishop.network.ProductApi
|
||||||
@@ -1225,7 +1188,6 @@ class ShopViewModel : ViewModel() {
|
|||||||
)
|
)
|
||||||
private val database = createShopDatabase()
|
private val database = createShopDatabase()
|
||||||
private val cartRepository = CartRepository(database)
|
private val cartRepository = CartRepository(database)
|
||||||
private val checkoutService = CheckoutService(cartRepository)
|
|
||||||
|
|
||||||
var uiState by mutableStateOf(ShopUiState())
|
var uiState by mutableStateOf(ShopUiState())
|
||||||
private set
|
private set
|
||||||
@@ -1309,9 +1271,7 @@ class ShopViewModel : ViewModel() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fun checkout(): String {
|
fun checkout(): String {
|
||||||
val result = checkoutService.checkout()
|
return "第4步这里先不真正下单,第5步再接订单闭环"
|
||||||
syncCart()
|
|
||||||
return result.message
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun syncCart() {
|
private fun syncCart() {
|
||||||
@@ -1319,7 +1279,7 @@ class ShopViewModel : ViewModel() {
|
|||||||
uiState = uiState.copy(
|
uiState = uiState.copy(
|
||||||
cartItems = items,
|
cartItems = items,
|
||||||
totalCount = items.sumOf { it.quantity },
|
totalCount = items.sumOf { it.quantity },
|
||||||
totalPrice = checkoutService.totalPrice()
|
totalPrice = items.sumOf { it.price * it.quantity }
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1626,58 +1586,27 @@ class OrderRepository(
|
|||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
## Step 5.3:`CheckoutService` 从“模拟结算”升级成“生成订单”
|
## Step 5.3:让 `ShopViewModel` 直接编排下单流程
|
||||||
|
|
||||||
### 为什么这里要改
|
### 为什么这里不再保留 `CheckoutService`
|
||||||
第6节/Step4 的 `CheckoutService` 还停留在:
|
到这一步我们重新梳理一下分层,会发现:
|
||||||
- 随机成功
|
- `CartRepository` 负责购物车数据
|
||||||
- 随机失败
|
- `OrderRepository` 负责订单数据
|
||||||
- 成功就清购物车
|
- 下单逻辑目前只服务于当前页面流程
|
||||||
|
|
||||||
这一步要改成更像真实业务的最小流程:
|
如果这里再夹一层很薄的 `CheckoutService`,反而会让学生多记一个抽象。
|
||||||
- 读取购物车
|
|
||||||
- 创建订单
|
|
||||||
- 清空购物车
|
|
||||||
- 返回订单号
|
|
||||||
|
|
||||||
### 文件路径
|
所以第7节里更自然的方案是:
|
||||||
`shared/src/commonMain/kotlin/com/example/minishop/checkout/CheckoutService.kt`
|
- `Repository` 负责数据访问
|
||||||
|
- `ShopViewModel` 直接负责编排:
|
||||||
|
- 读取购物车
|
||||||
|
- 创建订单
|
||||||
|
- 清空购物车
|
||||||
|
- 刷新订单列表
|
||||||
|
- 切换到订单页
|
||||||
|
|
||||||
### 完整代码
|
### 课堂结论
|
||||||
|
> 当前业务还不复杂时,直接放在 ViewModel 里会比额外抽一个薄 Service 更自然。
|
||||||
```kotlin
|
|
||||||
package com.example.minishop.checkout
|
|
||||||
|
|
||||||
import com.example.minishop.cart.CartRepository
|
|
||||||
import com.example.minishop.order.OrderRepository
|
|
||||||
|
|
||||||
data class CheckoutResult(
|
|
||||||
val success: Boolean,
|
|
||||||
val message: String
|
|
||||||
)
|
|
||||||
|
|
||||||
class CheckoutService(
|
|
||||||
private val cartRepository: CartRepository,
|
|
||||||
private val orderRepository: OrderRepository
|
|
||||||
) {
|
|
||||||
fun totalPrice(): Double {
|
|
||||||
return cartRepository.items().sumOf { item ->
|
|
||||||
item.price * item.quantity
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fun checkout(): CheckoutResult {
|
|
||||||
val cartItems = cartRepository.items()
|
|
||||||
if (cartItems.isEmpty()) return CheckoutResult(false, "购物车为空")
|
|
||||||
|
|
||||||
val order = orderRepository.createOrder(cartItems)
|
|
||||||
?: return CheckoutResult(false, "创建订单失败")
|
|
||||||
|
|
||||||
cartRepository.clear()
|
|
||||||
return CheckoutResult(true, "下单成功,订单号:${order.orderId}")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
```
|
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
@@ -1713,7 +1642,6 @@ import androidx.compose.runtime.setValue
|
|||||||
import androidx.lifecycle.ViewModel
|
import androidx.lifecycle.ViewModel
|
||||||
import com.example.minishop.cart.CartItem
|
import com.example.minishop.cart.CartItem
|
||||||
import com.example.minishop.cart.CartRepository
|
import com.example.minishop.cart.CartRepository
|
||||||
import com.example.minishop.checkout.CheckoutService
|
|
||||||
import com.example.minishop.db.createShopDatabase
|
import com.example.minishop.db.createShopDatabase
|
||||||
import com.example.minishop.model.Product
|
import com.example.minishop.model.Product
|
||||||
import com.example.minishop.network.ProductApi
|
import com.example.minishop.network.ProductApi
|
||||||
@@ -1749,7 +1677,6 @@ class ShopViewModel : ViewModel() {
|
|||||||
private val database = createShopDatabase()
|
private val database = createShopDatabase()
|
||||||
private val cartRepository = CartRepository(database)
|
private val cartRepository = CartRepository(database)
|
||||||
private val orderRepository = OrderRepository(database)
|
private val orderRepository = OrderRepository(database)
|
||||||
private val checkoutService = CheckoutService(cartRepository, orderRepository)
|
|
||||||
|
|
||||||
var uiState by mutableStateOf(ShopUiState())
|
var uiState by mutableStateOf(ShopUiState())
|
||||||
private set
|
private set
|
||||||
@@ -1834,13 +1761,19 @@ class ShopViewModel : ViewModel() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fun checkout(): String {
|
fun checkout(): String {
|
||||||
val result = checkoutService.checkout()
|
val cartItems = cartRepository.items()
|
||||||
|
if (cartItems.isEmpty()) {
|
||||||
|
return "购物车为空"
|
||||||
|
}
|
||||||
|
|
||||||
|
val order = orderRepository.createOrder(cartItems)
|
||||||
|
?: return "创建订单失败"
|
||||||
|
|
||||||
|
cartRepository.clear()
|
||||||
syncCart()
|
syncCart()
|
||||||
syncOrders()
|
syncOrders()
|
||||||
if (result.success) {
|
|
||||||
uiState = uiState.copy(currentTab = Tab.Orders)
|
uiState = uiState.copy(currentTab = Tab.Orders)
|
||||||
}
|
return "下单成功,订单号:${order.orderId}"
|
||||||
return result.message
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun syncCart() {
|
private fun syncCart() {
|
||||||
@@ -1848,7 +1781,7 @@ class ShopViewModel : ViewModel() {
|
|||||||
uiState = uiState.copy(
|
uiState = uiState.copy(
|
||||||
cartItems = items,
|
cartItems = items,
|
||||||
totalCount = items.sumOf { it.quantity },
|
totalCount = items.sumOf { it.quantity },
|
||||||
totalPrice = checkoutService.totalPrice()
|
totalPrice = items.sumOf { it.price * it.quantity }
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,31 +1,4 @@
|
|||||||
package com.example.minishop.checkout
|
package com.example.minishop.checkout
|
||||||
|
|
||||||
import com.example.minishop.cart.CartRepository
|
@Deprecated("Lesson 7 moved checkout orchestration into ShopViewModel")
|
||||||
import com.example.minishop.order.OrderRepository
|
class CheckoutService
|
||||||
|
|
||||||
data class CheckoutResult(
|
|
||||||
val success: Boolean,
|
|
||||||
val message: String
|
|
||||||
)
|
|
||||||
|
|
||||||
class CheckoutService(
|
|
||||||
private val cartRepository: CartRepository,
|
|
||||||
private val orderRepository: OrderRepository
|
|
||||||
) {
|
|
||||||
fun totalPrice(): Double {
|
|
||||||
return cartRepository.items().sumOf { item ->
|
|
||||||
item.price * item.quantity
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fun checkout(): CheckoutResult {
|
|
||||||
val cartItems = cartRepository.items()
|
|
||||||
if (cartItems.isEmpty()) return CheckoutResult(false, "购物车为空")
|
|
||||||
|
|
||||||
val order = orderRepository.createOrder(cartItems)
|
|
||||||
?: return CheckoutResult(false, "创建订单失败")
|
|
||||||
|
|
||||||
cartRepository.clear()
|
|
||||||
return CheckoutResult(true, "下单成功,订单号:${order.orderId}")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Reference in new issue
Block a user