I have a simple Biding service I'd like to build:
- Each Item can have multiple Bids.
- Each Bid has a reference to the Item and an Int
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:
- How to set default value to 0 on
gethighestBidPrice
if bids is empty. - How to find the highest price out of the bids.