amitaibu
amitaibu

Reputation: 1116

How to iterate over values upon validation

I have a simple Biding service I'd like to build:

  1. Each Item can have multiple Bids.
  2. 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:

  1. How to set default value to 0 on gethighestBidPrice if bids is empty.
  2. How to find the highest price out of the bids.

Upvotes: 1

Views: 54

Answers (1)

amitaibu
amitaibu

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

Related Questions