Bid validation

master
E. Almqvist 3 years ago
parent 7a5622ec69
commit d9fb8c938f
  1. 4
      src/config.rb
  2. 18
      src/lib/db_models.rb
  3. 2
      src/routes/auction.rb
  4. 2
      src/views/auction/view.slim

@ -41,6 +41,8 @@ AUCTION_ERRORS = {
initprice: "The initial price must be at least #{MIN_INIT_PRICE}!",
deltatime: "Time span is too short! Must be at least one day!",
bidamount: "Bid amount must be at least #{((AH_BIDS_FACTOR-1)*100).round(2)}% greater than the highest bid!",
imagecount: "You need to submit at least #{AH_MIN_IMAGES} image(s)!"
imagecount: "You need to submit at least #{AH_MIN_IMAGES} image(s)!",
expired: "Auction has expired!",
cantafford: "You can not afford to bid that much!"
}

@ -427,21 +427,27 @@ class Bid < EntityModel
data && data.map! {|dat| self.new(dat)}
end
def self.place(ahid, uid, amount, message)
private def self.validate_bid(ahid, uid, amount, message)
ah = Auction.find_by_id ahid
if not ah then return false, "Invalid auction" end
return false, "Invalid auction" unless ah.is_a? Auction
return false, AUCTION_ERRORS[:expired] unless not ah.expired?
return false, AUCTION_ERRORS[:cantafford] unless User.find_by_id(uid).balance - amount >= 0
return false, AUCTION_ERRORS[:bidamount] unless amount >= ah.min_new_bid
return true, ""
end
if amount >= ah.min_new_bid then
def self.place(ahid, uid, amount, message)
check, resp = self.validate_bid(ahid, uid, amount, message)
if check then
payload = {
auction_id: ahid,
user_id: uid,
amount: amount,
message: message
}
self.insert(payload)
else
return false, AUCTION_ERRORS[:bidamount]
resp = self.insert(payload)
end
return check, resp
end
end

@ -75,7 +75,7 @@ post "/auctions/:id/bids" do
message = params[:message]
if !auction_obj.nil? then
success, resp = auction_obj.place_bid( session[:userid], amount, message)
success, resp = auction_obj.place_bid(session[:userid], amount, message)
if success then
flash[:success] = "Placed bid."
else

@ -59,8 +59,6 @@
h1.red
| Auction Expired
#auctionbid-container.card
h2.tcenter Bid History
- if auction.bids.length >= 1

Loading…
Cancel
Save