--
module TSN.XmlImport (
XmlImport(..),
- XmlImportFk(..) )
+ XmlImportFk(..),
+ XmlImportFkTeams(..) )
where
+-- System imports.
import Database.Groundhog (
AutoKey,
DefaultKey,
insertByAll )
import Database.Groundhog.Core ( PersistBackend, PersistEntity )
-import Xml ( FromXml(..), FromXmlFk(..), ToDb(..) )
+
+-- Local imports.
+import TSN.Team ( Team(..) )
+import Xml (
+ Child(..),
+ FromXml(..),
+ FromXmlFk(..),
+ FromXmlFkTeams(..),
+ ToDb(..) )
-- | In Groundhog, there is a typeclass of things you can insert into
-- | A total copy of 'XmlImport' for instances of 'FromXmlFk'.
--
-class (FromXmlFk a, PersistEntity (Db a)) => XmlImportFk a where
+class (Child a, FromXmlFk a, PersistEntity (Db a)) => XmlImportFk a where
insert_xml_fk :: (PersistBackend m)
=> DefaultKey (Parent a)
-> a
insert_xml_or_select_fk fk x = do
tmp <- insertByAll_xml_fk fk x
return $ (either id id) tmp
+
+
+-- | A total copy of 'XmlImport' for instances of 'FromXmlFkTeams'.
+-- This is a lot of duplicated boilerplate, but you don't have to
+-- think about it usually. What you're really worried about is that
+-- the dbimport code is understandable, and having these convenience
+-- classes makes the import much simpler since you don't have to do
+-- these conversions on-the-fly.
+--
+class (Child a,
+ FromXmlFkTeams a,
+ PersistEntity (Db a))
+ => XmlImportFkTeams a where
+ insert_xml_fk_teams :: (PersistBackend m)
+ => DefaultKey (Parent a)
+ -> DefaultKey Team -- ^ Away team FK
+ -> DefaultKey Team -- ^ Home team FK
+ -> a
+ -> m (AutoKey (Db a))
+ insert_xml_fk_teams fk fk_away fk_home x =
+ insert $ from_xml_fk_teams fk fk_away fk_home x
+
+ insert_xml_fk_teams_ :: (PersistBackend m)
+ => DefaultKey (Parent a)
+ -> DefaultKey Team
+ -> DefaultKey Team
+ -> a
+ -> m ()
+ insert_xml_fk_teams_ fk fk_away fk_home x =
+ insert_ $ from_xml_fk_teams fk fk_away fk_home x
+
+ insertByAll_xml_fk_teams :: (PersistBackend m)
+ => DefaultKey (Parent a)
+ -> DefaultKey Team
+ -> DefaultKey Team
+ -> a
+ -> m ( Either (AutoKey (Db a)) (AutoKey (Db a)) )
+ insertByAll_xml_fk_teams fk fk_away fk_home x =
+ insertByAll $ from_xml_fk_teams fk fk_away fk_home x
+
+ insert_xml_or_select_fk_teams :: (PersistBackend m)
+ => DefaultKey (Parent a)
+ -> DefaultKey Team
+ -> DefaultKey Team
+ -> a
+ -> m (AutoKey (Db a))
+ insert_xml_or_select_fk_teams fk fk_away fk_home x = do
+ tmp <- insertByAll_xml_fk_teams fk fk_away fk_home x
+ return $ (either id id) tmp