From de38811c95b64afe3b2ccc92e3a2b56509bcb5d7 Mon Sep 17 00:00:00 2001 From: Archivist Date: Mon, 9 Jul 2018 17:31:26 +0200 Subject: [PATCH] atomization of the addresses changes and token listing --- src/sales_backend/address.cr | 5 +++- src/sales_backend/http/user.cr | 48 ++++++++++++++++++++++++++++++++++ 2 files changed, 52 insertions(+), 1 deletion(-) diff --git a/src/sales_backend/address.cr b/src/sales_backend/address.cr index 590242e..2401971 100644 --- a/src/sales_backend/address.cr +++ b/src/sales_backend/address.cr @@ -17,6 +17,9 @@ class Address postcode: String, city: String, country: Country, - is_default: Bool + is_default: {type: Bool, default: false}, ) + def == (other : Address) + self.to_json==other.to_json + end end \ No newline at end of file diff --git a/src/sales_backend/http/user.cr b/src/sales_backend/http/user.cr index 59b65f2..524279a 100644 --- a/src/sales_backend/http/user.cr +++ b/src/sales_backend/http/user.cr @@ -41,6 +41,7 @@ post "/login" do |context| user_file.tokens = user_file.tokens.not_nil!.last(5) end File.write(Statics.data_path+"user/"+user_file.email,user_file.to_json) + context.response.content_type = "application/json" token.to_json end @@ -49,6 +50,7 @@ post "/logout" do |context| user_file = User.from_json File.read(Statics.data_path+"user/"+user.email) user_file.tokens=user_file.tokens.not_nil!-user.tokens.not_nil! File.write(Statics.data_path+"user/"+user_file.email,user_file.to_json) + context.response.content_type = "application/json" "OK".to_json end @@ -57,6 +59,7 @@ post "/logout-all" do |context| user_file = User.from_json File.read(Statics.data_path+"user/"+user.email) user_file.tokens=Array(UUID).new File.write(Statics.data_path+"user/"+user_file.email,user_file.to_json) + context.response.content_type = "application/json" "OK".to_json end @@ -74,9 +77,54 @@ post "/user" do |context| raise Exception.new("Bad email address") end File.write(Statics.data_path+"user/"+user.email,user.to_json) + context.response.content_type = "application/json" + "OK".to_json +end + +get "/user/tokens" do |context| + user = authenticate!(context.request.headers["user"],UUID.new(context.request.headers["api_token"])) + context.response.content_type = "application/json" + user.tokens.to_json +end + +get "/user/address" do |context| + user = authenticate!(context.request.headers["user"],UUID.new(context.request.headers["api_token"])) + context.response.content_type = "application/json" + user.addresses.to_json +end + +post "/user/address" do |context| + user = authenticate!(context.request.headers["user"],UUID.new(context.request.headers["api_token"])) + addresses = Array(Address).from_json(context.request.body.not_nil!).not_nil! + user_file = User.from_json File.read(Statics.data_path+"user/"+user.email) + old_list=user_file.addresses + if old_list.nil? + else + addresses=old_list+addresses + end + user_file.addresses=addresses + File.write(Statics.data_path+"user/"+user.email,user_file.to_json) + context.response.content_type = "application/json" + "OK".to_json +end + +delete "/user/address" do |context| + user = authenticate!(context.request.headers["user"],UUID.new(context.request.headers["api_token"])) + addresses = Array(Address).from_json(context.request.body.not_nil!).not_nil! + user_file = User.from_json File.read(Statics.data_path+"user/"+user.email) + old_list=user_file.addresses + if old_list.nil? + addresses=Array(Address).new + else + addresses=old_list-addresses + end + user_file.addresses=addresses + File.write(Statics.data_path+"user/"+user.email,user_file.to_json) + context.response.content_type = "application/json" "OK".to_json end get "/user" do |context| + context.response.content_type = "application/json" authenticate!(context.request.headers["user"],UUID.new(context.request.headers["api_token"])).to_json end