Permalink
Switch branches/tags
Nothing to show
Find file Copy path
d527fdc Aug 19, 2015
1 contributor

Users who have contributed to this file

44 lines (32 sloc) 946 Bytes
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE DeriveGeneric #-}
import Data.Monoid ((<>))
import Data.Aeson (FromJSON, ToJSON)
import GHC.Generics
import Web.Scotty
data User = User { userId :: Int, userName :: String } deriving (Show, Generic)
instance ToJSON User
instance FromJSON User
bob :: User
bob = User { userId = 1, userName = "bob" }
jenny :: User
jenny = User { userId = 2, userName = "jenny" }
allUsers :: [User]
allUsers = [bob, jenny]
matchesId :: Int -> User -> Bool
matchesId id user = userId user == id
main = do
putStrLn "Starting Server..."
scotty 3000 $ do
get "/hello/:name" $ do
name <- param "name"
text ("hello " <> name <> "!")
get "/users" $ do
json allUsers
get "/users/:id" $ do
id <- param "id"
json (filter (matchesId id) allUsers)
-- assignment: post user and print it out
post "/users" $ do
user <- jsonData :: ActionM User
json user