Skip to content

Commit

Permalink
[#333] 코드리뷰 반영
Browse files Browse the repository at this point in the history
  • Loading branch information
taehwandev committed Jun 10, 2024
1 parent 24766b0 commit d2676a1
Show file tree
Hide file tree
Showing 8 changed files with 88 additions and 72 deletions.
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package com.droidknights.app.core.data.repository.api

import com.droidknights.app.core.model.Contributor
import com.droidknights.app.core.model.ContributorGroup
import kotlinx.coroutines.flow.Flow

interface ContributorRepository {

fun flowContributors(
owner: String,
name: String,
): Flow<Map<Int, List<Contributor>>>
): Flow<List<ContributorGroup>>
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import com.droidknights.app.core.data.api.GithubApi
import com.droidknights.app.core.data.api.GithubRawApi
import com.droidknights.app.core.data.mapper.toData
import com.droidknights.app.core.data.repository.api.ContributorRepository
import com.droidknights.app.core.model.Contributor
import com.droidknights.app.core.model.ContributorGroup
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.combine
import kotlinx.coroutines.flow.flow
Expand All @@ -16,7 +16,7 @@ internal class DefaultContributorRepository @Inject constructor(
private val githubRawApi: GithubRawApi
) : ContributorRepository {

override fun flowContributors(owner: String, name: String): Flow<Map<Int, List<Contributor>>> =
override fun flowContributors(owner: String, name: String): Flow<List<ContributorGroup>> =
combine(
flow {
emit(githubApi.getContributors(owner, name))
Expand Down Expand Up @@ -48,5 +48,11 @@ internal class DefaultContributorRepository @Inject constructor(
yearMap.mapValues { year ->
year.value.mapNotNull { id -> resultMap[id] }
}
yearMap.map { year ->
ContributorGroup(
year = year.key,
contributors = year.value.mapNotNull { id -> resultMap[id] },
)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import com.droidknights.app.core.data.api.fake.FakeGithubApi
import com.droidknights.app.core.data.api.fake.FakeGithubRawApi
import com.droidknights.app.core.data.api.model.ContributorResponse
import com.droidknights.app.core.model.Contributor
import com.droidknights.app.core.model.ContributorGroup
import io.kotest.core.spec.style.BehaviorSpec
import io.kotest.matchers.shouldBe
import kotlinx.coroutines.flow.first
Expand All @@ -24,27 +25,33 @@ internal class DefaultContributorRepositoryTest : BehaviorSpec() {
).first()
Then("컨트리뷰터를 반환한다") {
contributorList.size shouldBe 2
contributorList shouldBe mapOf(
2024 to listOf(
Contributor(
name = "2024 - name",
imageUrl = "test image url",
githubUrl = "test github url",
id = 32327475
contributorList shouldBe listOf(
ContributorGroup(
year = 2024,
contributors = listOf(
Contributor(
name = "2024 - name",
imageUrl = "test image url",
githubUrl = "test github url",
id = 32327475
),
),
),
2023 to listOf(
Contributor(
name = "test name",
imageUrl = "test image url",
githubUrl = "test github url",
id = 28249981
),
Contributor(
name = "2024 - name",
imageUrl = "test image url",
githubUrl = "test github url",
id = 32327475
ContributorGroup(
year = 2023,
contributors = listOf(
Contributor(
name = "test name",
imageUrl = "test image url",
githubUrl = "test github url",
id = 28249981
),
Contributor(
name = "2024 - name",
imageUrl = "test image url",
githubUrl = "test github url",
id = 32327475
),
),
),
)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
package com.droidknights.app.core.domain.usecase

import com.droidknights.app.core.data.repository.api.ContributorRepository
import com.droidknights.app.core.model.Contributor
import com.droidknights.app.core.model.ContributorGroup
import kotlinx.coroutines.flow.Flow
import javax.inject.Inject

class GetContributorsUseCase @Inject constructor(
private val repository: ContributorRepository,
) {

operator fun invoke(): Flow<Map<Int, List<Contributor>>> =
operator fun invoke(): Flow<List<ContributorGroup>> =
repository.flowContributors(
owner = OWNER,
name = NAME,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
package com.droidknights.app.core.domain.usecase

import com.droidknights.app.core.data.repository.api.ContributorRepository
import com.droidknights.app.core.model.Contributor
import com.droidknights.app.core.model.ContributorGroup
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.flowOf

internal class FakeContributorRepository(
private val contributors: Map<Int, List<Contributor>>,
private val contributors: List<ContributorGroup>,
) : ContributorRepository {

override fun flowContributors(owner: String, name: String): Flow<Map<Int, List<Contributor>>> =
override fun flowContributors(owner: String, name: String): Flow<List<ContributorGroup>> =
flowOf(contributors)
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
package com.droidknights.app.core.domain.usecase

import com.droidknights.app.core.model.Contributor
import com.droidknights.app.core.model.ContributorGroup
import io.kotest.core.spec.style.BehaviorSpec
import io.kotest.matchers.shouldBe
import kotlinx.coroutines.flow.first

internal class GetContributorsUseCaseTest : BehaviorSpec() {

private val useCase: GetContributorsUseCase = GetContributorsUseCase(
repository = FakeContributorRepository(contributors)
repository = FakeContributorRepository(mockContributors)
)

init {
Expand All @@ -17,35 +18,42 @@ internal class GetContributorsUseCaseTest : BehaviorSpec() {
When("드로이드나이츠 컨트리뷰터를 조회한다") {
val contributors = useCase.invoke().first()

Then("올해 드로이드나이츠 컨트리뷰터를 반환한다") {
contributors shouldBe contributors
Then("연도별 드로이드나이츠 컨트리뷰터를 반환한다") {
contributors shouldBe mockContributors
}
}
}
}

companion object {
private val contributors = mapOf(
2023 to listOf(
Contributor(
name = "test name",
imageUrl = "test image url",
githubUrl = "test github url",
id = 28249981
),
Contributor(
name = "2024 - name",
imageUrl = "test image url",
githubUrl = "test github url",
id = 32327475

private val mockContributors = listOf(
ContributorGroup(
year = 2024,
contributors = listOf(
Contributor(
name = "2024 - name",
imageUrl = "test image url",
githubUrl = "test github url",
id = 32327475
),
),
),
2024 to listOf(
Contributor(
name = "2024 - name",
imageUrl = "test image url",
githubUrl = "test github url",
id = 32327475
ContributorGroup(
year = 2023,
contributors = listOf(
Contributor(
name = "test name",
imageUrl = "test image url",
githubUrl = "test github url",
id = 28249981
),
Contributor(
name = "2024 - name",
imageUrl = "test image url",
githubUrl = "test github url",
id = 32327475
),
),
),
)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.droidknights.app.core.model

data class ContributorGroup(
val year: Int,
val contributors: List<Contributor>,
)
Original file line number Diff line number Diff line change
@@ -1,32 +1,21 @@
package com.droidknights.app.feature.contributor.model.convert

import com.droidknights.app.core.model.Contributor
import com.droidknights.app.core.model.ContributorGroup
import com.droidknights.app.feature.contributor.model.ContributorsUiState
import kotlinx.collections.immutable.toPersistentList

internal fun Map<Int, List<Contributor>>.toContributorsUiState(): ContributorsUiState {
val newList = mutableListOf<ContributorsUiState.Contributors.Item>()

// key, value 추가
forEach { (key, values) ->
newList.add(
ContributorsUiState.Contributors.Item.Section(
title = key.toString(),
)
)
values.forEach { item ->
newList.add(
internal fun List<ContributorGroup>.toContributorsUiState(): ContributorsUiState =
ContributorsUiState.Contributors(
contributors = flatMap { (year, contributors) ->
sequenceOf(
ContributorsUiState.Contributors.Item.Section(title = year.toString())
) + contributors.map { item ->
ContributorsUiState.Contributors.Item.User(
id = item.id,
imageUrl = item.imageUrl,
githubUrl = item.githubUrl,
name = item.name,
name = item.name
)
)
}
}

return ContributorsUiState.Contributors(
contributors = newList.toPersistentList(),
)
}
}
}.toPersistentList(),
)

0 comments on commit d2676a1

Please sign in to comment.