summaryrefslogtreecommitdiff
path: root/Model.hs
diff options
context:
space:
mode:
authorFélix Sipma <felix.sipma@no-log.org>2014-11-30 18:06:11 +0100
committerFélix Sipma <felix.sipma@no-log.org>2014-11-30 18:06:11 +0100
commit2f47e973d9a35d3a4c01fa0b61e0fb8d6dc4a823 (patch)
tree524484a7e906825c0886fe9bef1f2db10cca6f19 /Model.hs
parent29048e5003142ff1c37351e2eb846cb59ccc4e96 (diff)
parent316ebb8ad0e0febbba7f8ab14600a699419ed115 (diff)
Merge branch 'angular'
Diffstat (limited to 'Model.hs')
-rw-r--r--Model.hs24
1 files changed, 24 insertions, 0 deletions
diff --git a/Model.hs b/Model.hs
index 1d8e80a..98a4d9a 100644
--- a/Model.hs
+++ b/Model.hs
@@ -1,3 +1,4 @@
+{-# LANGUAGE FlexibleInstances #-}
module Model where
import Prelude
@@ -9,6 +10,9 @@ import Data.Time (UTCTime)
import Shop.Unit.Types (Unit)
import Shop.Amount.Types (Amount)
import Shop.Quantity.Types (Quantity)
+import Control.Applicative ((<$>), (<*>))
+import Control.Monad (mzero)
+import Data.Aeson ((.:?))
-- You can define all of your database entities in the entities file.
-- You can find more information on persistent and how to declare entities
@@ -16,3 +20,23 @@ import Shop.Quantity.Types (Quantity)
-- http://www.yesodweb.com/book/persistent/
share [mkPersist sqlSettings, mkMigrate "migrateAll"]
$(persistFileWith lowerCaseSettings "config/models")
+
+instance ToJSON (Entity Shop) where
+ toJSON (Entity sid s) = object
+ [ "id" .= (String $ toPathPiece sid)
+ , "ident" .= (String $ shopIdent s)
+ , "vendor" .= (String $ toPathPiece $ shopVendor s)
+ , "image" .= maybe Null (String . toPathPiece) (shopImage s)
+ , "created" .= shopCreated s
+ , "updated" .= shopUpdated s
+ ]
+
+instance FromJSON Shop where
+ parseJSON (Object o) = Shop
+ <$> o .: "ident"
+ <*> o .: "vendor"
+ <*> o .:? "image"
+ <*> o .: "created"
+ <*> o .: "updated"
+
+ parseJSON _ = mzero