Skip to content

Instantly share code, notes, and snippets.

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

svdberg commented Aug 6, 2012

Line 28 is wrong according to the all-knowing haskell compiler:
Handler/Feeding.hs:28:20: Couldn't match type YesodPersistBackend master0' withmongoDB-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 of update' 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 typeMaybe (Entity val0)'
In the second argument of (=.)', namelymuser'
In the expression: UserId =. muser
In the second argument of update', namely[UserId =. muser]'

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