Refactor & refactor & refactor ...

master
E. Almqvist 3 years ago
parent d9fb8c938f
commit 1bc2fbecb2
  1. 7
      src/TODO.md
  2. 26
      src/lib/db_models.rb
  3. 11
      src/routes/auction.rb
  4. 7
      src/views/auction/index.slim
  5. 4
      src/views/auction/view.slim

@ -1,12 +1,7 @@
# TODO
- Auction expiry (unable to bid after x time)
- Auction searching at index 40%
- Post editing
- User reviews
- Bidding 99% TODO: add money check + other checks
----------------
- Yardoc 50%
----------------
- Remove bids

@ -427,7 +427,25 @@ class Bid < EntityModel
data && data.map! {|dat| self.new(dat)}
end
private def self.validate_bid(ahid, uid, amount, message)
def self.get_user_bids(uid)
data = self.get "*", "user_id = ?", uid
data && data.map! {|dat| self.new(dat)}
end
def self.get_user_active_amount(uid)
bids = self.get_user_bids uid
return bids.sum {|bid| bid.amount}
end
def self.get_delta_amount(ahid, uid, amount)
data = self.get "*", "auction_id = ?, user_id = ?", ahid, uid
data && data.map! {|dat| self.new(dat)}
p data
return amount
end
def self.validate_bid(ahid, uid, amount, message)
ah = Auction.find_by_id ahid
return false, "Invalid auction" unless ah.is_a? Auction
return false, AUCTION_ERRORS[:expired] unless not ah.expired?
@ -439,6 +457,11 @@ class Bid < EntityModel
def self.place(ahid, uid, amount, message)
check, resp = self.validate_bid(ahid, uid, amount, message)
if check then
# Deduct delta amount from balance
delta_amount = self.get_delta_amount(ahid, uid, amount)
user = User.find_by_id uid
user.balance -= delta_amount
payload = {
auction_id: ahid,
user_id: uid,
@ -449,7 +472,6 @@ class Bid < EntityModel
end
return check, resp
end
end

@ -1,13 +1,12 @@
# Auction stuff
get "/auctions" do
title = params[:title]
#categories = (params[:categories].split ",").map {|id| id.to_i}
#price_rng = (params[:price_rng].split "-").map {|p| p.to_i}
expired = params[:expired]
categories = params[:categories]
min_price = params[:min_price]
max_price = params[:max_price]
expired = params[:expired] == "on"
# TODO FIX THIS
auctions = Auction.search title #, categories, price_rng, expired
p auctions
auctions = Auction.search title, categories, min_price, max_price, expired
serve :"auction/index", {auctions: auctions}
end

@ -20,10 +20,9 @@
- if Category.get_all.length > 0
label Categories
- Category.get_all.each do |category|
.checkbox-container
input type="checkbox" name="category-#{category.id}" value="#{category.id}"
label style="color: #{category.color};" = "#{category.name}"
select.card.border multiple="" name="categories[]"
- Category.get_all.each do |category|
option value="#{category.id}" selected=("selected" if params[:categories]&.include? category.id) style="color: #{category.color};" #{category.name}
input type="submit" value="Search"
a.button href="/auctions" = "Clear Filters"

@ -66,7 +66,9 @@
- auction.bids.reverse_each do |bid|
li
h3
| #{User.find_by_id(bid.user_id).name} bid
a.inlbutton.gray href="/profile/#{bid.user_id}"
| #{User.find_by_id(bid.user_id).name.strip}
| bid
span
| #{COINS_PREFIX}#{bid.amount.round 2}#{COINS_SUFFIX}
h4 = "#{bid.message}"

Loading…
Cancel
Save