Last active
August 29, 2015 14:19
-
-
Save cschneid/6f22389f722670a2040b 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 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