Created
August 6, 2012 09:37
-
-
Save svdberg/3272738 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) | |
import Data.Maybe | |
{- 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 | |
let userKey = getUserId muser | |
u <- runDB $ update fid [ UserId =. userKey] --link the feeding to the user | |
sendResponseCreated $ FeedingR fid --return the id | |
getUserId userEntity = case userEntity of | |
Just (Entity k s) -> k | |
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
Handler/Feeding.hs:27:24:
No instance for (aeson-0.6.0.2:Data.Aeson.Types.Class.FromJSON
(UserGeneric mongoDB-1.2.0:Database.MongoDB.Query.Action))
arising from a use of
parseJsonBody_' Possible fix: add an instance declaration for (aeson-0.6.0.2:Data.Aeson.Types.Class.FromJSON (UserGeneric mongoDB-1.2.0:Database.MongoDB.Query.Action)) In a stmt of a 'do' block: parsedFeeding <- parseJsonBody_ In the expression: do { muser <- maybeAuth; parsedFeeding <- parseJsonBody_; fid <- runDB $ insert parsedFeeding; let userKey = getUserId muser; .... } In an equation for
postFeedingsR':postFeedingsR
= do { muser <- maybeAuth;
parsedFeeding <- parseJsonBody_;
fid <- runDB $ insert parsedFeeding;
.... }
Handler/Feeding.hs:31:38:
Couldn't match expected type
Feeding' with actual type
UserGeneric backend0'Expected type: FeedingId
Actual type: Key
mongoDB-1.2.0:Database.MongoDB.Query.Action (UserGeneric backend0)
In the first argument of
FeedingR', namely
fid'In the second argument of
($)', namely
FeedingR fid'