Quellcode durchsuchen

Enhanced categories, removed some code duplication

master
Archivist vor 6 Jahren
Ursprung
Commit
452d216e80
4 geänderte Dateien mit 69 neuen und 30 gelöschten Zeilen
  1. +29
    -0
      src/config.cr
  2. +2
    -1
      src/sales_backend/category.cr
  3. +34
    -5
      src/sales_backend/http/category.cr
  4. +4
    -24
      src/sales_backend/http/product.cr

+ 29
- 0
src/config.cr
Datei-Diff unterdrückt, da er zu groß ist
Datei anzeigen


+ 2
- 1
src/sales_backend/category.cr Datei anzeigen

@ -5,7 +5,8 @@ require "./product"
class Category
JSON.mapping(
name: String,
name: {type: String, nilable: true},
name_id: String,
parent: {type: String, nilable: true},
products: Array(UUID)
)

+ 34
- 5
src/sales_backend/http/category.cr Datei anzeigen

@ -8,11 +8,32 @@ require "uuid/json"
require "../../config"
post "/category" do |context|
begin
begin
user = authenticate_admin!(context.request.headers["user"],UUID.new(context.request.headers["api_token"]))
rescue ex
halt context, status_code: 403, response: ex.to_s
end
cat = Category.from_json(context.request.body.not_nil!).not_nil!
if cat.name_id.count(' ')==0
File.write Statics.data_path+"category/"+cat.name_id,cat.to_json
else
halt context, status_code: 400, response: "name_id should not contain spaces ' ' or '/'"
end
rescue ex
halt context, status_code: 500, response: ex.to_s
end
"OK".to_json
end
get "/category/m/:id" do |context|
begin
ret = Category.from_json File.read Statics.data_path+"category/"+context.params.url["id"]
rescue ex
halt context, status_code: 403, response: ex.to_s
halt context, status_code: 404, response: ex.to_s
end
ret.to_json
end
@ -21,15 +42,23 @@ get "/category/d/:id" do |context|
ret = Array(Product).new
begin
cat = Category.from_json File.read Statics.data_path+"category/"+context.params.url["id"]
sent=0
skipped=0
skip, limit = Statics.extract_skip_info context
cat.products.each do |product|
begin
ret.push Product.from_json File.read Statics.data_path+"products/"+product.to_s
begin
if(skipped<skip)
skipped+=1
else
ret.push Product.from_json File.read Statics.data_path+"products/"+product.to_s
sent+=1
end
rescue
end
end
rescue ex
halt context, status_code: 403, response: ex.to_s
halt context, status_code: 404, response: ex.to_s
end
ret.to_json
end

+ 4
- 24
src/sales_backend/http/product.cr Datei anzeigen

@ -16,29 +16,9 @@ get "/products" do |context|
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
skip, limit = Statics.extract_skip_info context
path.each do |filename|
if(sent<limit)
begin
@ -62,7 +42,7 @@ get "/products/:id" do |context|
begin
ret = Product.from_json File.read Statics.data_path+"products/"+context.params.url["id"]
rescue ex
halt context, status_code: 403, response: ex.to_s
halt context, status_code: 404, response: ex.to_s
end
ret.to_json
end

Laden…
Abbrechen
Speichern