Skip to content

Instantly share code, notes, and snippets.

@svdberg
Created August 6, 2012 09:37
Show Gist options
  • Save svdberg/3272738 to your computer and use it in GitHub Desktop.
Save svdberg/3272738 to your computer and use it in GitHub Desktop.
{-# 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
@svdberg
Copy link
Author

svdberg commented Aug 6, 2012

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 forpostFeedingsR':
postFeedingsR
= do { muser <- maybeAuth;
parsedFeeding <- parseJsonBody_;
fid <- runDB $ insert parsedFeeding;
.... }

Handler/Feeding.hs:31:38:
Couldn't match expected type Feeding' with actual typeUserGeneric backend0'
Expected type: FeedingId
Actual type: Key
mongoDB-1.2.0:Database.MongoDB.Query.Action (UserGeneric backend0)
In the first argument of FeedingR', namelyfid'
In the second argument of ($)', namelyFeedingR fid'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment