Auction: category

master
E. Almqvist 3 years ago
parent b9f06a3533
commit 1f9e9df4a4
  1. 5
      src/app.rb
  2. 3
      src/const.rb
  3. 22
      src/db_models.rb
  4. 11
      src/lib/database.rb
  5. 13
      src/views/auction/new.slim

@ -163,6 +163,11 @@ post "/auctions" do
user_id = session[:userid]
title = params[:title]
init_price = params[:init_price]
delta_time = params[:delta_time]
# Select the category ids
category_choices = (params.select { |k, v| k.to_s.match(/^category-\d+/) }).map{ |k, v| v.to_i }
newid = 0
redirect "/auctions/#{newid}"

@ -24,7 +24,7 @@ MAX_TITLE_LEN = 32
MIN_DESC_LEN = 0
MAX_DESC_LEN = 512
MIN_DELTA_TIME = 60 * 60 * 24 # 1 day
MIN_DELTA_TIME = 60*60 # 1 hour
# User constants
AVATAR_SIZE = 1024 # width & height
@ -40,6 +40,7 @@ EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-]+(\.[a-z\d\-]+)*\.[a-z]+\z/
NAME_REGEX_STR = "[a-zA-Z-_0-9 ]{#{MIN_NAME_LEN},#{MAX_NAME_LEN}}"
BIO_REGEX_STR = "{#{MIN_BIO_LEN},#{MAX_BIO_LEN}}"
DESC_REGEX_STR = "{#{MIN_DESC_LEN},#{MAX_DESC_LEN}}"
TITLE_REGEX_STR = "{#{MIN_TITLE_LEN},#{MAX_TITLE_LEN}}"
# Routes that needs auth

@ -138,6 +138,14 @@ class User < EntityModel
return true, user.id
end
# Check if user has permission
# TODO: Make this work
def self.permitted?(id, perm)
user = self.find_by_id id
roles = user.roles
# check each role for flag
end
end
# Role model
@ -150,6 +158,11 @@ class Role < EntityModel
@flags = data["flags"]
end
# TODO: Check if role has specific flag
def has_flag?(flag)
# do bitwise ops
end
def self.find_by_id(id)
data = self.get("*", "id = ?", id).first
data && Role.new(data)
@ -198,14 +211,19 @@ class Auction < EntityModel
@end_time = data["end_time"]
end
def self.validate_ah(title, description, init_price, delta_time)
private 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 self.create(user_id, title, description, init_price, delta_time)
def self.create(user_id, title, description, init_price, delta_time, categories)
# Remove invalid categories
categories.select! do |id|
self.exists? id
end
# Validate the input
check, errorstr = self.validate_ah(title, description, init_price, delta_time)
return errorstr unless check

@ -75,9 +75,18 @@ class EntityModel
self.insert(data, filter)
end
end
def self.get_all(ents="*")
self.query "SELECT #{ents} FROM #{self.name}"
end
def self.exists?(id)
resp = self.get "id", "id = ?", id
resp.length > 0
end
end
class RelationModel < EntityModel
class RelationModel < EntityModel # TODO: make this work
def self.tables = nil
def self.get_relation(id)

@ -1,14 +1,21 @@
h1 Post Auction
.form-container
.form-container#auction_new
form action="/auctions" method="post" enctype="multipart/form-data"
input type="file" name="image" accept="image"
label Title
input type="text" name="title" placeholder="Title"
input type="text" name="title" placeholder="Title" pattern="#{TITLE_REGEX_STR}"
label = "Initial Price #{COINS_PREFIX} #{COINS_SUFFIX}"
input type="number" name="init_price" value="#{MIN_INIT_PRICE}"
input type="number" name="init_price" value="#{MIN_INIT_PRICE}" min="#{MIN_INIT_PRICE}"
label Auction duration (hours)
input type="number" name="delta_time" value="1" min="1"
textarea name="description" cols="20" rows="5" title="Content length must be between #{MIN_DESC_LEN} and #{MAX_DESC_LEN} characters" pattern="#{DESC_REGEX_STR}" maxlength="#{MAX_DESC_LEN}" placeholder="Description"
label Categories
- Category.get_all.each do |category|
input type="checkbox" name="category-#{category["id"]}" value="#{category["id"]}" = category["name"]
input type="submit" value="Post"

Loading…
Cancel
Save