xpWrap )
-- Local imports.
-import TSN.Codegen (
- tsn_codegen_config )
+import TSN.Codegen ( tsn_codegen_config )
+import TSN.Database ( insert_or_select )
import TSN.DbImport ( DbImport(..), ImportResult(..), run_dbmigrate )
import TSN.Picklers ( xp_date_padded, xp_tba_time, xp_time_stamp )
-import TSN.Team ( Team(..) )
+import TSN.Team ( Team(..), HTeam(..), VTeam(..) )
import TSN.XmlImport ( XmlImport(..) )
import Xml (
FromXml(..),
xml_game_date :: UTCTime,
xml_game_time :: Maybe UTCTime,
xml_location :: Maybe String,
- xml_away_team :: ScheduleChangesListingTeamXml,
- xml_home_team :: ScheduleChangesListingTeamXml,
+ xml_away_team :: VTeam,
+ xml_home_team :: HTeam,
xml_vscore :: Int,
xml_hscore :: Int,
xml_listing_status :: ScheduleChangesListingStatus,
--- * ScheduleChangesListingTeamXml
-
--- | The XML representation of a 'ScheduleChangesListing'
--- team. Its corresponding database representation (along with that
--- of the home team) is a "TSN.Team", but their XML representations
--- are slightly different.
---
-data ScheduleChangesListingTeamXml =
- ScheduleChangesListingTeamXml {
- xml_team_id :: String,
- xml_team_name :: Maybe String }
- deriving (Eq, Show)
-
-
-instance ToDb ScheduleChangesListingTeamXml where
- -- | The database analogue of an 'ScheduleChangesListingTeamXml' is
- -- a 'Team'.
- --
- type Db ScheduleChangesListingTeamXml = Team
-
-
-instance FromXml ScheduleChangesListingTeamXml where
- -- | To convert a 'ScheduleChangesListingTeamXml' to a 'Team',
- -- we set the non-existent abbreviation to \"Nothing\".
- --
- from_xml ScheduleChangesListingTeamXml{..} =
- Team {
- team_id = xml_team_id,
- abbreviation = Nothing,
- name = xml_team_name }
-
--- | Allow us to import ScheduleChangesListingTeamXml directly.
---
-instance XmlImport ScheduleChangesListingTeamXml
-
-
-
--
-- * Database stuff.
--
-- Now loop through the listings so that we can handle the teams
-- one listing at a time.
forM_ (xml_sc_listings sc) $ \listing -> do
- away_team_id <- insert_xml_or_select (xml_away_team listing)
- home_team_id <- insert_xml_or_select (xml_home_team listing)
+ away_team_id <- insert_or_select (vteam $ xml_away_team listing)
+ home_team_id <- insert_or_select (hteam $ xml_home_team listing)
-- Finish constructing the xml -> db function.
let listing_xml_to_db' = listing_xml_to_db away_team_id home_team_id
-- | An (un)pickler for the \<Away_Team\> elements.
--
-pickle_away_team :: PU ScheduleChangesListingTeamXml
+pickle_away_team :: PU VTeam
pickle_away_team =
xpElem "Away_Team" $
xpWrap (from_tuple, to_tuple) $
xpPair (xpAttr "AT_ID" xpText)
(xpOption xpText)
where
- from_tuple = uncurry ScheduleChangesListingTeamXml
- to_tuple t = (xml_team_id t,
- xml_team_name t)
+ from_tuple (x,y) = VTeam (Team x Nothing y)
+ to_tuple (VTeam t) = (team_id t, name t)
-- | An (un)pickler for the \<Away_Team\> elements.
--
-pickle_home_team :: PU ScheduleChangesListingTeamXml
+pickle_home_team :: PU HTeam
pickle_home_team =
xpElem "Home_Team" $
xpWrap (from_tuple, to_tuple) $
xpPair (xpAttr "HT_ID" xpText)
(xpOption xpText)
where
- from_tuple = uncurry ScheduleChangesListingTeamXml
- to_tuple t = (xml_team_id t,
- xml_team_name t)
+ from_tuple (x,y) = HTeam (Team x Nothing y)
+ to_tuple (HTeam t) = (team_id t, name t)
-- | An (un)pickler for the \<status\> elements.