Last active
May 5, 2016 10:24
-
-
Save martintrojer/1803f11f9994656bd0b0b763f1a42854 to your computer and use it in GitHub Desktop.
beyond clojure: haskell
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[persistLowerCase| | |
Player | |
name String | |
level Int | |
deriving Show | |
deriving Generic | |
|] | |
instance ToJSON Player | |
instance FromJSON Player |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- Flattening 2 nested calls that returns Maybe | |
getPlayer' = do | |
player <- runMaybeT $ do | |
param <- MaybeT $ getParam "player" | |
MaybeT . runPersist . getPlayer $ BS.unpack param | |
return player |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
setupDB = do | |
runMigration migrateAll | |
insert_ $ Player "Sally" 2 | |
insert_ $ Player "Lance" 1 | |
insert_ $ Player "Aki" 3 | |
insert_ $ Player "Maria" 4 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
createPlayerHandler = do | |
player <- getPlayer' | |
name <- getParam "player" | |
level <- getPostParam "level" | |
case (player, name, level) of | |
(Nothing, Just n, Just l) -> do | |
runPersist $ createPlayer (BS.unpack n) $ read (BS.unpack l) | |
modifyResponse $ setResponseStatus 201 "Created" | |
(Just _, _, _) -> modifyResponse $ setResponseStatus 400 "Player exists" | |
_ -> notFound |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- Setting up the routes | |
appInit = makeSnaplet "app" "a player db backend" Nothing $ do | |
addRoutes [ ("", ifTop $ writeText "Welcome to the Players API v0.1") | |
, ("players", method GET getPlayersHandler) | |
, ("players/:player", method GET getPlayerHandler <|> | |
method POST createPlayerHandler <|> | |
method DELETE deletePlayerHandler) | |
] | |
d <- nestSnaplet "db" db $ initSqlite setupDB | |
return $ App d | |
-- A simple handler | |
getPlayersHandler = do | |
users <- runPersist getAllPlayers | |
writeJSON users | |
-- A model/db function | |
getAllPlayers = do | |
players <- select $ from $ \player -> do | |
orderBy [asc (player ^. PlayerName)] | |
return player | |
return $ map entityVal players |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment