Skip to content

Instantly share code, notes, and snippets.

@cschneid
Last active August 29, 2015 14:19
Show Gist options
  • Save cschneid/6f22389f722670a2040b to your computer and use it in GitHub Desktop.
Save cschneid/6f22389f722670a2040b to your computer and use it in GitHub Desktop.
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE FunctionalDependencies #-}
module Grocery.Database.Calendar where
import Grocery.DatabaseSchema
import Grocery.Types.Meal
import Database.Persist
import Database.Persist.Sqlite
import qualified Database.Esqueleto as E
import Database.Esqueleto ((^.))
import Data.Time
import Control.Monad.Trans -- for MonadIO
getMeals :: (MonadIO m) => DbUserId -> SqlPersistT m [Meal]
getMeals user = do
r <- E.select $
E.from $ \(m `E.InnerJoin` u) -> do
E.on (m ^. DbMealUserId E.==. u ^. DbUserId)
E.where_ (m ^. DbMealUserId E.==. E.val user )
return m
return $ map (deserializeDb . entityVal) r
class DeserializeDb a r | a -> r where
deserializeDb :: a -> r
instance DeserializeDb DbMeal Meal where
deserializeDb db = let d = dbMealDay db
n = dbMealName db
in Meal Nothing (utctDay d) n []
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment