Auction posting

master
E. Almqvist 3 years ago
parent 1f9e9df4a4
commit 856e1b9ba8
  1. 20
      src/app.rb
  2. 13
      src/db_models.rb
  3. 9
      src/lib/database.rb
  4. 2
      src/views/auction/view.slim

@ -162,19 +162,29 @@ end
post "/auctions" do
user_id = session[:userid]
title = params[:title]
init_price = params[:init_price]
delta_time = params[:delta_time]
description = params[:description]
init_price = params[:init_price].to_f
delta_time = params[:delta_time].to_i * 3600 # hours to seconds
# Select the category ids
category_choices = (params.select { |k, v| k.to_s.match(/^category-\d+/) }).map{ |k, v| v.to_i }
newid, resp = Auction.create user_id, title, description, init_price, delta_time, category_choices
p "###################"
p newid
p "###################"
newid = 0
redirect "/auctions/#{newid}"
end
get "/auctions/:id" do
id = params[:id].to_i
serve :"auction/view"
auction_obj = Auction.find_by_id id
if !auction_obj.nil? then
serve :"auction/view", {auction: auction_obj}
else
raise Sinatra::NotFound
end
end

@ -65,12 +65,6 @@ class User < EntityModel
return true, nil
end
# Find user by ID, returns a user object
def self.find_by_id(id)
data = self.get("*", "id = ?", id).first
data && User.new(data)
end
# Find user by email, same as above but for emails.
def self.find_by_email(email)
data = self.get("*", "email = ?", email).first
@ -163,11 +157,6 @@ class Role < EntityModel
# do bitwise ops
end
def self.find_by_id(id)
data = self.get("*", "id = ?", id).first
data && Role.new(data)
end
def self.find_by_name(name)
data = self.get("*", "name = ?", name).first
data && Role.new(data)
@ -211,7 +200,7 @@ class Auction < EntityModel
@end_time = data["end_time"]
end
private def self.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

@ -65,7 +65,9 @@ class EntityModel
def self.insert(data) # Inserts new data into the table
entstr, valstr = self.gen_insert_query data.keys
self.query( "INSERT INTO #{self.name} #{entstr} VALUES #{valstr}", *data.values )
r = self.query( "INSERT INTO #{self.name} #{entstr} VALUES #{valstr}", *data.values )
newid = db.last_insert_row_id
return newid, r
end
def self.set(attr, data, filter="") # slower but more lazy
@ -84,6 +86,11 @@ class EntityModel
resp = self.get "id", "id = ?", id
resp.length > 0
end
def self.find_by_id(id)
data = self.get("*", "id = ?", id).first
data && self.new(data)
end
end
class RelationModel < EntityModel # TODO: make this work

@ -0,0 +1,2 @@
h1 = auction.title
h2 = auction.description
Loading…
Cancel
Save