Skip to content

Commit

Permalink
Adding MatchRepository. (#580)
Browse files Browse the repository at this point in the history
  • Loading branch information
AdamMc331 authored Dec 1, 2024
1 parent eefc919 commit a3ed71b
Show file tree
Hide file tree
Showing 3 changed files with 141 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.adammcneilly.pocketleague.shared.app.data.match

/**
* Defines an enumeration of the ways we can request a list of matches
* in the PocketLeague app.
*/
sealed interface MatchListRequest {
/**
* Requests matches (should only be one) with the given [matchId].
*/
data class Id(
val matchId: String,
) : MatchListRequest

/**
* Request a list of matches within a given date range.
*/
data class DateRange(
val startDateUTC: String,
val endDateUTC: String,
) : MatchListRequest

/**
* Finds a list of matches with a given [eventId] and [stageId] combination.
*/
data class EventStage(
val eventId: String,
val stageId: String,
) : MatchListRequest
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.adammcneilly.pocketleague.shared.app.data.match

import com.adammcneilly.pocketleague.shared.app.core.models.Match
import kotlinx.coroutines.flow.Flow

/**
* Defines the data layer for any match related requests.
*/
interface MatchRepository {
/**
* Stream a list of [Match] entities that apply to the supplied [request].
*/
fun getMatches(
request: MatchListRequest,
): Flow<List<Match>>
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
package com.adammcneilly.pocketleague.shared.app.data.octanegg.match

import com.adammcneilly.pocketleague.shared.app.core.models.Match
import com.adammcneilly.pocketleague.shared.app.data.match.MatchListRequest
import com.adammcneilly.pocketleague.shared.app.data.match.MatchRepository
import com.adammcneilly.pocketleague.shared.app.data.octanegg.dto.OctaneGGMatch
import com.adammcneilly.pocketleague.shared.app.data.octanegg.dto.OctaneGGMatchListResponse
import com.adammcneilly.pocketleague.shared.app.data.remote.BaseKtorClient
import com.adammcneilly.pocketleague.shared.app.data.remote.RemoteParams
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.flow

/**
* An implementation of [MatchRepository] that requests
* data from the supplied [apiClient] assuming it's an octane.gg client.
*/
class OctaneGGMatchRepository(
private val apiClient: BaseKtorClient,
) : MatchRepository {
override fun getMatches(
request: MatchListRequest,
): Flow<List<Match>> {
return when (request) {
is MatchListRequest.Id -> {
flow {
val matchResult = apiClient.getResponse<OctaneGGMatch>(
endpoint = matchByIdEndpoint(request.matchId),
params = getParamsForRequest(request),
).map { octaneMatch ->
listOf(octaneMatch.toMatch())
}

emit(matchResult.getOrNull().orEmpty())
}
}

else -> {
flow {
val matchResult = apiClient.getResponse<OctaneGGMatchListResponse>(
endpoint = MATCHES_ENDPOINT,
params = getParamsForRequest(request),
).map { octaneMatchList ->
octaneMatchList.matches?.map(OctaneGGMatch::toMatch).orEmpty()
}

emit(matchResult.getOrNull().orEmpty())
}
}
}
}

private fun getParamsForRequest(
request: MatchListRequest,
): RemoteParams {
val initialParams = when (request) {
is MatchListRequest.DateRange -> {
mapOf(
AFTER_KEY to request.startDateUTC,
BEFORE_KEY to request.endDateUTC,
)
}

is MatchListRequest.EventStage -> {
mapOf(
EVENT_KEY to request.eventId,
STAGE_KEY to request.stageId,
)
}

is MatchListRequest.Id -> {
emptyMap()
}
}

return initialParams + mapOf(
GROUP_KEY to "rlcs",
)
}

companion object {
private const val MATCHES_ENDPOINT = "/matches"

private fun matchByIdEndpoint(
id: String,
): String {
return "$MATCHES_ENDPOINT/$id"
}

private const val BEFORE_KEY = "before"
private const val AFTER_KEY = "after"
private const val GROUP_KEY = "group"
private const val EVENT_KEY = "event"
private const val STAGE_KEY = "stage"
}
}

0 comments on commit a3ed71b

Please sign in to comment.