From d70bfda43ee8b139893d8f315e84e769b1f64b55 Mon Sep 17 00:00:00 2001 From: Archivist Date: Sat, 14 Jul 2018 18:44:09 +0200 Subject: [PATCH] added categories handling --- src/sales_backend.cr | 1 + src/sales_backend/category.cr | 12 ++++++++++ src/sales_backend/http/category.cr | 35 ++++++++++++++++++++++++++++++ 3 files changed, 48 insertions(+) create mode 100644 src/sales_backend/category.cr create mode 100644 src/sales_backend/http/category.cr diff --git a/src/sales_backend.cr b/src/sales_backend.cr index 5630415..2a59514 100644 --- a/src/sales_backend.cr +++ b/src/sales_backend.cr @@ -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" diff --git a/src/sales_backend/category.cr b/src/sales_backend/category.cr new file mode 100644 index 0000000..f681c00 --- /dev/null +++ b/src/sales_backend/category.cr @@ -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 \ No newline at end of file diff --git a/src/sales_backend/http/category.cr b/src/sales_backend/http/category.cr new file mode 100644 index 0000000..756995a --- /dev/null +++ b/src/sales_backend/http/category.cr @@ -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 \ No newline at end of file