|
|
@ -2,8 +2,68 @@ require "kemal" |
|
|
|
require "../*" |
|
|
|
require "io" |
|
|
|
require "file" |
|
|
|
require "math" |
|
|
|
require "exception" |
|
|
|
require "crypto/bcrypt/password" |
|
|
|
require "dir" |
|
|
|
require "uuid" |
|
|
|
require "uuid/json" |
|
|
|
require "../../config" |
|
|
|
require "kemal/param_parser" |
|
|
|
require "../../config" |
|
|
|
|
|
|
|
get "/products" do |context| |
|
|
|
ret = Array(Product).new |
|
|
|
path = Dir.new(Statics.data_path+"products") |
|
|
|
sent = 0 |
|
|
|
skipped = 0 |
|
|
|
skip = 0 |
|
|
|
|
|
|
|
begin |
|
|
|
skip_o = context.params.query["skip"] |
|
|
|
if skip_o.is_a? String |
|
|
|
skip = Int32.from_json skip_o |
|
|
|
elsif skip_o.is_a? Int32 |
|
|
|
skip=skip_o |
|
|
|
end |
|
|
|
rescue |
|
|
|
skip=0 |
|
|
|
end |
|
|
|
limit = Statics.max_product_query |
|
|
|
begin |
|
|
|
limit_o = Int32.from_json context.params.query["limit"] |
|
|
|
if limit_o.is_a? String |
|
|
|
limit = Math.min(Int32.from_json(limit_o), limit) |
|
|
|
elsif limit_o.is_a? Int32 |
|
|
|
limit=Math.min(limit_o, Statics.max_product_query) |
|
|
|
end |
|
|
|
rescue |
|
|
|
limit=Statics.max_product_query |
|
|
|
end |
|
|
|
path.each do |filename| |
|
|
|
if(sent<limit) |
|
|
|
begin |
|
|
|
if filename.char_at(0)!='.' |
|
|
|
if(skipped<skip) |
|
|
|
skipped+=1 |
|
|
|
STDOUT.puts "Skipped "+filename |
|
|
|
else |
|
|
|
ret.push Product.from_json File.read Statics.data_path+"products/"+filename |
|
|
|
sent+=1; |
|
|
|
STDOUT.puts "Sent "+filename |
|
|
|
end |
|
|
|
end |
|
|
|
rescue exception |
|
|
|
|
|
|
|
end |
|
|
|
end |
|
|
|
end |
|
|
|
ret.to_json |
|
|
|
end |
|
|
|
|
|
|
|
|
|
|
|
post "/products" do |context| |
|
|
|
user = authenticate_admin!(context.request.headers["user"],UUID.new(context.request.headers["api_token"])) |
|
|
|
product = Product.from_json(context.request.body.not_nil!).not_nil! |
|
|
|
File.write Statics.data_path+"products/"+product.id.to_s,product.to_json |
|
|
|
"OK".to_json |
|
|
|
end |