refactor: move lesson 7 checkout orchestration into viewmodel
This commit is contained in:
1 parent
a773ac7867
commit
0e5eff673b
3 files changed
+62
-152
No files matched your search
@@ -1,31 +1,4 @@
|
||||
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}")
|
||||
}
|
||||
}
|
||||
@Deprecated("Lesson 7 moved checkout orchestration into ShopViewModel")
|
||||
class CheckoutService
|
||||
Reference in new issue
Block a user