Skip to content

Commit cac48bb

Browse files
committed
refactor: CoroutineExceptionHandler 적용
1 parent 5ddd01f commit cac48bb

33 files changed

+432
-522
lines changed

app/src/main/java/woowacourse/shopping/data/repository/CartRepository.kt

Lines changed: 22 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -14,47 +14,40 @@ class CartRepository(
1414
override suspend fun fetchCartProducts(
1515
page: Int,
1616
size: Int,
17-
): Result<Products> =
18-
runCatching {
19-
api.getCartItems(page, size)
20-
}.mapCatching { response ->
21-
val items = response.content.map { it.toDomain() }
22-
val pageInfo = Page(page, response.first, response.last)
23-
Products(items, pageInfo)
24-
}
25-
26-
override suspend fun fetchAllCartProducts(): Result<Products> {
17+
): Products {
18+
val response = api.getCartItems(page, size)
19+
val items = response.content.map { it.toDomain() }
20+
val pageInfo = Page(page, response.first, response.last)
21+
return Products(items, pageInfo)
22+
}
23+
24+
override suspend fun fetchAllCartProducts(): Products {
2725
val firstPage = 0
2826
val maxSize = Int.MAX_VALUE
2927

3028
return fetchCartProducts(firstPage, maxSize)
3129
}
3230

33-
override suspend fun fetchCartItemCount(): Result<Int> =
34-
runCatching {
35-
api.getCartItemsCount()
36-
}.mapCatching { response ->
37-
response.quantity
38-
}
31+
override suspend fun fetchCartItemCount(): Int {
32+
val response = api.getCartItemsCount()
33+
return response.quantity
34+
}
3935

4036
override suspend fun addCartProduct(
4137
productId: Long,
4238
quantity: Int,
43-
): Result<Unit> =
44-
runCatching {
45-
api.postCartItem(CartItemRequest(productId = productId, quantity = quantity))
46-
}
39+
) {
40+
api.postCartItem(CartItemRequest(productId = productId, quantity = quantity))
41+
}
4742

4843
override suspend fun updateCartProduct(
4944
cartId: Long,
5045
quantity: Int,
51-
): Result<Unit> =
52-
runCatching {
53-
api.patchCartItem(cartId, CartItemQuantityRequest(quantity))
54-
}
55-
56-
override suspend fun deleteCartProduct(cartId: Long): Result<Unit> =
57-
runCatching {
58-
api.deleteCartItem(cartId)
59-
}
46+
) {
47+
api.patchCartItem(cartId, CartItemQuantityRequest(quantity))
48+
}
49+
50+
override suspend fun deleteCartProduct(cartId: Long) {
51+
api.deleteCartItem(cartId)
52+
}
6053
}

app/src/main/java/woowacourse/shopping/data/repository/CouponRepository.kt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ import woowacourse.shopping.domain.repository.CouponRepository
99
class CouponRepository(
1010
private val api: CouponApi,
1111
) : CouponRepository {
12-
override suspend fun fetchAllCoupons(): Result<List<Coupon>> =
13-
runCatching {
14-
api.getCoupons().mapNotNull { it.toDomain()?.toCoupon() }
15-
}
12+
override suspend fun fetchAllCoupons(): List<Coupon> =
13+
api
14+
.getCoupons()
15+
.mapNotNull { it.toDomain()?.toCoupon() }
1616
}

app/src/main/java/woowacourse/shopping/data/repository/HistoryRepository.kt

Lines changed: 14 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -10,30 +10,23 @@ import woowacourse.shopping.domain.repository.HistoryRepository
1010
class HistoryRepository(
1111
private val dao: HistoryDao,
1212
) : HistoryRepository {
13-
override suspend fun fetchAllHistory(): Result<List<HistoryProduct>> =
14-
runCatching {
15-
dao.getHistoryProducts().map { it.toDomain() }
16-
}
13+
override suspend fun fetchAllHistory(): List<HistoryProduct> = dao.getHistoryProducts().map { it.toDomain() }
1714

18-
override suspend fun fetchRecentHistory(): Result<HistoryProduct?> =
19-
runCatching {
20-
dao.getRecentHistoryProduct()?.toDomain()
21-
}
15+
override suspend fun fetchRecentHistory(): HistoryProduct? = dao.getRecentHistoryProduct()?.toDomain()
2216

2317
override suspend fun addHistoryWithLimit(
2418
productDetail: ProductDetail,
2519
limit: Int,
26-
): Result<Unit> =
27-
runCatching {
28-
dao.insertHistoryWithLimit(
29-
history =
30-
HistoryProductEntity(
31-
productId = productDetail.id,
32-
name = productDetail.name,
33-
imageUrl = productDetail.imageUrl,
34-
category = productDetail.category,
35-
),
36-
limit = limit,
37-
)
38-
}
20+
) {
21+
dao.insertHistoryWithLimit(
22+
history =
23+
HistoryProductEntity(
24+
productId = productDetail.id,
25+
name = productDetail.name,
26+
imageUrl = productDetail.imageUrl,
27+
category = productDetail.category,
28+
),
29+
limit = limit,
30+
)
31+
}
3932
}

app/src/main/java/woowacourse/shopping/data/repository/OrderRepository.kt

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,7 @@ import woowacourse.shopping.domain.repository.OrderRepository
77
class OrderRepository(
88
private val api: OrderApi,
99
) : OrderRepository {
10-
override suspend fun postOrderProducts(cartIds: List<Long>): Result<Unit> =
11-
runCatching {
12-
api.postOrderProducts(OrderProductsRequest(cartIds))
13-
}
10+
override suspend fun postOrderProducts(cartIds: List<Long>) {
11+
api.postOrderProducts(OrderProductsRequest(cartIds))
12+
}
1413
}

app/src/main/java/woowacourse/shopping/data/repository/ProductRepository.kt

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -16,25 +16,19 @@ class ProductRepository(
1616
page: Int,
1717
size: Int,
1818
category: String?,
19-
): Result<Products> =
20-
runCatching {
21-
api.getProducts(category, page, size)
22-
}.mapCatching { response ->
23-
val items = response.content.map { it.toDomain() }
24-
val pageInfo = Page(page, response.first, response.last)
25-
Products(items, pageInfo)
26-
}
19+
): Products {
20+
val response = api.getProducts(category, page, size)
21+
val items = response.content.map { it.toDomain() }
22+
val pageInfo = Page(page, response.first, response.last)
23+
return Products(items, pageInfo)
24+
}
2725

28-
override suspend fun fetchAllProducts(): Result<List<Product>> {
26+
override suspend fun fetchAllProducts(): List<Product> {
2927
val firstPage = 0
3028
val maxSize = Int.MAX_VALUE
31-
32-
return runCatching {
33-
api.getProducts(page = firstPage, size = maxSize)
34-
}.mapCatching { response ->
35-
response.content.map { it.toDomain() }
36-
}
29+
val response = api.getProducts(page = firstPage, size = maxSize)
30+
return response.content.map { it.toDomain() }
3731
}
3832

39-
override suspend fun fetchProduct(productId: Long): Result<ProductDetail> = runCatching { api.getProduct(productId).toDomain() }
33+
override suspend fun fetchProduct(productId: Long): ProductDetail = api.getProduct(productId).toDomain()
4034
}

app/src/main/java/woowacourse/shopping/domain/repository/CartRepository.kt

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,21 @@ interface CartRepository {
66
suspend fun fetchCartProducts(
77
page: Int,
88
size: Int,
9-
): Result<Products>
9+
): Products
1010

11-
suspend fun fetchAllCartProducts(): Result<Products>
11+
suspend fun fetchAllCartProducts(): Products
1212

13-
suspend fun fetchCartItemCount(): Result<Int>
13+
suspend fun fetchCartItemCount(): Int
1414

1515
suspend fun addCartProduct(
1616
productId: Long,
1717
quantity: Int,
18-
): Result<Unit>
18+
): Unit
1919

20-
suspend fun deleteCartProduct(cartId: Long): Result<Unit>
20+
suspend fun deleteCartProduct(cartId: Long): Unit
2121

