summaryrefslogtreecommitdiff
path: root/Model.hs
blob: 0339c5975db24520e25ce7255a2b394697fee7c5 (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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
{-# 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)
        , "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

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
        ]