Auction searching refactor & more

master
E. Almqvist 3 years ago
parent 05939e82a8
commit b38fafccf0
  1. 2
      src/TODO.md
  2. 15
      src/lib/db_models.rb
  3. 7
      src/routes/auction.rb
  4. 11
      src/views/auction/index.slim

@ -1,5 +1,5 @@
# TODO
- Auction views
- Auction views 80%
----------------
- Auction searching at index
- Bidding

@ -286,7 +286,7 @@ class Auction < EntityModel
@user_id = data["user_id"].to_i
@title = data["title"]
@description = data["description"]
@init_price = data["init_price"].to_i
@init_price = data["price"].to_i
@start_time = data["start_time"].to_i
@end_time = data["end_time"].to_i
end
@ -320,22 +320,23 @@ class Auction < EntityModel
self.insert data
end
def self.compose_query_filters(title=nil, categories=nil, price_rng=nil, isopen=nil)
def self.compose_query_filters(title=nil, categories=nil, price_rng=nil, expired=nil)
querystr = "SELECT * FROM Auction "
querystr += "WHERE " if title or categories or price_rng or isopen
querystr += "WHERE " if title or categories or price_rng or expired
filters = []
filters << "LIKE '%#{title}%'" if title
filters << "price BETWEEN #{price_rng[0]} AND #{price_rng[1]}" if price_rng && price_rng.length == 2
filters << "end_time > #{Time.now.to_i}" if !isopen.nil?
filters << "end_time < #{Time.now.to_i}" if !expired.nil?
querystr += filters.join " AND "
return querystr
end
def self.search(title=nil, categories=nil, price_rng=nil, isopen=nil)
q = self.compose_query_filters title, categories, price_rng, isopen
self.query q
def self.search(title=nil, categories=nil, price_rng=nil, expired=nil)
q = self.compose_query_filters title, categories, price_rng, expired
data = self.query(q) or []
data.map! {|dat| self.new(dat)}
end
def self.expired?(id)

@ -3,9 +3,12 @@ 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}
isopen = params[:isopen]
expired = params[:expired]
p "#####"
p expired
p "#####"
auctions = Auction.search title #, categories, price_rng, isopen
auctions = Auction.search title #, categories, price_rng, expired
serve :"auction/index", {auctions: auctions}
end

@ -11,8 +11,8 @@
input type="range" name="price_rng"
.checkbox-container
input type="checkbox" name="isopen" value="1"
label Is Open?
input type="checkbox" name="expired"
label Expired?
input type="submit" value="Search"
article
@ -21,5 +21,8 @@
ul
- auctions.each do |auction|
li
a href="/auctions/#{auction["id"]}"
| #{auction["title"]}
a href="/auctions/#{auction.id}"
| #{auction.title}
- if auction.end_time and auction.end_time - Time.now.to_i <= 0
span.red
| [Expired]

Loading…
Cancel
Save