Auction posting progress

master
E. Almqvist 3 years ago
parent 644d1343f0
commit 8d1aadfa75
  1. 17
      src/app.rb
  2. 54
      src/db_models.rb
  3. 12
      src/lib/database.rb
  4. 5
      src/views/auction/index.slim
  5. 2
      src/views/auction/new.slim

@ -160,9 +160,6 @@ end
post "/user/update" do
id = (get_current_user.admin? and params[:id]) ? params[:id].to_i : session[:userid]
p "##########################"
puts "id=#{id}"
p "##########################"
data = {
name: params["displayname"].chomp,
@ -199,15 +196,23 @@ end
post "/auctions" do
user_id = session[:userid]
title = params[:title]
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
# Create the auction
newid, resp = Auction.create user_id, title, description, init_price, delta_time
# Apply categories to auction
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
# Save auction images TODO: literally make this work
images = params[:images]
p "########################"
#image = Magick::Image.from_blob(images.read)
p "########################"
if newid then
flash[:success] = "Auction posted!"

@ -298,11 +298,11 @@ class Auction < EntityModel
return true, ""
end
def self.create(user_id, title, description, init_price, delta_time, categories)
def self.create(user_id, title, description, init_price, delta_time)
# Remove invalid categories
categories.select! do |id|
self.exists? id
end
# categories.select! do |id|
# self.exists? id
# end
# Validate the input
check, errorstr = self.validate_ah(title, description, init_price, delta_time)
@ -342,6 +342,10 @@ class Auction < EntityModel
q = self.compose_query_filters title, categories, price_rng, isopen
self.query q
end
def images
Image.get_relation @id
end
end
@ -362,31 +366,49 @@ class Category < EntityModel
end
end
class Image < EntityModel
attr_reader :auction_id, :image_order, :url
class Auction_Category_relation < EntityModel
attr_reader :auction_id, :category_id
def initialize(data)
super data
@auction_id = data["auction_id"]
@image_order = data["image_order"]
@url = data["url"]
@category_id = data["category_id"]
end
end
class Auction_Category_relation < EntityModel
attr_reader :auction_id, :category_id
class Image < EntityModel
attr_reader :auction_id, :image_order, :url
def initialize(data)
super data
@auction_id = data["auction_id"]
@category_id = data["category_id"]
@image_order = data["image_order"]
@url = data["url"]
end
def self.get_user_roles(user_id)
roleids = self.get "role_id", "user_id = ?", user_id
roles = roleids.map do |ent|
Role.find_by_id(ent["role_id"].to_i)
def self.save(imgdata, ah_id, order)
data = {
auction_id: ah_id,
image_order: order,
url: "/auctions/#{ah_id}/#{order}.png"
}
newid, resp = self.insert data
if newid then
image = Magick::Image.from_blob(imgdata).first
image.format = "PNG"
path = "./public/auctions/#{ah_id}/#{order}.png"
File.open(path, 'wb') do |f|
image.write(f) { self.quality = 50 }
end
end
end
def self.get_relation(ah_id)
imgs = self.get "*", "auction_id = ?", ah_id
imgs.map! do |img|
self.new(img)
end
return imgs
end
end

@ -114,7 +114,6 @@ class EntityModel
data && self.new(data)
end
def self.get_all_ids
ids = self.get "id"
ids.map! {|k, id| id.to_i}
@ -129,10 +128,13 @@ end
class RelationModel < EntityModel # TODO: make this work
def self.tables = nil
def self.get_relation(id)
roleids = self.get "role_id", "user_id = ?", user_id
roles = roleids.map do |ent|
Role.find_by_id(ent["role_id"].to_i)
def self.get_relation(s_ent, table, filter="", *args)
q = "SELECT #{s_ent} FROM #{self.name}"
q = self.apply_filter(q, filter)
ents = self.get q, *args
ents.each do |ent|
table.find_by_id ent["id"].to_i
end
end
end

@ -10,8 +10,9 @@
label Price range
input type="range" name="price_rng"
label Is Open?
input type="checkbox" name="isopen" value="1"
.checkbox-container
input type="checkbox" name="isopen" value="1"
label Is Open?
input type="submit" value="Search"
article

@ -3,7 +3,7 @@
.form-container
form#auction_new action="/auctions" method="post" enctype="multipart/form-data"
label Select images
input type="file" name="image" accept="image" multiple="multiple"
input type="file" name="images" accept="image" multiple="multiple"
label Title
input type="text" name="title" placeholder="Title" pattern="#{TITLE_REGEX_STR}"

Loading…
Cancel
Save