]> gitweb.michael.orlitzky.com - dead/htsn-import.git/blobdiff - src/TSN/XML/Odds.hs
Add the FromXmlFk class, like FromXml except it requires an FK (old idea).
[dead/htsn-import.git] / src / TSN / XML / Odds.hs
index 467dbcf8ee6df3bf69241f35e5bad97cff2a3e1d..5551bee08f48b5c6ce1fd08f33448eca221ce7aa 100644 (file)
@@ -27,7 +27,7 @@ where
 
 -- System imports.
 import Control.Monad ( forM_, join )
-import Data.Time ( UTCTime )
+import Data.Time ( UTCTime(..) )
 import Data.Tuple.Curry ( uncurryN )
 import Database.Groundhog (
   (=.),
@@ -64,7 +64,7 @@ import TSN.Codegen (
 import TSN.DbImport ( DbImport(..), ImportResult(..), run_dbmigrate )
 import TSN.Picklers ( xp_date, xp_time )
 import TSN.XmlImport ( XmlImport(..) )
-import Xml ( FromXml(..), pickle_unpickle, unpickleable )
+import Xml ( FromXml(..), ToDb(..), pickle_unpickle, unpickleable )
 
 
 
@@ -102,12 +102,13 @@ data OddsCasino =
   deriving (Eq, Show)
 
 
-instance FromXml OddsGameCasinoXml where
+instance ToDb OddsGameCasinoXml where
   -- | The database representation of an 'OddsGameCasinoXml' is an
   --   'OddsCasino'.
   --
   type Db OddsGameCasinoXml = OddsCasino
 
+instance FromXml OddsGameCasinoXml where
   -- | We convert from XML to the database by dropping the line field.
   from_xml OddsGameCasinoXml{..} =
     OddsCasino {
@@ -153,12 +154,13 @@ data OddsGameHomeTeamXml =
     xml_home_casinos         :: [OddsGameCasinoXml] }
   deriving (Eq, Show)
 
-instance FromXml OddsGameHomeTeamXml where
+instance ToDb OddsGameHomeTeamXml where
   -- | The database representation of an 'OddsGameHomeTeamXml' is an
   --   'OddsGameTeam'.
   --
   type Db OddsGameHomeTeamXml = OddsGameTeam
 
+instance FromXml OddsGameHomeTeamXml where
   -- | We convert from XML to the database by dropping the lines and
   --   rotation number (which are specific to the games, not the teams
   --   themselves).
@@ -192,12 +194,13 @@ data OddsGameAwayTeamXml =
     xml_away_casinos         :: [OddsGameCasinoXml] }
   deriving (Eq, Show)
 
-instance FromXml OddsGameAwayTeamXml where
+instance ToDb OddsGameAwayTeamXml where
   -- | The database representation of an 'OddsGameAwayTeamXml' is an
   --   'OddsGameTeam'.
   --
   type Db OddsGameAwayTeamXml = OddsGameTeam
 
+instance FromXml OddsGameAwayTeamXml where
   -- | We convert from XML to the database by dropping the lines and
   --   rotation number (which are specific to the games, not the teams
   --   themselves).
@@ -257,8 +260,7 @@ data OddsGameLine =
 data OddsGame =
   OddsGame {
     db_game_id           :: Int,
-    db_game_date         :: UTCTime,
-    db_game_time         :: UTCTime,
+    db_game_time         :: UTCTime, -- ^ Contains both the date and time.
     db_game_away_team_rotation_number :: Int,
     db_game_home_team_rotation_number :: Int }
   deriving (Eq, Show)
@@ -268,8 +270,8 @@ data OddsGame =
 data OddsGameXml =
   OddsGameXml {
     xml_game_id         :: Int,
-    xml_game_date       :: UTCTime,
-    xml_game_time       :: UTCTime,
+    xml_game_date       :: UTCTime, -- ^ Contains only the date
+    xml_game_time       :: UTCTime, -- ^ Contains only the time
     xml_game_away_team  :: OddsGameAwayTeamXml,
     xml_game_home_team  :: OddsGameHomeTeamXml,
     xml_game_over_under :: OddsGameOverUnderXml }
@@ -282,12 +284,13 @@ xml_game_over_under_casinos :: OddsGameXml -> [OddsGameCasinoXml]
 xml_game_over_under_casinos = xml_casinos . xml_game_over_under
 
 
-instance FromXml OddsGameXml where
+instance ToDb OddsGameXml where
   -- | The database representation of an 'OddsGameXml' is an
   --   'OddsGame'.
   --
   type Db OddsGameXml = OddsGame
 
+instance FromXml OddsGameXml where
   -- | To convert from the XML representation to the database one, we
   --   drop the home/away teams and the casino lines, but retain the
   --   home/away rotation numbers.
@@ -295,10 +298,14 @@ instance FromXml OddsGameXml where
   from_xml OddsGameXml{..} =
     OddsGame {
       db_game_id   = xml_game_id,
-      db_game_date = xml_game_date,
-      db_game_time = xml_game_time,
+
+      db_game_time = UTCTime
+                       (utctDay xml_game_date) -- Take the day part from one,
+                       (utctDayTime xml_game_time), -- the time from the other.
+
       db_game_away_team_rotation_number =
         (xml_away_rotation_number xml_game_away_team),
+
       db_game_home_team_rotation_number =
         (xml_home_rotation_number xml_game_home_team) }
 
@@ -367,11 +374,12 @@ xml_games :: Message -> [OddsGameXml]
 xml_games m = map game (xml_games_with_notes m)
 
 
-instance FromXml Message where
+instance ToDb Message where
   -- | The database representation of a 'Message' is 'Odds'.
   --
   type Db Message = Odds
 
+instance FromXml Message where
   -- | To convert from the XML representation to the database one, we
   --   just drop a bunch of fields.
   --
@@ -431,11 +439,27 @@ mkPersist tsn_codegen_config [groundhog|
       fields:
         - name: odds_OddsGame0 # Default created by mkNormalFieldName
           dbName: odds_id
+          reference:
+            onDelete: cascade
         - name: odds_OddsGame1 # Default created by mkNormalFieldName
           dbName: odds_games_id
+          reference:
+            onDelete: cascade
 
 - entity: OddsGame_OddsGameTeam
   dbName: odds_games__odds_games_teams
+  constructors:
+    - name: OddsGame_OddsGameTeam
+      fields:
+        - name: ogogt_odds_games_id
+          reference:
+            onDelete: cascade
+        - name: ogogt_away_team_id
+          reference:
+            onDelete: cascade
+        - name: ogogt_home_team_id
+          reference:
+            onDelete: cascade
 |]
 
 instance DbImport Message where
@@ -670,7 +694,7 @@ odds_tests =
 
 
 -- | If we unpickle something and then pickle it, we should wind up
---   with the same thing we started with. WARNING: succeess of this
+--   with the same thing we started with. WARNING: success of this
 --   test does not mean that unpickling succeeded.
 --
 test_pickle_of_unpickle_is_identity :: TestTree