-
Notifications
You must be signed in to change notification settings - Fork 555
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add dcr json endpoint for football/live
- Loading branch information
1 parent
8b7f135
commit cc94943
Showing
2 changed files
with
116 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
package football.model | ||
|
||
import model.Competition | ||
import model.dotcomrendering.DotcomRenderingUtils.withoutNull | ||
import pa.{ | ||
Fixture, | ||
FootballMatch, | ||
LeagueStats, | ||
LeagueTableEntry, | ||
LeagueTeam, | ||
LiveMatch, | ||
MatchDay, | ||
MatchDayTeam, | ||
Official, | ||
Result, | ||
Round, | ||
Stage, | ||
Venue, | ||
Competition => PaCompetition, | ||
} | ||
import play.api.libs.json.{JsObject, JsString, Json, Writes} | ||
|
||
import java.time.LocalDate | ||
import java.time.format.DateTimeFormatter | ||
|
||
case class CompetitionMatches(competition: Competition, matches: List[FootballMatch]) | ||
case class DateCompetitionMatches(date: LocalDate, competitions: List[CompetitionMatches]) | ||
|
||
case class LiveScores( | ||
pageTitle: String, | ||
pageType: String, | ||
matchesGroupedByDateAndCompetition: Seq[DateCompetitionMatches], | ||
nextPage: Option[String], | ||
) | ||
|
||
object LiveScores { | ||
implicit val localDateWrites: Writes[LocalDate] = Writes[LocalDate] { date => | ||
JsString(date.format(DateTimeFormatter.ISO_LOCAL_DATE)) | ||
} | ||
|
||
implicit val stageFormat: Writes[Stage] = Json.writes[Stage] | ||
implicit val roundFormat: Writes[Round] = Json.writes[Round] | ||
implicit val matchDayTeamFormat: Writes[MatchDayTeam] = Json.writes[MatchDayTeam] | ||
implicit val venueFormat: Writes[Venue] = Json.writes[Venue] | ||
implicit val paCompetitionFormat: Writes[PaCompetition] = Json.writes[PaCompetition] | ||
implicit val officialFormat: Writes[Official] = Json.writes[Official] | ||
|
||
// Writes for Fixture with a type discriminator | ||
implicit val fixtureWrites: Writes[Fixture] = Writes { fixture => | ||
Json.writes[Fixture].writes(fixture).as[JsObject] + ("type" -> JsString("Fixture")) | ||
} | ||
|
||
// Writes for MatchDay with a type discriminator | ||
implicit val matchDayWrites: Writes[MatchDay] = Writes { matchDay => | ||
Json.writes[MatchDay].writes(matchDay).as[JsObject] + ("type" -> JsString("MatchDay")) | ||
} | ||
|
||
// Writes for Result with a type discriminator | ||
implicit val resultWrites: Writes[Result] = Writes { result => | ||
Json.writes[Result].writes(result).as[JsObject] + ("type" -> JsString("Result")) | ||
} | ||
|
||
// Writes for LiveMatch with a type discriminator | ||
implicit val liveMatchWrites: Writes[LiveMatch] = Writes { liveMatch => | ||
Json.writes[LiveMatch].writes(liveMatch).as[JsObject] + ("type" -> JsString("LiveMatch")) | ||
} | ||
|
||
implicit val footballMatchWrites: Writes[FootballMatch] = Writes { matchInstance => | ||
matchInstance match { | ||
case f: Fixture => Json.toJson(f)(fixtureWrites) | ||
case m: MatchDay => Json.toJson(m)(matchDayWrites) | ||
case r: Result => Json.toJson(r)(resultWrites) | ||
case l: LiveMatch => Json.toJson(l)(liveMatchWrites) | ||
} | ||
} | ||
|
||
implicit val leagueStatsWrites: Writes[LeagueStats] = Json.writes[LeagueStats] | ||
implicit val leagueTeamWrites: Writes[LeagueTeam] = Json.writes[LeagueTeam] | ||
implicit val leagueTableEntryWrites: Writes[LeagueTableEntry] = Json.writes[LeagueTableEntry] | ||
|
||
implicit val competitionFormat: Writes[Competition] = Json.writes[Competition] | ||
implicit val competitionMatchesFormat: Writes[CompetitionMatches] = Json.writes[CompetitionMatches] | ||
implicit val dateCompetitionMatchesFormat: Writes[DateCompetitionMatches] = Json.writes[DateCompetitionMatches] | ||
implicit val SportsFormat: Writes[LiveScores] = Json.writes[LiveScores] | ||
|
||
def toJson(model: LiveScores): String = { | ||
val jsValue = Json.toJson(model) | ||
Json.stringify(withoutNull(jsValue)) | ||
} | ||
} |