我想建立一个简单的招投标服务:
price字段。。
当一个新的出价被创建,我想验证它的价格高于现有的价格。
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如果我试图将bids视为列表:gethighestBidPrice [] = 0
我收到一个错误:
Couldn't match type `[a0]' with `QueryBuilder "bids"'我的问题是:
如果出价为空,
gethighestBidPrice上将默认值设置为0。发布于 2021-09-27 13:07:33
我好像错过了fetch的get #bids item。
我就是这样解决的:
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) xshttps://stackoverflow.com/questions/69330430
复制相似问题