2015-08-17 23:56:29 +02:00
|
|
|
{-# LANGUAGE DataKinds #-}
|
2015-05-05 22:54:55 +02:00
|
|
|
{-# LANGUAGE DeriveGeneric #-}
|
2015-08-17 23:56:29 +02:00
|
|
|
{-# LANGUAGE TypeFamilies #-}
|
2015-05-05 22:54:55 +02:00
|
|
|
{-# LANGUAGE TypeOperators #-}
|
2015-05-10 13:39:18 +02:00
|
|
|
module T3 where
|
2015-05-05 22:54:55 +02:00
|
|
|
|
2015-09-12 14:11:24 +02:00
|
|
|
import Control.Monad.Trans.Except
|
2015-08-17 23:56:29 +02:00
|
|
|
import Data.Aeson
|
|
|
|
import Data.List
|
|
|
|
import GHC.Generics
|
|
|
|
import Network.Wai
|
|
|
|
import Servant
|
2015-05-05 22:54:55 +02:00
|
|
|
|
|
|
|
data Position = Position
|
|
|
|
{ x :: Int
|
|
|
|
, y :: Int
|
2015-05-07 17:48:21 +02:00
|
|
|
} deriving (Show, Generic)
|
2015-05-05 22:54:55 +02:00
|
|
|
|
2015-05-07 17:48:21 +02:00
|
|
|
instance FromJSON Position
|
2015-05-05 22:54:55 +02:00
|
|
|
instance ToJSON Position
|
|
|
|
|
|
|
|
newtype HelloMessage = HelloMessage { msg :: String }
|
2015-05-07 17:48:21 +02:00
|
|
|
deriving (Show, Generic)
|
2015-05-05 22:54:55 +02:00
|
|
|
|
2015-05-07 17:48:21 +02:00
|
|
|
instance FromJSON HelloMessage
|
2015-05-05 22:54:55 +02:00
|
|
|
instance ToJSON HelloMessage
|
|
|
|
|
|
|
|
data ClientInfo = ClientInfo
|
2015-08-17 23:56:29 +02:00
|
|
|
{ name :: String
|
|
|
|
, email :: String
|
|
|
|
, age :: Int
|
2015-05-05 22:54:55 +02:00
|
|
|
, interested_in :: [String]
|
2015-05-07 17:48:21 +02:00
|
|
|
} deriving (Show, Generic)
|
2015-05-05 22:54:55 +02:00
|
|
|
|
|
|
|
instance FromJSON ClientInfo
|
2015-05-07 17:48:21 +02:00
|
|
|
instance ToJSON ClientInfo
|
2015-05-05 22:54:55 +02:00
|
|
|
|
|
|
|
data Email = Email
|
2015-08-17 23:56:29 +02:00
|
|
|
{ from :: String
|
|
|
|
, to :: String
|
2015-05-05 22:54:55 +02:00
|
|
|
, subject :: String
|
2015-08-17 23:56:29 +02:00
|
|
|
, body :: String
|
2015-05-07 17:48:21 +02:00
|
|
|
} deriving (Show, Generic)
|
2015-05-05 22:54:55 +02:00
|
|
|
|
2015-05-07 17:48:21 +02:00
|
|
|
instance FromJSON Email
|
2015-05-05 22:54:55 +02:00
|
|
|
instance ToJSON Email
|
|
|
|
|
|
|
|
emailForClient :: ClientInfo -> Email
|
|
|
|
emailForClient c = Email from' to' subject' body'
|
|
|
|
|
|
|
|
where from' = "great@company.com"
|
|
|
|
to' = email c
|
|
|
|
subject' = "Hey " ++ name c ++ ", we miss you!"
|
|
|
|
body' = "Hi " ++ name c ++ ",\n\n"
|
|
|
|
++ "Since you've recently turned " ++ show (age c)
|
|
|
|
++ ", have you checked out our latest "
|
|
|
|
++ intercalate ", " (interested_in c)
|
2015-05-06 00:20:54 +02:00
|
|
|
++ " products? Give us a visit!"
|
2015-05-05 22:54:55 +02:00
|
|
|
|
|
|
|
type API = "position" :> Capture "x" Int :> Capture "y" Int :> Get '[JSON] Position
|
|
|
|
:<|> "hello" :> QueryParam "name" String :> Get '[JSON] HelloMessage
|
|
|
|
:<|> "marketing" :> ReqBody '[JSON] ClientInfo :> Post '[JSON] Email
|
|
|
|
|
|
|
|
api :: Proxy API
|
|
|
|
api = Proxy
|
|
|
|
|
|
|
|
server :: Server API
|
|
|
|
server = position
|
|
|
|
:<|> hello
|
|
|
|
:<|> marketing
|
|
|
|
|
2015-09-12 14:11:24 +02:00
|
|
|
where position :: Int -> Int -> ExceptT ServantErr IO Position
|
2015-05-05 22:54:55 +02:00
|
|
|
position x y = return (Position x y)
|
|
|
|
|
2015-09-12 14:11:24 +02:00
|
|
|
hello :: Maybe String -> ExceptT ServantErr IO HelloMessage
|
2015-05-05 22:54:55 +02:00
|
|
|
hello mname = return . HelloMessage $ case mname of
|
|
|
|
Nothing -> "Hello, anonymous coward"
|
|
|
|
Just n -> "Hello, " ++ n
|
|
|
|
|
2015-09-12 14:11:24 +02:00
|
|
|
marketing :: ClientInfo -> ExceptT ServantErr IO Email
|
2015-05-05 22:54:55 +02:00
|
|
|
marketing clientinfo = return (emailForClient clientinfo)
|
|
|
|
|
|
|
|
app :: Application
|
2016-01-07 12:51:30 +01:00
|
|
|
app = serve api EmptyConfig server
|