瀏覽代碼

added categories handling

master
Archivist 6 年之前
父節點
當前提交
d70bfda43e
共有 3 個文件被更改,包括 48 次插入0 次删除
  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 查看文件

@ -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 查看文件

@ -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 查看文件

@ -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…
取消
儲存