Auction searching refactor & more

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

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

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

@ -3,9 +3,13 @@ get "/auctions" do
title = params[:title] title = params[:title]
#categories = (params[:categories].split ",").map {|id| id.to_i} #categories = (params[:categories].split ",").map {|id| id.to_i}
#price_rng = (params[:price_rng].split "-").map {|p| p.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 # TODO FIX THIS
auctions = Auction.search title #, categories, price_rng, expired
serve :"auction/index", {auctions: auctions} serve :"auction/index", {auctions: auctions}
end end

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

Loading…
Cancel
Save