Browse Source

atomization of the addresses changes and token listing

master
Archivist 6 years ago
parent
commit
de38811c95
2 changed files with 52 additions and 1 deletions
  1. +4
    -1
      src/sales_backend/address.cr
  2. +48
    -0
      src/sales_backend/http/user.cr

+ 4
- 1
src/sales_backend/address.cr View File

@ -17,6 +17,9 @@ class Address
postcode: String,
city: String,
country: Country,
is_default: Bool
is_default: p">{type: Bool, default: false},
)
def == (other : Address)
self.to_json==other.to_json
end
end

+ 48
- 0
src/sales_backend/http/user.cr View File

@ -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

Loading…
Cancel
Save