Created
August 6, 2012 06:42
-
-
Save svdberg/3271593 to your computer and use it in GitHub Desktop.
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
{-# LANGUAGE TupleSections, OverloadedStrings #-} | |
module Handler.Feeding where | |
import Import | |
import Network.HTTP.Types (status201, status204, status200) | |
{- This module contains the Feeding resource -} | |
getFeedingR :: FeedingId -> Handler RepJson | |
getFeedingR fid = runDB (get404 fid) >>= jsonToRepJson . Entity fid | |
deleteFeedingR :: FeedingId -> Handler () | |
deleteFeedingR fid = do --incomming Id is Base64, mongo expects Base16 | |
runDB (delete fid) | |
sendResponseStatus status200 () | |
putFeedingR :: FeedingId -> Handler () | |
putFeedingR fid = do --incomming Id is Base64, mongo expects Base16 | |
feeding <- parseJsonBody_ | |
runDB $ replace fid feeding | |
sendResponseStatus status200 () | |
postFeedingsR :: Handler RepJson | |
postFeedingsR = do | |
muser <- maybeAuth | |
parsedFeeding <- parseJsonBody_ --get content as JSON | |
fid <- runDB $ insert parsedFeeding --store in mongo | |
u <- runDB $ update fid [ UserId =. muser] --link the feeding to the user | |
sendResponseCreated $ FeedingR fid --return the id | |
getFeedingsR :: Handler RepJson | |
getFeedingsR = runDB (selectList [] [Desc FeedingDate, Desc FeedingTime]) >>= jsonToRepJson . asFeedingEntities | |
where | |
asFeedingEntities :: [Entity Feeding] -> [Entity Feeding] | |
asFeedingEntities = id |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Line 28 is wrong according to the all-knowing haskell compiler:
Handler/Feeding.hs:28:20: Couldn't match type
YesodPersistBackend master0' with
mongoDB-1.2.0:Database.MongoDB.Query.Action' Expected type: YesodDB sub0 master0 () Actual type: mongoDB-1.2.0:Database.MongoDB.Query.Action (GHandler sub0 master0) () In the return type of a call ofupdate' In the second argument of
($)', namely `update fid [UserId =. muser]' In a stmt of a 'do' block: u <- runDB $ update fid [UserId =. muser]Handler/Feeding.hs:28:43:
Couldn't match expected type
Key backend0 (UserGeneric backend0)' with actual type
Maybe (Entity val0)'In the second argument of
(=.)', namely
muser'In the expression: UserId =. muser
In the second argument of
update', namely
[UserId =. muser]'