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 MatchLineup API #84

Merged
merged 9 commits into from
May 23, 2024
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import com.eshc.goonersapp.core.domain.model.DataResult
import com.eshc.goonersapp.core.domain.model.match.Match
import com.eshc.goonersapp.core.domain.model.match.MatchData
import com.eshc.goonersapp.core.domain.model.match.MatchInformation
import com.eshc.goonersapp.core.domain.model.match.MatchLineup
import com.eshc.goonersapp.core.domain.repository.MatchRepository
import com.eshc.goonersapp.core.network.fake.FakeMatchDataSource
import kotlinx.coroutines.flow.Flow
Expand Down Expand Up @@ -63,4 +64,12 @@ class FakeMatchRepositoryImpl @Inject constructor(
emit(result)
}

override fun getMatchLineup(matchId: Int): Flow<DataResult<MatchLineup>> = flow {
val result = fakeMatchDataSource
.getMatchLineup(matchId)
.toDataResult { remote -> remote.toModel() }

emit(result)
}

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

import com.eshc.goonersapp.core.domain.model.match.LineUp
import com.eshc.goonersapp.core.domain.model.match.PlayerLineup
import com.eshc.goonersapp.core.domain.model.match.Match
import com.eshc.goonersapp.core.domain.model.match.MatchData
import com.eshc.goonersapp.core.domain.model.match.MatchDetail
import com.eshc.goonersapp.core.domain.model.match.MatchInformation
import com.eshc.goonersapp.core.domain.model.match.MatchLineup
import com.eshc.goonersapp.core.domain.model.match.NotablePlayer
import com.eshc.goonersapp.core.domain.model.match.Performance
import com.eshc.goonersapp.core.domain.model.match.TeamLineup
import com.eshc.goonersapp.core.domain.model.match.toMatchDetailType
import com.eshc.goonersapp.core.network.model.match.RemoteMatch
import com.eshc.goonersapp.core.network.model.match.RemoteMatchData
import com.eshc.goonersapp.core.network.model.match.RemoteMatchDetail
import com.eshc.goonersapp.core.network.model.match.RemoteMatchInformation
import com.eshc.goonersapp.core.network.model.match.RemoteMatchLineup
import com.eshc.goonersapp.core.network.model.match.RemotePlayerLineup
import com.eshc.goonersapp.core.network.model.match.RemoteTeamLineup

/**
* [RemoteMatch] Mapper
Expand Down Expand Up @@ -54,21 +59,6 @@ fun RemoteMatchInformation.toModel() = MatchInformation(
playerGoalCount = remote.goalCount
)
},
lineUp = lineUp.map { remote ->
LineUp(
lineUpId = remote.lineUpId,
matchId = remote.matchId,
playerId = remote.playerId,
teamId = remote.teamId,
playerName = remote.playerName,
playerBackNumber = remote.jerseyNumber,
formationField = remote.formationField,
formationPosition = remote.formationPosition,
positionId = remote.positionId,
positionCategory = remote.positionCategory,
positionInitial = remote.positionInitial
)
},
performance = Performance(
opponentImageUrl = performance.opponentImage,
win = performance.win,
Expand Down Expand Up @@ -101,4 +91,34 @@ fun RemoteMatchDetail.toModel() = MatchDetail(
fun RemoteMatchData.toModel() = MatchData(
match = match.toModel(),
matchDetail = matchDetail.map { remote -> remote.toModel() }
)
)

/**
* [RemoteMatchLineup] Mapper
* - Mapper [RemoteMatchLineup] to [MatchLineup]
*/
fun RemoteMatchLineup.toModel() = MatchLineup(
homeLineup = homeLineup.toModel(),
awayLineup = awayLineup.toModel()
)

fun RemoteTeamLineup.toModel() = TeamLineup(
teamId = teamId.toInt(),
formation = formation,
playerLineup = players.map { remote -> remote.toModel() }
)

fun RemotePlayerLineup.toModel() = PlayerLineup(
lineUpId = lineUpId,
matchId = matchId,
playerId = playerId,
teamId = teamId,
playerName = playerName,
playerImageUrl = playerImageUrl,
playerBackNumber = jerseyNumber,
formationField = formationField,
formationPosition = formationPosition,
positionId = positionId,
positionCategory = positionCategory,
positionInitial = positionInitial
)
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import com.eshc.goonersapp.core.domain.model.DataResult
import com.eshc.goonersapp.core.domain.model.match.Match
import com.eshc.goonersapp.core.domain.model.match.MatchData
import com.eshc.goonersapp.core.domain.model.match.MatchInformation
import com.eshc.goonersapp.core.domain.model.match.MatchLineup
import com.eshc.goonersapp.core.domain.repository.MatchRepository
import com.eshc.goonersapp.core.network.MatchNetworkDataSource
import kotlinx.coroutines.flow.Flow
Expand Down Expand Up @@ -59,4 +60,12 @@ class MatchRepositoryImpl @Inject constructor(

emit(result)
}

override fun getMatchLineup(matchId: Int): Flow<DataResult<MatchLineup>> = flow {
val result = matchNetworkDataSource
.getMatchLineup(matchId)
.toDataResult { remote -> remote.toModel() }

emit(result)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ import com.eshc.goonersapp.core.domain.model.DataResult
import com.eshc.goonersapp.core.domain.model.match.Match
import com.eshc.goonersapp.core.domain.model.match.MatchData
import com.eshc.goonersapp.core.domain.model.match.MatchInformation
import com.eshc.goonersapp.core.domain.model.match.MatchLineup
import com.eshc.goonersapp.core.domain.model.match.Performance
import com.eshc.goonersapp.core.domain.model.match.TeamLineup
import com.eshc.goonersapp.core.domain.repository.MatchRepository
import com.eshc.goonersapp.core.network.fake.FakeMatchDataSource
import kotlinx.coroutines.runBlocking
Expand Down Expand Up @@ -62,7 +64,6 @@ class FakeMatchTest {
assertEquals(
MatchInformation(
notablePlayer = null,
lineUp = listOf(),
performance = Performance(opponentImageUrl = "", win = 0, draw = 0, lose = 0)
),
result.data
Expand Down Expand Up @@ -110,4 +111,30 @@ class FakeMatchTest {
}
}
}

@Test
fun testMatchLineupWithFake() = runBlocking {
fakeMatchRepository.getMatchLineup(38).collect { result ->
when (result) {
is DataResult.Success -> {
assertEquals(
MatchLineup(
homeLineup = TeamLineup(
teamId = 0,
formation = "",
playerLineup = emptyList()
),
awayLineup = TeamLineup(
teamId = 0,
formation = "",
playerLineup = emptyList()
)
),
result.data
)
}
is DataResult.Failure -> { /* Nothing */ }
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package com.eshc.goonersapp.core.domain.model.match

data class MatchInformation(
val notablePlayer: NotablePlayer? = null,
val lineUp: List<LineUp>,
val performance: Performance
)

Expand All @@ -17,20 +16,6 @@ data class NotablePlayer(
val playerGoalCount: Int
)

data class LineUp(
val lineUpId: Double,
val matchId: Int,
val playerId: Int,
val teamId: Int,
val playerName: String,
val playerBackNumber: Int,
val formationField: String?,
val formationPosition: Int?,
val positionId: Int,
val positionCategory: String,
val positionInitial: String
)

data class Performance(
val opponentImageUrl: String,
val win: Int,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.eshc.goonersapp.core.domain.model.match

data class MatchLineup(
val homeLineup : TeamLineup,
val awayLineup : TeamLineup
)

data class TeamLineup(
val teamId : Int,
val formation : String,
val playerLineup : List<PlayerLineup>
)

data class PlayerLineup(
val lineUpId: Long,
val matchId: Int,
val playerId: Int,
val teamId: Int,
val playerName: String,
val playerImageUrl : String,
val playerBackNumber: Int,
val formationField: String?,
val formationPosition: Int?,
val positionId: Int,
val positionCategory: String,
val positionInitial: String
)
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import com.eshc.goonersapp.core.domain.model.DataResult
import com.eshc.goonersapp.core.domain.model.match.Match
import com.eshc.goonersapp.core.domain.model.match.MatchData
import com.eshc.goonersapp.core.domain.model.match.MatchInformation
import com.eshc.goonersapp.core.domain.model.match.MatchLineup
import kotlinx.coroutines.flow.Flow

interface MatchRepository {
Expand All @@ -18,4 +19,5 @@ interface MatchRepository {

fun getRecentlyMatch() : Flow<DataResult<MatchData>>

fun getMatchLineup(matchId: Int) : Flow<DataResult<MatchLineup>>
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.eshc.goonersapp.core.domain.usecase.match

import com.eshc.goonersapp.core.domain.model.DataResult
import com.eshc.goonersapp.core.domain.model.match.MatchLineup
import com.eshc.goonersapp.core.domain.repository.MatchRepository
import kotlinx.coroutines.flow.Flow
import javax.inject.Inject

class GetMatchLineupUseCase @Inject constructor(
private val matchRepository: MatchRepository
) {
operator fun invoke(
matchId: Int,
): Flow<DataResult<MatchLineup>> = matchRepository.getMatchLineup(
matchId
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import com.eshc.goonersapp.core.network.model.NetworkResult
import com.eshc.goonersapp.core.network.model.match.RemoteMatch
import com.eshc.goonersapp.core.network.model.match.RemoteMatchData
import com.eshc.goonersapp.core.network.model.match.RemoteMatchInformation
import com.eshc.goonersapp.core.network.model.match.RemoteMatchLineup

interface MatchNetworkDataSource {

Expand All @@ -20,4 +21,6 @@ interface MatchNetworkDataSource {
suspend fun getUpcomingMatches(): NetworkResult<List<RemoteMatch>>

suspend fun getRecentlyMatch(): NetworkResult<RemoteMatchData>

suspend fun getMatchLineup(matchId: Int) : NetworkResult<RemoteMatchLineup>
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import com.eshc.goonersapp.core.network.model.BaseResponse
import com.eshc.goonersapp.core.network.model.match.RemoteMatch
import com.eshc.goonersapp.core.network.model.match.RemoteMatchData
import com.eshc.goonersapp.core.network.model.match.RemoteMatchInformation
import com.eshc.goonersapp.core.network.model.match.RemoteMatchLineup
import retrofit2.Response
import retrofit2.http.GET
import retrofit2.http.Path
Expand Down Expand Up @@ -40,4 +41,9 @@ interface MatchNetworkService {
suspend fun getRecentlyMatch(
@Query("teamId") teamId : Int = 19
) : Response<BaseResponse<RemoteMatchData>>

@GET(value = "$MATCH_BASE_URL/lineup")
suspend fun getMatchLineup(
@Query("matchId") matchId: Int
) : Response<BaseResponse<RemoteMatchLineup>>
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import com.eshc.goonersapp.core.network.model.match.Performance
import com.eshc.goonersapp.core.network.model.match.RemoteMatch
import com.eshc.goonersapp.core.network.model.match.RemoteMatchData
import com.eshc.goonersapp.core.network.model.match.RemoteMatchInformation
import com.eshc.goonersapp.core.network.model.match.RemoteMatchLineup
import com.eshc.goonersapp.core.network.model.match.RemoteTeamLineup
import javax.inject.Inject

/**
Expand All @@ -31,6 +33,10 @@ import javax.inject.Inject
*
* getRecentlyMatch()
* - return [NetworkResult.Success]
*
* getMatchLineup(matchId: Int)
* - if matchId is over 39 then return [NetworkResult.Error]
* - else return [NetworkResult.Success]
*/
class FakeMatchDataSource @Inject constructor(): MatchNetworkDataSource {
override suspend fun getMatch(matchId: Int): NetworkResult<RemoteMatchData> {
Expand All @@ -57,7 +63,6 @@ class FakeMatchDataSource @Inject constructor(): MatchNetworkDataSource {
NetworkResult.Success(
RemoteMatchInformation(
notablePlayer = null,
lineUp = listOf(),
performance = Performance()
)
)
Expand Down Expand Up @@ -89,4 +94,16 @@ class FakeMatchDataSource @Inject constructor(): MatchNetworkDataSource {
)
}

override suspend fun getMatchLineup(matchId: Int): NetworkResult<RemoteMatchLineup> {
return if (matchId < 39) {
NetworkResult.Success(
RemoteMatchLineup(
homeLineup = RemoteTeamLineup(),
awayLineup = RemoteTeamLineup()
)
)
} else {
NetworkResult.Error(code = 404, message = "Not Found")
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import kotlinx.serialization.Serializable
@Serializable
data class RemoteMatchInformation(
@SerialName("notablePlayer") val notablePlayer: NotablePlayer? = null,
@SerialName("lineUp") val lineUp: List<LineUp>,
@SerialName("performance") val performance: Performance
)

Expand All @@ -22,22 +21,6 @@ data class NotablePlayer(
@SerialName("goal_count") val goalCount: Int
)

@Serializable
data class LineUp(
@SerialName("lineup_id") val lineUpId: Double = 0.0,
@SerialName("match_id") val matchId: Int = 0,
@SerialName("player_id") val playerId: Int = 0,
@SerialName("team_id") val teamId: Int = 0,
@SerialName("player_name") val playerName: String = "",
@SerialName("player_image_url") val playerImageUrl: String = "",
@SerialName("jersey_number") val jerseyNumber: Int = 0,
@SerialName("formation_field") val formationField: String? = "",
@SerialName("formation_position") val formationPosition: Int? = 0,
@SerialName("position_id") val positionId: Int = 0,
@SerialName("position_category") val positionCategory: String = "",
@SerialName("position_initial") val positionInitial: String = ""
)

@Serializable
data class Performance(
@SerialName("opponent_image_url") val opponentImage: String = "",
Expand Down
Loading
Loading