+instance Child OddsGameXml where
+ -- | Each 'OddsGameXml' is contained in an 'Odds'. In other words
+ -- the foreign key for 'OddsGame' points to an 'Odds'.
+ --
+ type Parent OddsGameXml = Odds
+
+
+instance FromXmlFkTeams OddsGameXml where
+ -- | To convert from the XML representation to the database one, we
+ -- drop the casino lines, but retain the home/away rotation
+ -- numbers and the starters. The foreign keys to 'Odds' and the
+ -- home/away teams are passed in.
+ --
+ from_xml_fk_teams fk fk_away fk_home OddsGameXml{..} =
+ OddsGame {
+ db_odds_id = fk,
+ db_away_team_id = fk_away,
+ db_home_team_id = fk_home,
+ db_game_id = xml_game_id,
+
+ db_game_time = make_game_time xml_game_date xml_game_time,
+
+ db_away_team_rotation_number =
+ (xml_team_rotation_number xml_away_team),
+
+ db_home_team_rotation_number =
+ (xml_team_rotation_number xml_home_team),
+
+ db_away_team_starter_id =
+ (xml_starter_id <$> xml_team_starter xml_away_team),
+
+ -- Sometimes the starter element is present but the name isn't,
+ -- so we combine the two maybes with join.
+ db_away_team_starter_name = join
+ (xml_starter_name <$> xml_team_starter xml_away_team),
+
+ db_home_team_starter_id =
+ (xml_starter_id <$> xml_team_starter xml_home_team),
+
+ -- Sometimes the starter element is present but the name isn't,
+ -- so we combine the two maybes with join.
+ db_home_team_starter_name = join
+ (xml_starter_name <$> xml_team_starter xml_home_team) }
+ where
+ -- | Construct the database game time from the XML \<Game_Date\>
+ -- and \<Game_Time\> elements. The \<Game_Time\> elements
+ -- sometimes have a value of \"TBA\"; in that case, we don't
+ -- want to pretend that we know the time by setting it to
+ -- e.g. midnight, so instead we make the entire date/time
+ -- Nothing.
+ make_game_time :: UTCTime -> Maybe UTCTime -> Maybe UTCTime
+ make_game_time _ Nothing = Nothing
+ make_game_time d (Just t) = Just $ UTCTime (utctDay d) (utctDayTime t)
+
+
+-- | This lets us insert the XML representation 'OddsGameXml' directly.
+--
+instance XmlImportFkTeams OddsGameXml