]> gitweb.michael.orlitzky.com - dead/htsn-import.git/blobdiff - src/TSN/XML/Weather.hs
Add time_stamp and xml_file_id to Weather.
[dead/htsn-import.git] / src / TSN / XML / Weather.hs
index 3773b312bc021af4a78a97b93c85db4f02e736b4..4951951c3d47101cf531f7125e1fe451696361aa 100644 (file)
@@ -50,11 +50,14 @@ import Text.XML.HXT.Core (
 import TSN.Codegen (
   tsn_codegen_config )
 import TSN.DbImport ( DbImport(..), ImportResult(..), run_dbmigrate )
-import TSN.Picklers ( xp_gamedate )
+import TSN.Picklers ( xp_gamedate, xp_time_stamp )
 import TSN.XmlImport ( XmlImport(..) )
-import Xml ( FromXml(..), pickle_unpickle, unpickleable )
+import Xml ( FromXml(..), ToDb(..), pickle_unpickle, unpickleable )
 
 
+--
+-- DB/XML Data types
+--
 
 -- | Database/XML representation of a weather forecast listing.
 --
@@ -64,12 +67,14 @@ data WeatherForecastListing =
     db_weather :: String }
   deriving (Eq, Show)
 
+-- | The database analogue of a 'WeatherForecastListing' is itself.
+instance ToDb WeatherForecastListing where
+  type Db WeatherForecastListing = WeatherForecastListing
+
 -- | This is needed to define the XmlImport instance for
--- 'WeatherForecastListing'; it basically says that the DB
--- representation is the same as the XML representation.
+-- 'WeatherForecastListing'.
 --
 instance FromXml WeatherForecastListing where
-  type Db WeatherForecastListing = WeatherForecastListing
   from_xml = id
 
 -- | Allows us to call 'insert_xml' on the XML representation of
@@ -110,12 +115,13 @@ data WeatherForecastXml =
   deriving (Eq, Show)
 
 
-instance FromXml WeatherForecastXml where
+instance ToDb WeatherForecastXml where
   -- | The database representation of a 'WeatherForecastXml' is a
   --   'WeatherForecast'.
   --
   type Db WeatherForecastXml = WeatherForecast
 
+instance FromXml WeatherForecastXml where
   -- | To convert a 'WeatherForecastXml' into a 'WeatherForecast', we
   --   replace the 'WeatherLeague' with its name.
   --
@@ -133,8 +139,10 @@ instance XmlImport WeatherForecastXml
 --
 data Weather =
   Weather {
+    db_xml_file_id :: Int,
     db_sport :: String,
-    db_title :: String }
+    db_title :: String,
+    db_time_stamp :: UTCTime }
 
 
 -- | The XML representation of a weather message.
@@ -147,22 +155,25 @@ data Message =
     xml_sport :: String,
     xml_title :: String,
     xml_forecasts :: [WeatherForecastXml],
-    xml_time_stamp :: String }
+    xml_time_stamp :: UTCTime }
   deriving (Eq, Show)
 
 
-instance FromXml Message where
+instance ToDb Message where
   -- | The database representation of 'Message' is 'Weather'.
   --
   type Db Message = Weather
 
+instance FromXml Message where
   -- | To get a 'Weather' from a 'Message', we drop a bunch of
   --   unwanted fields.
   --
   from_xml Message{..} =
     Weather {
+      db_xml_file_id = xml_xml_file_id,
       db_sport = xml_sport,
-      db_title = xml_title }
+      db_title = xml_title,
+      db_time_stamp = xml_time_stamp }
 
 -- | This allows us to call 'insert_xml' on a 'Message' without first
 --   converting it to the database representation.
@@ -188,6 +199,13 @@ data WeatherForecast_WeatherForecastListing =
 
 mkPersist tsn_codegen_config [groundhog|
 - entity: Weather
+  constructors:
+    - name: Weather
+      uniques:
+        - name: unique_weather
+          type: constraint
+          # Prevent multiple imports of the same message.
+          fields: [db_xml_file_id]
 
 - entity: WeatherForecast
   dbName: weather_forecasts
@@ -309,7 +327,7 @@ pickle_message =
         (xpElem "sport" xpText)
         (xpElem "title" xpText)
         (xpList pickle_forecast)
-        (xpElem "time_stamp" xpText)
+        (xpElem "time_stamp" xp_time_stamp)
   where
     from_tuple = uncurryN Message
     to_tuple Message{..} = (xml_xml_file_id,