Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- {-# LANGUAGE DeriveGeneric #-}
- {-# LANGUAGE DataKinds #-}
- {-# LANGUAGE TypeOperators #-}
- {-# LANGUAGE OverloadedStrings #-}
- module Main where
- import GHC.Generics
- import Servant
- import Data.Aeson.Types (ToJSON, FromJSON)
- import Data.Text (Text)
- import qualified Data.Text as T
- type API = "position" :> Capture "x" Int :> Capture "y" Int :> Get '[JSON] Position
- :<|> "hello" :> QueryParam "name" Text :> Get '[JSON] HelloMessage
- :<|> "marketing" :> ReqBody '[JSON] ClientInfo :> Post '[JSON] Email
- data Position = Position
- { xCoord :: Int
- , yCoord :: Int
- } deriving Generic
- instance ToJSON Position
- newtype HelloMessage = HelloMessage {
- msg :: Text
- } deriving Generic
- instance ToJSON HelloMessage
- data ClientInfo = ClientInfo
- { clientName :: Text
- , clientEmail :: Text
- , clientAge :: Int
- , clientInterestedIn :: [Text]
- } deriving Generic
- instance FromJSON ClientInfo
- instance ToJSON ClientInfo
- data Email = Email
- { from :: Text
- , to :: Text
- , subject :: Text
- , body :: Text
- } deriving Generic
- instance ToJSON Email
- emailForClient :: ClientInfo -> Email
- emailForClient client = Email from' to' sub' body'
- where
- from' = "great@company.com"
- to' = clientEmail client
- sub'= "Hey " <> clientName client <> " we miss you!"
- body' :: Text
- body'= "Hi " <> clientName client <> T.pack "\n\n"
- <> T.pack "since you've recently turned " <> T.pack (show $ clientAge client)
- <> T.pack ", Have you checked out our latest "
- <> T.intercalate (T.pack ", ") (clientInterestedIn client)
- <> T.pack " products? Give us a visit!"
- main :: IO ()
- main = do
- putStrLn "Hello, world!"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement