{-# LANGUAGE FlexibleInstances #-} module Model where import ClassyPrelude.Yesod import Database.Persist.Quasi import Model.Amount (Amount) import Model.Point (Point (..)) import Model.Quantity (Quantity) import Model.Unit (Unit) 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 -- at: -- 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) , "location" .= [ toJSON $ pntLon $ shopLocation s, toJSON $ pntLat $ shopLocation s ] , "created" .= shopCreated s , "updated" .= shopUpdated s ] instance FromJSON Shop where parseJSON (Object o) = Shop <$> o .: "ident" <*> o .: "vendor" <*> o .:? "image" <*> o .: "location" <*> o .: "created" <*> o .: "updated" parseJSON _ = mzero instance ToJSON (Entity Place) where toJSON (Entity pid p) = object [ "geometry" .= object [ "type" .= String "Point" , "coordinates" .= [ toJSON $ pntLon $ placeLocation p, toJSON $ pntLat $ placeLocation p ] ] , "type" .= String "Feature" , "properties" .= object [ "id" .= (String $ toPathPiece pid) , "ident" .= (String $ placeIdent p) ] ] instance ToJSON [Entity Place] where toJSON l = object [ "crs" .= object [ "type" .= String "name" , "properties" .= object [ "name" .= String "urn:ogc:def:crs:OGC:1.3:CRS84" ] ] , "type" .= String "FeatureCollection" , "features" .= map toJSON l ]