Browse Source

added categories handling

master
Archivist 6 years ago
parent
commit
d70bfda43e
3 changed files with 48 additions and 0 deletions
  1. +1
    -0
      src/sales_backend.cr
  2. +12
    -0
      src/sales_backend/category.cr
  3. +35
    -0
      src/sales_backend/http/category.cr

+ 1
- 0
src/sales_backend.cr View File

@ -1,6 +1,7 @@
require "kemal"
require "./sales_backend/http/user"
require "./sales_backend/http/product"
require "./sales_backend/http/category"
get "/" do |context|
context.response.content_type = "application/json"

+ 12
- 0
src/sales_backend/category.cr View File

@ -0,0 +1,12 @@
require "json"
require "uuid"
require "uuid/json"
require "./product"
class Category
JSON.mapping(
name: String,
parent: {type: String, nilable: true},
products: Array(UUID)
)
end

+ 35
- 0
src/sales_backend/http/category.cr View File

@ -0,0 +1,35 @@
require "kemal"
require "../*"
require "io"
require "file"
require "exception"
require "uuid"
require "uuid/json"
require "../../config"
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
end
ret.to_json
end
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"]
cat.products.each do |product|
begin
ret.push Product.from_json File.read Statics.data_path+"products/"+product.to_s
rescue
end
end
rescue ex
halt context, status_code: 403, response: ex.to_s
end
ret.to_json
end

Loading…
Cancel
Save