From 1bc2fbecb24bdd525348b0e397c65a24eda73abc Mon Sep 17 00:00:00 2001 From: "E. Almqvist" Date: Sun, 24 Apr 2022 14:45:04 +0200 Subject: [PATCH] Refactor & refactor & refactor ... --- src/TODO.md | 7 +------ src/lib/db_models.rb | 26 ++++++++++++++++++++++++-- src/routes/auction.rb | 11 +++++------ src/views/auction/index.slim | 7 +++---- src/views/auction/view.slim | 4 +++- 5 files changed, 36 insertions(+), 19 deletions(-) diff --git a/src/TODO.md b/src/TODO.md index 9eead11..74293d1 100644 --- a/src/TODO.md +++ b/src/TODO.md @@ -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 diff --git a/src/lib/db_models.rb b/src/lib/db_models.rb index cf95dea..4e780f3 100644 --- a/src/lib/db_models.rb +++ b/src/lib/db_models.rb @@ -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 diff --git a/src/routes/auction.rb b/src/routes/auction.rb index c9b26f2..9afc451 100644 --- a/src/routes/auction.rb +++ b/src/routes/auction.rb @@ -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 diff --git a/src/views/auction/index.slim b/src/views/auction/index.slim index ee1799c..e7972a7 100644 --- a/src/views/auction/index.slim +++ b/src/views/auction/index.slim @@ -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" diff --git a/src/views/auction/view.slim b/src/views/auction/view.slim index 7f9e21f..08d50d3 100644 --- a/src/views/auction/view.slim +++ b/src/views/auction/view.slim @@ -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}"