Advertisement
Guest User

Untitled

a guest
May 14th, 2021
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.69 KB | None | 0 0
  1. {-# LANGUAGE DeriveGeneric #-}
  2. {-# LANGUAGE DataKinds #-}
  3. {-# LANGUAGE TypeOperators #-}
  4. {-# LANGUAGE OverloadedStrings #-}
  5.  
  6. module Main where
  7. import GHC.Generics
  8.  
  9. import Servant
  10. import Data.Aeson.Types (ToJSON, FromJSON)
  11. import Data.Text (Text)
  12. import qualified Data.Text as T
  13.  
  14. type API = "position" :> Capture "x" Int :> Capture "y" Int :> Get '[JSON] Position
  15. :<|> "hello" :> QueryParam "name" Text :> Get '[JSON] HelloMessage
  16. :<|> "marketing" :> ReqBody '[JSON] ClientInfo :> Post '[JSON] Email
  17.  
  18. data Position = Position
  19. { xCoord :: Int
  20. , yCoord :: Int
  21. } deriving Generic
  22.  
  23. instance ToJSON Position
  24.  
  25. newtype HelloMessage = HelloMessage {
  26. msg :: Text
  27. } deriving Generic
  28.  
  29. instance ToJSON HelloMessage
  30.  
  31. data ClientInfo = ClientInfo
  32. { clientName :: Text
  33. , clientEmail :: Text
  34. , clientAge :: Int
  35. , clientInterestedIn :: [Text]
  36. } deriving Generic
  37.  
  38. instance FromJSON ClientInfo
  39. instance ToJSON ClientInfo
  40.  
  41. data Email = Email
  42. { from :: Text
  43. , to :: Text
  44. , subject :: Text
  45. , body :: Text
  46. } deriving Generic
  47.  
  48. instance ToJSON Email
  49.  
  50. emailForClient :: ClientInfo -> Email
  51. emailForClient client = Email from' to' sub' body'
  52. where
  53. from' = "great@company.com"
  54. to' = clientEmail client
  55. sub'= "Hey " <> clientName client <> " we miss you!"
  56. body' :: Text
  57. body'= "Hi " <> clientName client <> T.pack "\n\n"
  58. <> T.pack "since you've recently turned " <> T.pack (show $ clientAge client)
  59. <> T.pack ", Have you checked out our latest "
  60. <> T.intercalate (T.pack ", ") (clientInterestedIn client)
  61. <> T.pack " products? Give us a visit!"
  62.  
  63. main :: IO ()
  64. main = do
  65. putStrLn "Hello, world!"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement