From: Michael Orlitzky Date: Fri, 4 Jul 2014 06:40:15 +0000 (-0400) Subject: Add the XmlImportFkTeams class. X-Git-Tag: 0.0.6~42 X-Git-Url: http://gitweb.michael.orlitzky.com/?p=dead%2Fhtsn-import.git;a=commitdiff_plain;h=663bba70fe9780a7b881ecf664e40faa62db2900 Add the XmlImportFkTeams class. --- diff --git a/src/TSN/XmlImport.hs b/src/TSN/XmlImport.hs index 5693bd4..0779ee4 100644 --- a/src/TSN/XmlImport.hs +++ b/src/TSN/XmlImport.hs @@ -5,9 +5,11 @@ -- module TSN.XmlImport ( XmlImport(..), - XmlImportFk(..) ) + XmlImportFk(..), + XmlImportFkTeams(..) ) where +-- System imports. import Database.Groundhog ( AutoKey, DefaultKey, @@ -16,7 +18,15 @@ import Database.Groundhog ( 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 @@ -69,7 +79,7 @@ class (FromXml a, PersistEntity (Db a)) => XmlImport a where -- | 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 @@ -92,3 +102,52 @@ class (FromXmlFk a, PersistEntity (Db a)) => XmlImportFk a where 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