summaryrefslogtreecommitdiff
path: root/Model.hs
blob: f37d5a9aacc3e9645bc11ddeec1aec807892140f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
{-# LANGUAGE FlexibleInstances #-}
module Model where

import ClassyPrelude.Yesod
import Database.Persist.Quasi
import Shop.Unit.Types        (Unit)
import Shop.Amount.Types      (Amount)
import Shop.Quantity.Types    (Quantity)
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)
        , "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