Skip to content

Commit 4268072

Browse files
committed
[FEAT] : 레포지토리 적용 #54
1 parent 1575bf1 commit 4268072

File tree

2 files changed

+20
-8
lines changed

2 files changed

+20
-8
lines changed

data/src/main/java/com/velogm/data/repositoryimpl/TagRepositoryImpl.kt

+18-7
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,30 @@
11
package com.velogm.data.repositoryimpl
22

33
import com.velogm.data.datasource.TagDataSource
4+
import com.velogm.domain.OutResult
45
import com.velogm.domain.model.PostList
56
import com.velogm.domain.model.Tag
67
import com.velogm.domain.repository.TagRepository
78
import kotlinx.coroutines.flow.Flow
89
import kotlinx.coroutines.flow.flow
10+
import retrofit2.HttpException
11+
import java.lang.RuntimeException
912
import javax.inject.Inject
1013

1114
class TagRepositoryImpl @Inject constructor(
1215
private val dataSource: TagDataSource
1316
) : TagRepository {
1417

15-
override suspend fun getTag(): Flow<List<Tag>> {
16-
return flow {
17-
val result = kotlin.runCatching {
18-
dataSource.getTag().map { Tag(it) }
19-
}
20-
emit(result.getOrDefault(emptyList()))
18+
override suspend fun getTag(): Flow<OutResult<List<Tag>>> = flow {
19+
val result = runCatching {
20+
val tag = dataSource.getTag().map { Tag(it) }
21+
OutResult.Success(tag)
22+
}
23+
val outcome = result.getOrElse {
24+
val errorCode = (it as? HttpException)?.code() ?: -1
25+
OutResult.Failure(error = VelogHttpException(errorCode, "$errorCode"))
2126
}
27+
emit(outcome)
2228
}
2329

2430
override suspend fun getPopularTag(): Flow<List<Tag>> {
@@ -56,4 +62,9 @@ class TagRepositoryImpl @Inject constructor(
5662
emit(result.getOrDefault(PostList(emptyList())))
5763
}
5864
}
59-
}
65+
}
66+
67+
class VelogHttpException(
68+
val httpCode: Int,
69+
override val message: String,
70+
) : RuntimeException()

domain/src/main/java/com/velogm/domain/repository/TagRepository.kt

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
package com.velogm.domain.repository
22

3+
import com.velogm.domain.OutResult
34
import com.velogm.domain.model.PostList
45
import com.velogm.domain.model.Tag
56
import kotlinx.coroutines.flow.Flow
67

78
interface TagRepository {
8-
suspend fun getTag(): Flow<List<Tag>>
9+
suspend fun getTag(): Flow<OutResult<List<Tag>>>
910
suspend fun getPopularTag():Flow<List<Tag>>
1011
suspend fun deleteTag(tag:String):Flow<String>
1112
suspend fun addTag(tag:String):Flow<String>

0 commit comments

Comments
 (0)