Reputation: 1116
I have a simple Biding service I'd like to build:
price
field.When a new Bid is created I'd like to verify it's price is higher then the existing ones.
action CreateBidAction = do
let bid = newRecord @Bid
bid
|> buildBid
|> validateIsPriceAboveOtherBids
>>= ifValid \case
-- ...
validateIsPriceAboveOtherBids bid = do
item <- fetch (get #itemId bid)
let highestBidPrice = gethighestBidPrice (get #bids item)
bid
|> validateField #price (isGreaterThan highestBidPrice)
|> pure
gethighestBidPrice bids = 42
If I try to treat bids
as list: gethighestBidPrice [] = 0
I get an error:
Couldn't match type `[a0]' with `QueryBuilder "bids"'
My questions are:
gethighestBidPrice
if bids is empty.Upvotes: 1
Views: 54
Reputation: 1116
I seem to have missed a fetch
for the get #bids item
.
Here's how I've solved it:
validateIsPriceAboveOtherBids bid = do
item <- fetch (get #itemId bid)
bids <- fetch (get #bids item)
let prices = map (get #price) bids
let highestBidPrice = maximum' prices
bid
|> validateField #price (isGreaterThan highestBidPrice)
|> pure
where
maximum' :: [Int] -> Int
maximum' [] = 0
maximum' xs = foldr1 (\x y ->if x >= y then x else y) xs
Upvotes: 0