2222
suspend fun updateCartProduct(
2323
cartId: Long,
2424
quantity: Int,
25-
): Result<Unit>
25+
): Unit
2626
}

app/src/main/java/woowacourse/shopping/domain/repository/CouponRepository.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,5 @@ package woowacourse.shopping.domain.repository
33
import woowacourse.shopping.domain.model.Coupon
44

55
interface CouponRepository {
6-
suspend fun fetchAllCoupons(): Result<List<Coupon>>
6+
suspend fun fetchAllCoupons(): List<Coupon>
77
}

app/src/main/java/woowacourse/shopping/domain/repository/HistoryRepository.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@ import woowacourse.shopping.domain.model.HistoryProduct
44
import woowacourse.shopping.domain.model.ProductDetail
55

66
interface HistoryRepository {
7-
suspend fun fetchAllHistory(): Result<List<HistoryProduct>>
7+
suspend fun fetchAllHistory(): List<HistoryProduct>
88

9-
suspend fun fetchRecentHistory(): Result<HistoryProduct?>
9+
suspend fun fetchRecentHistory(): HistoryProduct?
1010

1111
suspend fun addHistoryWithLimit(
1212
productDetail: ProductDetail,
1313
limit: Int,
14-
): Result<Unit>
14+
): Unit
1515
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
package woowacourse.shopping.domain.repository
22

33
interface OrderRepository {
4-
suspend fun postOrderProducts(cartIds: List<Long>): Result<Unit>
4+
suspend fun postOrderProducts(cartIds: List<Long>): Unit
55
}

app/src/main/java/woowacourse/shopping/domain/repository/ProductRepository.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@ interface ProductRepository {
99
page: Int,
1010
size: Int,
1111
category: String? = null,
12-
): Result<Products>
12+
): Products
1313

14-
suspend fun fetchAllProducts(): Result<List<Product>>
14+
suspend fun fetchAllProducts(): List<Product>
1515

16-
suspend fun fetchProduct(productId: Long): Result<ProductDetail>
16+
suspend fun fetchProduct(productId: Long): ProductDetail
1717
}

app/src/main/java/woowacourse/shopping/domain/usecase/AddSearchHistoryUseCase.kt

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,12 @@ import woowacourse.shopping.domain.repository.HistoryRepository
77
class AddSearchHistoryUseCase(
88
private val repository: HistoryRepository,
99
) {
10-
suspend operator fun invoke(productDetail: ProductDetail): Result<Unit> {
11-
if (productDetail == EMPTY_PRODUCT_DETAIL) return Result.failure(Throwable("[AddSearchHistoryUseCase] 유효하지 않은 상품"))
12-
return repository.addHistoryWithLimit(productDetail, MAX_HISTORY_COUNT)
10+
suspend operator fun invoke(productDetail: ProductDetail) {
11+
if (productDetail == EMPTY_PRODUCT_DETAIL) {
12+
throw IllegalArgumentException("[AddSearchHistoryUseCase] 유효하지 않은 상품입니다.")
13+
}
14+
15+
repository.addHistoryWithLimit(productDetail, MAX_HISTORY_COUNT)
1316
}
1417

1518
companion object {

app/src/main/java/woowacourse/shopping/domain/usecase/DecreaseCartProductQuantityUseCase.kt

Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -10,31 +10,20 @@ class DecreaseCartProductQuantityUseCase(
1010
suspend operator fun invoke(
1111
product: Product,
1212
step: Int = DEFAULT_QUANTITY_STEP,
13-
): Result<Int> {
14-
if (product.cartId == null) return Result.failure(Throwable("[DecreaseCartProductQuantityUseCase] 유효하지 않은 상품"))
13+
): Int {
14+
val cartId = product.cartId ?: throw IllegalArgumentException("[DecreaseCartProductQuantityUseCase] 유효하지 않은 상품")
1515

1616
val newQuantity = (product.quantity - step).coerceAtLeast(MINIMUM_QUANTITY)
1717

1818
return if (newQuantity <= MINIMUM_QUANTITY) {
19-
deleteCartProduct(product.cartId)
19+
repository.deleteCartProduct(cartId)
20+
MINIMUM_QUANTITY
2021
} else {
21-
updateCartProduct(product.cartId, newQuantity)
22+
repository.updateCartProduct(cartId, newQuantity)
23+
newQuantity
2224
}
2325
}
2426

25-
private suspend fun deleteCartProduct(cartId: Long): Result<Int> =
26-
repository
27-
.deleteCartProduct(cartId)
28-
.map { MINIMUM_QUANTITY }
29-
30-
private suspend fun updateCartProduct(
31-
cartId: Long,
32-
newQuantity: Int,
33-
): Result<Int> =
34-
repository
35-
.updateCartProduct(cartId, newQuantity)
36-
.map { newQuantity }
37-
3827
companion object {
3928
private const val DEFAULT_QUANTITY_STEP = 1
4029
}

app/src/main/java/woowacourse/shopping/domain/usecase/GetCartProductsQuantityUseCase.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,5 @@ import woowacourse.shopping.domain.repository.CartRepository
55
class GetCartProductsQuantityUseCase(
66
private val repository: CartRepository,
77
) {
8-
suspend operator fun invoke(): Result<Int> = repository.fetchCartItemCount()
8+
suspend operator fun invoke(): Int = repository.fetchCartItemCount()
99
}

app/src/main/java/woowacourse/shopping/domain/usecase/GetCartProductsUseCase.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,5 @@ class GetCartProductsUseCase(
99
suspend operator fun invoke(
1010
page: Int,
1111
size: Int,
12-
): Result<Products> = repository.fetchCartProducts(page, size)
12+
): Products = repository.fetchCartProducts(page, size)
1313
}

app/src/main/java/woowacourse/shopping/domain/usecase/GetCartRecommendProductsUseCase.kt

Lines changed: 11 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -10,43 +10,31 @@ class GetCartRecommendProductsUseCase(
1010
private val cartRepository: CartRepository,
1111
private val historyRepository: HistoryRepository,
1212
) {
13-
suspend operator fun invoke(): Result<Products> {
14-
val recent =
15-
historyRepository
16-
.fetchRecentHistory()
17-
.getOrElse {
18-
return Result.failure(Throwable("[GetCartRecommendProductsUseCase] 최근 방문 기록 오류", it))
19-
}
20-
13+
suspend operator fun invoke(): Products {
14+
val recent = historyRepository.fetchRecentHistory()
2115
return filterProductsByCategory(recent?.category)
2216
}
2317

24-
private suspend fun filterProductsByCategory(category: String?): Result<Products> {
18+
private suspend fun filterProductsByCategory(category: String?): Products {
2519
val products =
26-
productRepository
27-
.fetchProducts(0, Int.MAX_VALUE, category)
28-
.getOrElse {
29-
return Result.failure(Throwable("[GetCartRecommendProductsUseCase] 상품 목록 불러오기 오류", it))
30-
}
31-
20+
productRepository.fetchProducts(
21+
page = 0,
22+
size = Int.MAX_VALUE,
23+
category = category,
24+
)
3225
return combineCartProducts(products)
3326
}
3427

35-
private suspend fun combineCartProducts(catalogProducts: Products): Result<Products> {
36-
val cartProducts =
37-
cartRepository
38-
.fetchAllCartProducts()
39-
.getOrElse {
40-
return Result.failure(Throwable("[GetCartRecommendProductsUseCase] 장바구니 불러오기 오류", it))
41-
}
28+
private suspend fun combineCartProducts(catalogProducts: Products): Products {
29+
val cartProducts = cartRepository.fetchAllCartProducts()
4230
val cartProductIds = cartProducts.products.map { it.productDetail.id }
4331

4432
val filteredProducts =
4533
catalogProducts.products
4634
.filterNot { it.productDetail.id in cartProductIds }
4735
.take(RECOMMEND_PRODUCT_COUNT)
4836

49-
return Result.success(Products(filteredProducts))
37+
return Products(filteredProducts)
5038
}
5139

5240
companion object {

0 commit comments

Comments
 (0)