diff --git a/src/lib/db_models.rb b/src/lib/db_models.rb index 4e780f3..f559f30 100644 --- a/src/lib/db_models.rb +++ b/src/lib/db_models.rb @@ -324,22 +324,45 @@ class Auction < EntityModel self.insert data end - def self.compose_query_filters(title=nil, categories=nil, price_rng=nil, expired=nil) - querystr = "SELECT * FROM Auction " - querystr += "WHERE " if (title and title.length != 0) or categories or price_rng or expired - + def self.compose_query_filters(title=nil, categories=nil, min_price=nil, max_price=nil, expired=nil) + querystr = "SELECT * FROM Auction WHERE " filters = [] + + # Title filter 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 " return querystr end - 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 [] + def self.search(title=nil, categories=nil, min_price=nil, max_price=nil, expired=nil) + q = self.compose_query_filters title, categories, min_price, max_price, expired + data = self.query(q) data.map! {|dat| self.new(dat)} end @@ -499,6 +522,11 @@ class Auction_Category_relation < EntityModel @auction_id = data["auction_id"] @category_id = data["category_id"] 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 diff --git a/src/routes/auction.rb b/src/routes/auction.rb index 9afc451..92f1543 100644 --- a/src/routes/auction.rb +++ b/src/routes/auction.rb @@ -1,9 +1,10 @@ # Auction stuff get "/auctions" do - title = params[:title] + title = params[:title] and params[:title] != "" ? params[:title].strip : nil categories = params[:categories] - min_price = params[:min_price] - max_price = params[:max_price] + categories.map! {|catid| catid.to_i} unless categories.nil? + 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" auctions = Auction.search title, categories, min_price, max_price, expired diff --git a/src/views/auction/index.slim b/src/views/auction/index.slim index e7972a7..c440148 100644 --- a/src/views/auction/index.slim +++ b/src/views/auction/index.slim @@ -5,24 +5,24 @@ .form-container form action="/auctions" method="get" label Keywords - input type="search" name="title" placeholder="Keywords (ex: computer, teapot)" + input type="search" name="title" placeholder="ex: computer, teapot" label Price range .range-container 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: - 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 - input type="checkbox" name="expired" + input type="checkbox" name="expired" checked=("checked" if params[:expired] == "on") label Expired? - if Category.get_all.length > 0 label Categories - select.card.border multiple="" name="categories[]" + select 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} + 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" a.button href="/auctions" = "Clear Filters" diff --git a/src/views/stylesheets/style.sass b/src/views/stylesheets/style.sass index d06de17..d444944 100644 --- a/src/views/stylesheets/style.sass +++ b/src/views/stylesheets/style.sass @@ -277,6 +277,9 @@ ul.list-container input[type=checkbox] margin: 0 .5rem + select option:checked + background-color: $shadow_clr !important + textarea background-color: $bg_dark_clr font-size: 1rem