Auctions: Search method

master
E. Almqvist 3 years ago
parent c244bbd919
commit 0019cef498
  1. 2
      src/app.rb
  2. 24
      src/db_models.rb
  3. 2
      src/sql/tables/Auction.sql
  4. 1
      src/views/auction/new.slim
  5. 4
      src/views/index.slim
  6. 2
      src/views/stylesheets/style.sass

@ -155,7 +155,7 @@ get "/auctions" do
serve :"auction/index"
end
get "/auction/new" do
get "/auctions/new" do
serve :"auction/new"
end

@ -198,14 +198,14 @@ class Auction < EntityModel
@end_time = data["end_time"]
end
def validate_ah(title, description, init_price, delta_time)
def self.validate_ah(title, description, init_price, delta_time)
return false, AUCTION_ERRORS[:titlelen] unless title.length.between?(MIN_TITLE_LEN, MAX_TITLE_LEN)
return false, AUCTION_ERRORS[:initprice] unless init_price >= MIN_INIT_PRICE
return false, AUCTION_ERRORS[:deltatime] unless delta_time >= MIN_DELTA_TIME
return true, ""
end
def create(user_id, title, description, init_price, delta_time)
def self.create(user_id, title, description, init_price, delta_time)
# Validate the input
check, errorstr = self.validate_ah(title, description, init_price, delta_time)
return errorstr unless check
@ -219,13 +219,31 @@ class Auction < EntityModel
user_id: user_id,
title: title,
description: description,
init_price: init_price,
price: init_price,
start_time: start_time,
end_time: end_time
}
self.insert data
end
def self.compose_query_filters(title=nil, categories=nil, price_rng=nil, isopen=nil)
querystr = "SELECT * FROM Auction "
querystr += "WHERE " if title or categories or price_rng or time_left
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?
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
end
end

@ -3,7 +3,7 @@ CREATE TABLE IF NOT EXISTS "Auction" (
"user_id" INTEGER NOT NULL,
"title" TEXT NOT NULL,
"description" TEXT NOT NULL,
"init_price" INTEGER NOT NULL DEFAULT 1,
"price" INTEGER NOT NULL DEFAULT 1,
"start_time" INTEGER NOT NULL,
"end_time" INTEGER NOT NULL,
PRIMARY KEY("id" AUTOINCREMENT)

@ -1,3 +1,4 @@
h1 Post Auction
.form-container
form action="/auctions" method="post" enctype="multipart/form-data"
input type="file" name="image" accept="image"

@ -4,9 +4,9 @@ article.index
| Welcome to the Auction House! With our convenient online auction platform, you can easily browse through listings and find great deals on everything from clothes to electronics.
ul.button-container
li.em
a.button href="/auction" = "Browse Auctions"
a.button href="/auctions" = "Browse Auctions"
li
a.button href="/auction/new" = "Post Auction"
a.button href="/auctions/new" = "Post Auction"
article.index
h1.tcenter.title

@ -370,3 +370,5 @@ ul.button-container
// Auctions

Loading…
Cancel
Save