Skip to content

Commit 176b58b

Browse files
committed
Return only the group the match is in
1 parent cd5750f commit 176b58b

File tree

3 files changed

+49
-25
lines changed

3 files changed

+49
-25
lines changed

sport/app/football/controllers/MatchController.scala

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -182,8 +182,7 @@ class MatchController(
182182
Action.async { implicit request =>
183183
maybeMatch match {
184184
case Some((competitionId, theMatch)) =>
185-
val table =
186-
competitionsService.competitions.find(_.id == competitionId).filter(_.hasLeagueTable).map(Table(_))
185+
val group = tableGroupForMatch(competitionId, theMatch)
187186
val lineup: Future[LineUp] = competitionsService.getLineup(theMatch)
188187
val page: Future[MatchPage] = lineup.map(MatchPage(theMatch, _))
189188
val tier = FootballSummaryPagePicker.getTier()
@@ -196,7 +195,7 @@ class MatchController(
196195
val model = DotcomRenderingFootballMatchSummaryDataModel(
197196
page = page,
198197
footballMatch = footballMatch,
199-
table = table,
198+
group = group,
200199
)
201200
Future.successful(Cached(CacheTime.FootballMatch)(JsonComponent.fromWritable(model)))
202201

@@ -209,7 +208,7 @@ class MatchController(
209208
val model = DotcomRenderingFootballMatchSummaryDataModel(
210209
page = page,
211210
footballMatch = footballMatch,
212-
table = table,
211+
group = group,
213212
)
214213
remoteRenderer.getFootballMatchSummaryPage(
215214
wsClient,
@@ -229,4 +228,13 @@ class MatchController(
229228
Future.successful(Cached(CacheTime.FootballMatch)(WithoutRevalidationResult(Found("/football/results"))))
230229
}
231230
}
231+
232+
private def tableGroupForMatch(competitionId: String, theMatch: FootballMatch): Option[Group] =
233+
competitionsService.competitions
234+
.find(_.id == competitionId)
235+
.filter(_.hasLeagueTable)
236+
.map(Table(_))
237+
.flatMap { table =>
238+
table.groups.find(group => group.hasTeam(theMatch.homeTeam.id) && group.hasTeam(theMatch.awayTeam.id))
239+
}
232240
}

sport/app/football/model/DotcomRenderingFootballDataModel.scala

Lines changed: 34 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ import play.api.libs.json._
2929
import play.api.mvc.RequestHeader
3030
import views.support.{CamelCase, JavaScriptPage}
3131
import ab.ABTests
32-
import football.model.DotcomRenderingFootballTablesDataModel.getEntries
3332

3433
import java.time.LocalDate
3534
import java.time.format.DateTimeFormatter
@@ -144,20 +143,6 @@ private object DotcomRenderingFootballDataModelImplicits {
144143
Json.writes[MatchesByDateAndCompetition]
145144

146145
implicit val competitionFilterFormat: Writes[CompetitionFilter] = Json.writes[CompetitionFilter]
147-
148-
implicit val tableWrites: Writes[Table] = (table: Table) =>
149-
withoutDeepNull(
150-
Json.obj(
151-
"competition" -> Json.toJson(table.competition: CompetitionSummary),
152-
"groups" -> table.groups.map { group =>
153-
Json.obj(
154-
"round" -> group.round,
155-
"entries" -> getEntries(table.competition, group),
156-
)
157-
},
158-
"hasGroups" -> table.multiGroup,
159-
),
160-
)
161146
}
162147

163148
case class DotcomRenderingFootballMatchListDataModel(
@@ -288,7 +273,7 @@ object DotcomRenderingFootballTablesDataModel {
288273

289274
import football.model.DotcomRenderingFootballDataModelImplicits._
290275

291-
def getEntries(competition: Competition, group: Group): Seq[JsObject] = {
276+
private def getEntries(competition: Competition, group: Group): Seq[JsObject] = {
292277
group.entries.map { entry =>
293278
Json.obj(
294279
"stageNumber" -> entry.stageNumber,
@@ -312,7 +297,20 @@ object DotcomRenderingFootballTablesDataModel {
312297

313298
private implicit val teamScoreFormat: Writes[TeamScore] = Json.writes[TeamScore]
314299
private implicit val teamResultFormat: Writes[TeamResult] = Json.writes[TeamResult]
315-
private implicit val groupFormat: Writes[Group] = Json.writes[Group]
300+
301+
private implicit val tableWrites: Writes[Table] = (table: Table) =>
302+
withoutDeepNull(
303+
Json.obj(
304+
"competition" -> Json.toJson(table.competition: CompetitionSummary),
305+
"groups" -> table.groups.map { group =>
306+
Json.obj(
307+
"round" -> group.round,
308+
"entries" -> getEntries(table.competition, group),
309+
)
310+
},
311+
"hasGroups" -> table.multiGroup,
312+
),
313+
)
316314

317315
implicit def dotcomRenderingFootballTablesDataModel: Writes[DotcomRenderingFootballTablesDataModel] =
318316
Json.writes[DotcomRenderingFootballTablesDataModel]
@@ -325,7 +323,7 @@ object DotcomRenderingFootballTablesDataModel {
325323

326324
case class DotcomRenderingFootballMatchSummaryDataModel(
327325
footballMatch: MatchDataAnswer,
328-
table: Option[Table],
326+
group: Option[Group],
329327
nav: Nav,
330328
editionId: String,
331329
guardianBaseURL: String,
@@ -341,7 +339,7 @@ object DotcomRenderingFootballMatchSummaryDataModel {
341339
def apply(
342340
page: MatchPage,
343341
footballMatch: MatchDataAnswer,
344-
table: Option[Table],
342+
group: Option[Group],
345343
)(implicit
346344
request: RequestHeader,
347345
context: ApplicationContext,
@@ -351,7 +349,7 @@ object DotcomRenderingFootballMatchSummaryDataModel {
351349
val combinedConfig: JsObject = DotcomRenderingFootballDataModel.getConfig(page)
352350
DotcomRenderingFootballMatchSummaryDataModel(
353351
footballMatch = footballMatch,
354-
table = table,
352+
group = group,
355353
nav = nav,
356354
editionId = edition.id,
357355
guardianBaseURL = Configuration.site.host,
@@ -366,6 +364,22 @@ object DotcomRenderingFootballMatchSummaryDataModel {
366364

367365
import football.model.DotcomRenderingFootballDataModelImplicits._
368366

367+
private def getGroupEntries(group: Group): Seq[JsObject] = {
368+
group.entries.map { entry =>
369+
Json.obj(
370+
"stageNumber" -> entry.stageNumber,
371+
"round" -> entry.round,
372+
"team" -> entry.team,
373+
"teamUrl" -> TeamUrl(entry.team),
374+
)
375+
}
376+
}
377+
implicit val groupWrites: Writes[Group] = (group: Group) =>
378+
Json.obj(
379+
"round" -> group.round,
380+
"entries" -> getGroupEntries(group),
381+
)
382+
369383
implicit def dotcomRenderingFootballMatchSummaryDataModel: Writes[DotcomRenderingFootballMatchSummaryDataModel] =
370384
Json.writes[DotcomRenderingFootballMatchSummaryDataModel]
371385

sport/app/football/model/model.scala

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,9 @@ case class Competition(
5656
}
5757
}
5858

59-
case class Group(round: Round, entries: Seq[LeagueTableEntry])
59+
case class Group(round: Round, entries: Seq[LeagueTableEntry]) {
60+
def hasTeam(teamId: String): Boolean = entries.exists(_.team.id == teamId)
61+
}
6062

6163
case class Table(competition: Competition, groups: Seq[Group], hasGroups: Boolean = false) {
6264
lazy val multiGroup = hasGroups || groups.size > 1

0 commit comments

Comments
 (0)