-
-
Save bitemyapp/2445f6f4a24a42560b270bc2b41a9163 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
-- | 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