Skip to content

Commit

Permalink
[FEAT] : 레포지토리 적용 #54
Browse files Browse the repository at this point in the history
  • Loading branch information
lsakee committed Oct 24, 2023
1 parent 1575bf1 commit 4268072
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -1,24 +1,30 @@
package com.velogm.data.repositoryimpl

import com.velogm.data.datasource.TagDataSource
import com.velogm.domain.OutResult
import com.velogm.domain.model.PostList
import com.velogm.domain.model.Tag
import com.velogm.domain.repository.TagRepository
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.flow
import retrofit2.HttpException
import java.lang.RuntimeException
import javax.inject.Inject

class TagRepositoryImpl @Inject constructor(
private val dataSource: TagDataSource
) : TagRepository {

override suspend fun getTag(): Flow<List<Tag>> {
return flow {
val result = kotlin.runCatching {
dataSource.getTag().map { Tag(it) }
}
emit(result.getOrDefault(emptyList()))
override suspend fun getTag(): Flow<OutResult<List<Tag>>> = flow {
val result = runCatching {
val tag = dataSource.getTag().map { Tag(it) }
OutResult.Success(tag)
}
val outcome = result.getOrElse {
val errorCode = (it as? HttpException)?.code() ?: -1
OutResult.Failure(error = VelogHttpException(errorCode, "$errorCode"))
}
emit(outcome)
}

override suspend fun getPopularTag(): Flow<List<Tag>> {
Expand Down Expand Up @@ -56,4 +62,9 @@ class TagRepositoryImpl @Inject constructor(
emit(result.getOrDefault(PostList(emptyList())))
}
}
}
}

class VelogHttpException(
val httpCode: Int,
override val message: String,
) : RuntimeException()
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package com.velogm.domain.repository

import com.velogm.domain.OutResult
import com.velogm.domain.model.PostList
import com.velogm.domain.model.Tag
import kotlinx.coroutines.flow.Flow

interface TagRepository {
suspend fun getTag(): Flow<List<Tag>>
suspend fun getTag(): Flow<OutResult<List<Tag>>>
suspend fun getPopularTag():Flow<List<Tag>>
suspend fun deleteTag(tag:String):Flow<String>
suspend fun addTag(tag:String):Flow<String>
Expand Down

0 comments on commit 4268072

Please sign in to comment.