Skip to content

Instantly share code, notes, and snippets.

@bitemyapp
Created June 14, 2018 16:49
Show Gist options
  • Save bitemyapp/2445f6f4a24a42560b270bc2b41a9163 to your computer and use it in GitHub Desktop.
Save bitemyapp/2445f6f4a24a42560b270bc2b41a9163 to your computer and use it in GitHub Desktop.
-- | Given a 'Key User', return the digital and physical product
-- cart entities associated with that user's cart.
userCartContents :: (SqlSelect a r)
=> (SqlExpr (Entity Cart)
-> SqlExpr (Entity CartProduct)
-> SqlQuery a)
-> Key User
-> DB [r]
userCartContents f userKey =
select .
from $ \( user
`InnerJoin`
cart
`InnerJoin`
cartProduct
) -> do
on (cart ^. CartId
==. cartProduct ^. CartProductCart)
on (user ^. UserId
==. cart ^. CartUser)
where_ (user ^. UserId ==. val userKey)
f cart cartProduct
userCartProducts :: Key User
-> DB [(Entity CartProduct, Entity Product)]
userCartProducts userKey = do
cartContents <- userCartContents
(\cart cartProduct -> do
where_ (cart ^. CartId
==. cartProduct ^. CartProductCart)
return cartProduct
)
userKey
productsFromCart (fmap entityKey cartContents)
userCartProduct :: Key User
-> Key CartProduct
-> DB (Maybe (Entity Cart))
userCartProduct userKey cartProductKey =
fmap listToMaybe $
userCartContents
(\cart cartProduct -> do
where_ (cartProduct ^. CartProductId
==. val cartProductKey)
return cart
)
userKey
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment