Skip to content

Instantly share code, notes, and snippets.

@StevenXL
Created January 18, 2018 14:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save StevenXL/edbb2b503a248d25b435eba3d98ab580 to your computer and use it in GitHub Desktop.
Save StevenXL/edbb2b503a248d25b435eba3d98ab580 to your computer and use it in GitHub Desktop.
Separate Query
-- WORKING CODE
getCategoryR :: Handler Html
getCategoryR = do
allCategories <- runDB $ selectList [] [] :: Handler [Entity Category]
let badges = []
categoriesWithExpenseCount <- categoriesWithExpenseCount
(formWidget, formEnctype) <- generateFormPost categoryForm
defaultLayout $(widgetFile "category")
categoriesWithExpenseCount :: Handler [(Entity Category, E.Value Int)]
categoriesWithExpenseCount = runDB $ E.select $ E.from $ \(e `E.InnerJoin` c) -> do
E.on (e E.^. ExpenseCategoryId E.==. c E.^. CategoryId)
E.groupBy (c E.^. CategoryId)
let cr = E.count (e E.^. ExpenseAmount)
return (c, cr)
-- WITH SEPARATE QUERY
getCategoryR :: Handler Html
getCategoryR = do
allCategories <- runDB $ selectList [] [] :: Handler [Entity Category]
let badges = []
categoriesWithExpenseCount <- runDB categoriesWithExpenseCountQuery
(formWidget, formEnctype) <- generateFormPost categoryForm
defaultLayout $(widgetFile "category")
categoriesWithExpenseCountQuery :: MonadIO m => SqlReadT m [(Entity Category, E.Value Int)]
categoriesWithExpenseCountQuery = E.select $ E.from $ \(e `E.InnerJoin` c) -> do
E.on (e E.^. ExpenseCategoryId E.==. c E.^. CategoryId)
E.groupBy (c E.^. CategoryId)
let cr = E.count (e E.^. ExpenseAmount)
return (c, cr)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment