Skip to content

Commit

Permalink
Merge pull request #26 from YAPP-Github/refactor/tgyuu/PC-000
Browse files Browse the repository at this point in the history
  • Loading branch information
tgyuuAn authored Jan 8, 2025
2 parents b6a187f + 6875901 commit dba8062
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 4 deletions.
13 changes: 13 additions & 0 deletions core/common/src/main/java/com/puzzle/common/ResultUtil.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.puzzle.common

import kotlin.coroutines.cancellation.CancellationException

suspend inline fun <T, R> T.suspendRunCatching(crossinline block: suspend T.() -> R): Result<R> {
return try {
Result.success(block())
} catch (e: CancellationException) {
throw e
} catch (t: Throwable) {
Result.failure(t)
}
}
1 change: 1 addition & 0 deletions core/data/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@ dependencies {
implementation(projects.core.domain)
implementation(projects.core.network)
implementation(projects.core.database)
implementation(projects.core.common)
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.puzzle.data.repository

import com.puzzle.common.suspendRunCatching
import com.puzzle.database.model.terms.TermEntity
import com.puzzle.database.source.term.LocalTermDataSource
import com.puzzle.domain.model.terms.Term
Expand All @@ -12,7 +13,7 @@ class TermsRepositoryImpl @Inject constructor(
private val termDataSource: TermDataSource,
private val localTermDataSource: LocalTermDataSource,
) : TermsRepository {
override suspend fun loadTerms(): Result<Unit> = runCatching {
override suspend fun loadTerms(): Result<Unit> = suspendRunCatching {
val terms = termDataSource.loadTerms()
.getOrThrow()
.toDomain()
Expand All @@ -31,8 +32,8 @@ class TermsRepositoryImpl @Inject constructor(
localTermDataSource.clearAndInsertTerms(termsEntity)
}

override suspend fun getTerms(): Result<List<Term>> = runCatching {
override suspend fun getTerms(): Result<List<Term>> = suspendRunCatching {
localTermDataSource.getTerms()
.map { it.toDomain() }
.map(TermEntity::toDomain)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import java.time.LocalDateTime
data class LoadTermsResponse(
val responses: List<TermResponse>?,
) {
fun toDomain() = responses?.map { it.toDomain() } ?: emptyList()
fun toDomain() = responses?.map(TermResponse::toDomain) ?: emptyList()
}

@Serializable
Expand Down

0 comments on commit dba8062

Please sign in to comment.