on bfb64cd help sign in
52 lines 1.3 kB history raw
1{-# LANGUAGE CPP #-}
2{-# LANGUAGE DataKinds #-}
3{-# LANGUAGE DeriveGeneric #-}
4{-# LANGUAGE TypeFamilies #-}
5{-# LANGUAGE TypeOperators #-}
6module T2 where
8import Data.Aeson
9import Data.Time.Calendar
10import GHC.Generics
11import Network.Wai
12import Servant
14data User = User
15 { name :: String
16 , age :: Int
17 , email :: String
18 , registration_date :: Day
19 } deriving (Eq, Show, Generic)
21#if !MIN_VERSION_aeson(0,10,0)
22-- orphan ToJSON instance for Day. necessary to derive one for User
23instance ToJSON Day where
24 -- display a day in YYYY-mm-dd format
25 toJSON d = toJSON (showGregorian d)
26#endif
28instance ToJSON User
30type UserAPI = "users" :> Get '[JSON] [User]
31 :<|> "albert" :> Get '[JSON] User
32 :<|> "isaac" :> Get '[JSON] User
34isaac :: User
35isaac = User "Isaac Newton" 372 "isaac@newton.co.uk" (fromGregorian 1683 3 1)
37albert :: User
38albert = User "Albert Einstein" 136 "ae@mc2.org" (fromGregorian 1905 12 1)
40users :: [User]
41users = [isaac, albert]
43userAPI :: Proxy UserAPI
44userAPI = Proxy
46server :: Server UserAPI
47server = return users
48 :<|> return albert
49 :<|> return isaac
51app :: Application
52app = serve userAPI server