Auction searching

master
E. Almqvist 3 years ago
parent 1bc2fbecb2
commit 2850f07c9f
  1. 46
      src/lib/db_models.rb
  2. 7
      src/routes/auction.rb
  3. 12
      src/views/auction/index.slim
  4. 3
      src/views/stylesheets/style.sass

@ -324,22 +324,45 @@ class Auction < EntityModel
self.insert data self.insert data
end end
def self.compose_query_filters(title=nil, categories=nil, price_rng=nil, expired=nil) def self.compose_query_filters(title=nil, categories=nil, min_price=nil, max_price=nil, expired=nil)
querystr = "SELECT * FROM Auction " querystr = "SELECT * FROM Auction WHERE "
querystr += "WHERE " if (title and title.length != 0) or categories or price_rng or expired
filters = [] filters = []
# Title filter
filters << "title LIKE '%#{title}%'" if title and title.length != 0 filters << "title LIKE '%#{title}%'" if title and title.length != 0
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 !expired.nil? # Price filters
if min_price and max_price then
filters << "price BETWEEN #{min_price} AND #{max_price}"
elsif min_price then
filters << "price >= #{min_price}"
elsif max_price then
filters << "price <= #{max_price}"
end
# Time filter
filters << "end_time #{ expired == true ? "<" : ">" } #{Time.now.to_i}"
# Categories filter
if categories then
ah_ids = []
categories.each do |catid|
if ah_ids == [] then
ah_ids = Auction_Category_relation.category_auction_ids(catid) # first time then include all
else
ah_ids |= Auction_Category_relation.category_auction_ids(catid) # union
end
end
filters << "id IN (#{ah_ids.join(", ")})" # check if the auction id is any of the ids calculated above
end
querystr += filters.join " AND " querystr += filters.join " AND "
return querystr return querystr
end end
def self.search(title=nil, categories=nil, price_rng=nil, expired=nil) def self.search(title=nil, categories=nil, min_price=nil, max_price=nil, expired=nil)
q = self.compose_query_filters title, categories, price_rng, expired q = self.compose_query_filters title, categories, min_price, max_price, expired
data = self.query(q) or [] data = self.query(q)
data.map! {|dat| self.new(dat)} data.map! {|dat| self.new(dat)}
end end
@ -499,6 +522,11 @@ class Auction_Category_relation < EntityModel
@auction_id = data["auction_id"] @auction_id = data["auction_id"]
@category_id = data["category_id"] @category_id = data["category_id"]
end end
def self.category_auction_ids(catid)
ids = self.get "auction_id", "category_id = ?", catid
ids && ids.map! {|id| id["auction_id"].to_i}
end
end end

@ -1,9 +1,10 @@
# Auction stuff # Auction stuff
get "/auctions" do get "/auctions" do
title = params[:title] title = params[:title] and params[:title] != "" ? params[:title].strip : nil
categories = params[:categories] categories = params[:categories]
min_price = params[:min_price] categories.map! {|catid| catid.to_i} unless categories.nil?
max_price = params[:max_price] min_price = params[:min_price].to_f > 0 ? params[:min_price].to_f : nil
max_price = params[:max_price].to_f > 0 ? params[:max_price].to_f : nil
expired = params[:expired] == "on" expired = params[:expired] == "on"
auctions = Auction.search title, categories, min_price, max_price, expired auctions = Auction.search title, categories, min_price, max_price, expired

@ -5,24 +5,24 @@
.form-container .form-container
form action="/auctions" method="get" form action="/auctions" method="get"
label Keywords label Keywords
input type="search" name="title" placeholder="Keywords (ex: computer, teapot)" input type="search" name="title" placeholder="ex: computer, teapot"
label Price range label Price range
.range-container .range-container
label for="price-min" From: label for="price-min" From:
input type="number" name="price-min" min="#{MIN_INIT_PRICE}" max="#{MAX_INIT_PRICE}" input type="number" name="min_price" value="#{params[:min_price].to_f >= 0 ? params[:min_price] : ""}" min="#{MIN_INIT_PRICE}" max="#{MAX_INIT_PRICE}"
label for="price-max" To: label for="price-max" To:
input type="number" name="price-max" min="#{MIN_INIT_PRICE}" max="#{MAX_INIT_PRICE}" input type="number" name="max_price" value="#{params[:min_price].to_f >= 0 ? params[:min_price] : ""}" min="#{MIN_INIT_PRICE}" max="#{MAX_INIT_PRICE}"
.checkbox-container .checkbox-container
input type="checkbox" name="expired" input type="checkbox" name="expired" checked=("checked" if params[:expired] == "on")
label Expired? label Expired?
- if Category.get_all.length > 0 - if Category.get_all.length > 0
label Categories label Categories
select.card.border multiple="" name="categories[]" select multiple="" name="categories[]"
- Category.get_all.each do |category| - Category.get_all.each do |category|
option value="#{category.id}" selected=("selected" if params[:categories]&.include? category.id) style="color: #{category.color};" #{category.name} option value="#{category.id}" selected=("selected" if params[:categories] and params[:categories].include?(category.id.to_s)) style="color: #{category.color};" #{category.name}
input type="submit" value="Search" input type="submit" value="Search"
a.button href="/auctions" = "Clear Filters" a.button href="/auctions" = "Clear Filters"

@ -277,6 +277,9 @@ ul.list-container
input[type=checkbox] input[type=checkbox]
margin: 0 .5rem margin: 0 .5rem
select option:checked
background-color: $shadow_clr !important
textarea textarea
background-color: $bg_dark_clr background-color: $bg_dark_clr
font-size: 1rem font-size: 1rem

Loading…
Cancel
Save