Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FEATURE] ADD Season Repository, UseCases #86

Merged
merged 4 commits into from
Jun 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@ package com.eshc.goonersapp.core.data.di
import com.eshc.goonersapp.core.data.repository.ChatRepositoryImpl
import com.eshc.goonersapp.core.data.repository.MatchRepositoryImpl
import com.eshc.goonersapp.core.data.repository.PlayerRepositoryImpl
import com.eshc.goonersapp.core.data.repository.SeasonRepositoryImpl
import com.eshc.goonersapp.core.data.repository.TeamRepositoryImpl
import com.eshc.goonersapp.core.data.repository.UserRepositoryImpl
import com.eshc.goonersapp.core.data.util.NetworkConnectivityManager
import com.eshc.goonersapp.core.data.util.NetworkConnectivityManagerImpl
import com.eshc.goonersapp.core.domain.repository.ChatRepository
import com.eshc.goonersapp.core.domain.repository.MatchRepository
import com.eshc.goonersapp.core.domain.repository.PlayerRepository
import com.eshc.goonersapp.core.domain.repository.SeasonRepository
import com.eshc.goonersapp.core.domain.repository.TeamRepository
import com.eshc.goonersapp.core.domain.repository.UserRepository
import dagger.Binds
Expand Down Expand Up @@ -41,6 +43,11 @@ abstract class DataModule {
teamRepository: TeamRepositoryImpl,
): TeamRepository

@Binds
abstract fun bindsSeasonRepository(
seasonRepository: SeasonRepositoryImpl
): SeasonRepository

@Binds
abstract fun bindsUserRepository(
userRepository: UserRepositoryImpl,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package com.eshc.goonersapp.core.data.fake

import com.eshc.goonersapp.core.data.mapper.toDataResult
import com.eshc.goonersapp.core.data.mapper.toModel
import com.eshc.goonersapp.core.domain.model.DataResult
import com.eshc.goonersapp.core.domain.model.season.League
import com.eshc.goonersapp.core.domain.model.season.Rank
import com.eshc.goonersapp.core.domain.model.season.Season
import com.eshc.goonersapp.core.domain.repository.SeasonRepository
import com.eshc.goonersapp.core.network.fake.FakeSeasonDataSource
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.flow
import javax.inject.Inject

class FakeSeasonRepositoryImpl @Inject constructor(
private val fakeSeasonDataSource: FakeSeasonDataSource
): SeasonRepository {
override fun getSeasonListByTeam(teamId: Int): Flow<DataResult<List<Season>>> = flow {
val result = fakeSeasonDataSource
.getSeasonListByTeam(teamId)
.toDataResult { remote -> remote.map { it.toModel() } }

emit(result)
}

override fun getLeagueListAsCurrentSeasonByTeam(
teamId: Int
): Flow<DataResult<List<League>>> = flow {
val result = fakeSeasonDataSource
.getLeagueListAsCurrentSeasonByTeam(teamId)
.toDataResult { remote -> remote.map { it.toModel() } }

emit(result)
}

override fun getPreviewRankListByTeamAndSeason(
teamId: Int,
seasonId: Int,
): Flow<DataResult<List<Rank>>> = flow {
val result = fakeSeasonDataSource
.getPreviewRankListByTeamAndSeason(teamId, seasonId)
.toDataResult { remote -> remote.map { it.toModel() } }

emit(result)
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.eshc.goonersapp.core.data.mapper

import com.eshc.goonersapp.core.domain.model.season.League
import com.eshc.goonersapp.core.domain.model.season.Rank
import com.eshc.goonersapp.core.domain.model.season.Season
import com.eshc.goonersapp.core.network.model.season.RemoteLeague
import com.eshc.goonersapp.core.network.model.season.RemoteRank
import com.eshc.goonersapp.core.network.model.season.RemoteSeason

fun RemoteLeague.toModel() = League(
leagueId = leagueId,
leagueName = leagueName,
shortCode = shortCode,
leagueImgUrl = leagueImageUrl,
seasonId = seasonId,
season = season,
teamId = teamId
)

fun RemoteSeason.toModel() = Season(seasonId = seasonId, season = season)

fun RemoteRank.toModel() = Rank(
standingId = standingId,
position = position,
points = points,
wins = win,
loss = loss,
draw = draw,
goalDifference = goalDifference,
teamId = teamId,
teamName = teamName,
shortCode = shortCode
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package com.eshc.goonersapp.core.data.repository

import com.eshc.goonersapp.core.data.mapper.toDataResult
import com.eshc.goonersapp.core.data.mapper.toModel
import com.eshc.goonersapp.core.domain.model.DataResult
import com.eshc.goonersapp.core.domain.model.season.League
import com.eshc.goonersapp.core.domain.model.season.Rank
import com.eshc.goonersapp.core.domain.model.season.Season
import com.eshc.goonersapp.core.domain.repository.SeasonRepository
import com.eshc.goonersapp.core.network.SeasonNetworkDataSource
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.flow
import javax.inject.Inject

class SeasonRepositoryImpl @Inject constructor(
private val seasonNetworkDataSource: SeasonNetworkDataSource
): SeasonRepository {
override fun getSeasonListByTeam(teamId: Int): Flow<DataResult<List<Season>>> = flow {
val result = seasonNetworkDataSource
.getSeasonListByTeam(teamId)
.toDataResult { remote -> remote.map { it.toModel() } }

emit(result)
}

override fun getLeagueListAsCurrentSeasonByTeam(
teamId: Int
): Flow<DataResult<List<League>>> = flow {
val result = seasonNetworkDataSource
.getLeagueListAsCurrentSeasonByTeam(teamId)
.toDataResult { remote -> remote.map { it.toModel() } }

emit(result)
}

override fun getPreviewRankListByTeamAndSeason(
teamId: Int,
seasonId: Int,
): Flow<DataResult<List<Rank>>> = flow {
val result = seasonNetworkDataSource
.getPreviewRankListByTeamAndSeason(teamId, seasonId)
.toDataResult { remote -> remote.map { it.toModel() } }

emit(result)
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ import org.junit.Test
/**
* Created By KanuKim97
*
* [FakeMatchTest]
* [FakeMatchRepositoryImplTest]
* - FakeMatchRepository Test
*/
class FakeMatchTest {
class FakeMatchRepositoryImplTest {
private lateinit var fakeMatchDataSource: FakeMatchDataSource
private lateinit var fakeMatchRepository: MatchRepository

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
package com.eshc.goonersapp.core.data

import com.eshc.goonersapp.core.data.fake.FakeSeasonRepositoryImpl
import com.eshc.goonersapp.core.domain.model.DataResult
import com.eshc.goonersapp.core.domain.model.season.Rank
import com.eshc.goonersapp.core.domain.repository.SeasonRepository
import com.eshc.goonersapp.core.network.fake.FakeSeasonDataSource
import kotlinx.coroutines.runBlocking
import org.junit.Assert.assertEquals
import org.junit.Assert.fail
import org.junit.Before
import org.junit.Test

/**
* Created By KanuKim97
*
* [FakeSeasonRepositoryImplTest]
* - FakeSeasonRepository Test
*/
class FakeSeasonRepositoryImplTest {
private lateinit var fakeSeasonDataSource: FakeSeasonDataSource
private lateinit var fakeSeasonRepository: SeasonRepository

@Before
fun setUp() {
fakeSeasonDataSource = FakeSeasonDataSource()
fakeSeasonRepository = FakeSeasonRepositoryImpl(fakeSeasonDataSource)
}

@Test
fun fake_get_season_list_by_team_flow_collect_as_success() = runBlocking {
fakeSeasonRepository.getSeasonListByTeam(teamId = 19).collect { result ->
when (result) {
is DataResult.Success -> {
assertEquals(
"2023/2024",
result.data.first().season
)
}
is DataResult.Failure -> fail("Test should not be reached here")
}
}
}

@Test
fun fake_get_season_list_by_team_flow_collect_as_fail() = runBlocking {
fakeSeasonRepository.getSeasonListByTeam(teamId = 20).collect { result ->
when (result) {
is DataResult.Success -> fail("Test should not be reached here")
is DataResult.Failure -> assertEquals(404, result.code)
}
}
}

@Test
fun fake_get_league_list_as_current_season_by_team_as_success() = runBlocking {
fakeSeasonRepository.getLeagueListAsCurrentSeasonByTeam(19).collect { result ->
when(result) {
is DataResult.Success -> {
assertEquals(
21366,
result.data.first().seasonId
)
}
is DataResult.Failure -> fail("Test should not be reached here")
}
}
}


@Test
fun fake_get_league_list_as_current_season_by_team_as_fail() = runBlocking {
fakeSeasonRepository.getLeagueListAsCurrentSeasonByTeam(20).collect { result ->
when(result) {
is DataResult.Success -> fail("Test should not be reached here")
is DataResult.Failure -> assertEquals(404, result.code)
}
}
}

@Test
fun fake_get_preview_rank_list_by_team_and_season_as_success() = runBlocking {
fakeSeasonRepository.getPreviewRankListByTeamAndSeason(
teamId = 19,
seasonId = 21366
).collect { result ->
when (result) {
is DataResult.Success -> {
assertEquals(
listOf<Rank>(),
result.data
)
}
is DataResult.Failure -> fail("Test should not be reached here")
}
}
}

@Test
fun fake_get_preview_rank_list_by_team_and_season_as_fail() = runBlocking {
fakeSeasonRepository.getPreviewRankListByTeamAndSeason(
teamId = 20,
seasonId = 21366
).collect { result ->
when (result) {
is DataResult.Success -> fail("Test should not be reached here")
is DataResult.Failure -> assertEquals(404, result.code)
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.eshc.goonersapp.core.domain.model.season

data class League(
val leagueId: Int,
val leagueName: String,
val shortCode: String,
val leagueImgUrl: String,
val seasonId: Int,
val season: String,
val teamId: Int
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.eshc.goonersapp.core.domain.model.season

data class Rank(
val standingId: Int,
val position: Int,
val points: Int,
val wins: Int,
val loss: Int,
val draw: Int,
val goalDifference: Int,
val teamId: Int,
val teamName: String,
val shortCode: String
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package com.eshc.goonersapp.core.domain.model.season

data class Season(val seasonId: Int, val season: String)
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.eshc.goonersapp.core.domain.repository

import com.eshc.goonersapp.core.domain.model.DataResult
import com.eshc.goonersapp.core.domain.model.season.League
import com.eshc.goonersapp.core.domain.model.season.Rank
import com.eshc.goonersapp.core.domain.model.season.Season
import kotlinx.coroutines.flow.Flow

interface SeasonRepository {

fun getSeasonListByTeam(teamId: Int): Flow<DataResult<List<Season>>>

fun getLeagueListAsCurrentSeasonByTeam(teamId: Int): Flow<DataResult<List<League>>>

fun getPreviewRankListByTeamAndSeason(
teamId: Int,
seasonId: Int
): Flow<DataResult<List<Rank>>>
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.eshc.goonersapp.core.domain.usecase.season

import com.eshc.goonersapp.core.domain.model.DataResult
import com.eshc.goonersapp.core.domain.model.season.League
import com.eshc.goonersapp.core.domain.repository.SeasonRepository
import kotlinx.coroutines.flow.Flow
import javax.inject.Inject

class GetLeagueListAsCurrentSeasonByTeamUseCase @Inject constructor(
private val seasonRepository: SeasonRepository
) {
operator fun invoke(
teamId: Int
): Flow<DataResult<List<League>>> = seasonRepository.getLeagueListAsCurrentSeasonByTeam(teamId)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.eshc.goonersapp.core.domain.usecase.season

import com.eshc.goonersapp.core.domain.model.DataResult
import com.eshc.goonersapp.core.domain.model.season.Rank
import com.eshc.goonersapp.core.domain.repository.SeasonRepository
import kotlinx.coroutines.flow.Flow
import javax.inject.Inject

class GetPreviewRankListByTeamAndSeasonUseCase @Inject constructor(
private val seasonRepository: SeasonRepository
) {
operator fun invoke(
teamId: Int,
seasonId: Int
): Flow<DataResult<List<Rank>>> = seasonRepository.getPreviewRankListByTeamAndSeason(
teamId = teamId,
seasonId = seasonId
)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.eshc.goonersapp.core.domain.usecase.season

import com.eshc.goonersapp.core.domain.model.DataResult
import com.eshc.goonersapp.core.domain.model.season.Season
import com.eshc.goonersapp.core.domain.repository.SeasonRepository
import kotlinx.coroutines.flow.Flow
import javax.inject.Inject

class GetSeasonListByTeamUseCase @Inject constructor(
private val seasonRepository: SeasonRepository
) {
operator fun invoke(
teamId: Int
): Flow<DataResult<List<Season>>> = seasonRepository.getSeasonListByTeam(teamId)
}
Loading
Loading