Update order_handler, fix json_order_converter
This commit is contained in:
@@ -6,73 +6,64 @@
|
||||
|
||||
class
|
||||
JSON_ORDER_CONVERTER
|
||||
|
||||
inherit
|
||||
JSON_CONVERTER
|
||||
|
||||
create
|
||||
make
|
||||
|
||||
feature -- Initialization
|
||||
|
||||
make
|
||||
do
|
||||
create object.make ("", "", "")
|
||||
create object.make ("","","")
|
||||
end
|
||||
|
||||
feature -- Access
|
||||
object : ORDER
|
||||
|
||||
object: ORDER
|
||||
|
||||
value: detachable JSON_OBJECT
|
||||
|
||||
value : detachable JSON_OBJECT
|
||||
feature -- Conversion
|
||||
|
||||
from_json (j: attached like value): detachable like object
|
||||
from_json (j: attached like value): detachable like object
|
||||
-- Convert from JSON value. Returns Void if unable to convert
|
||||
local
|
||||
s_name, s_key, s_option: detachable STRING_32
|
||||
q: INTEGER_8
|
||||
lstr1, lstr2, lstr3 : detachable STRING_32
|
||||
q: detachable INTEGER_8
|
||||
o: ORDER
|
||||
i : ITEM
|
||||
l_array : detachable LIST [JSON_VALUE]
|
||||
l_val : detachable JSON_ARRAY
|
||||
l_array : detachable ARRAYED_LIST[JSON_VALUE]
|
||||
jv : detachable JSON_OBJECT
|
||||
is_valid_from_json : BOOLEAN
|
||||
do
|
||||
is_valid_from_json := True
|
||||
lstr1 ?= json.object (j.item (id_key), Void)
|
||||
lstr2 ?= json.object (j.item (location_key), Void)
|
||||
lstr3 ?= json.object (j.item (status_key), Void)
|
||||
l_val ?= j.item (items_key)
|
||||
|
||||
s_name ?= json.object (j.item (id_key), Void)
|
||||
s_key ?= json.object (j.item (location_key), Void)
|
||||
s_option ?= json.object (j.item (status_key), Void)
|
||||
create o.make (lstr1, lstr2, lstr3)
|
||||
|
||||
create o.make (s_name, s_key, s_option)
|
||||
|
||||
if attached {JSON_ARRAY} j.item (items_key) as l_val then
|
||||
if l_val /= void then
|
||||
l_array := l_val.array_representation
|
||||
from
|
||||
l_array.start
|
||||
until
|
||||
l_array.after
|
||||
loop
|
||||
if attached {JSON_OBJECT} l_array.item_for_iteration as jv then
|
||||
if attached {INTEGER_8} json.object (jv.item (quantity_key), Void) as l_integer then
|
||||
q := l_integer
|
||||
else
|
||||
q := 0
|
||||
end
|
||||
|
||||
s_name ?= json.object (jv.item (id_key), Void)
|
||||
s_key ?= json.object (jv.item (location_key), Void)
|
||||
s_option ?= json.object (jv.item (status_key), Void)
|
||||
|
||||
if s_name /= Void and s_key /= Void and s_option /= Void then
|
||||
if is_valid_item_customization (s_name, s_key, s_option,q) then
|
||||
create i.make (s_name, s_key, s_option, q)
|
||||
jv ?= l_array.item_for_iteration
|
||||
if jv /= Void then
|
||||
lstr1 ?= json.object (jv.item (name_key), Void)
|
||||
lstr2 ?= json.object (jv.item (size_key), Void)
|
||||
lstr3 ?= json.object (jv.item (option_key), Void)
|
||||
q ?= json.object (jv.item (quantity_key),Void)
|
||||
if lstr1/= Void and then lstr2 /= Void and then lstr3 /= Void then
|
||||
if is_valid_item_customization(lstr1,lstr2,lstr3,q) then
|
||||
create i.make (lstr1, lstr2,lstr3, q)
|
||||
o.add_item (i)
|
||||
else
|
||||
is_valid_from_json := False
|
||||
is_valid_from_json := false
|
||||
end
|
||||
else
|
||||
is_valid_from_json := False
|
||||
is_valid_from_json := false
|
||||
end
|
||||
end
|
||||
|
||||
@@ -84,6 +75,7 @@ feature -- Conversion
|
||||
else
|
||||
Result := o
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
to_json (o: like object): like value
|
||||
@@ -106,7 +98,7 @@ feature -- Conversion
|
||||
i := o.items.item_for_iteration
|
||||
create jv.make
|
||||
jv.put (json.value (i.name), name_key)
|
||||
jv.put (json.value (i.size), size_key)
|
||||
jv.put (json.value (i.size),size_key)
|
||||
jv.put (json.value (i.quantity), quantity_key)
|
||||
jv.put (json.value (i.option), option_key)
|
||||
ja.add (jv)
|
||||
@@ -116,7 +108,6 @@ feature -- Conversion
|
||||
end
|
||||
|
||||
feature {NONE} -- Implementation
|
||||
|
||||
id_key: JSON_STRING
|
||||
once
|
||||
create Result.make_json ("id")
|
||||
@@ -156,12 +147,12 @@ feature -- Conversion
|
||||
create Result.make_json ("quantity")
|
||||
end
|
||||
|
||||
|
||||
option_key : JSON_STRING
|
||||
|
||||
once
|
||||
create Result.make_json ("option")
|
||||
end
|
||||
|
||||
feature -- Validation
|
||||
|
||||
is_valid_item_customization ( name : STRING_32; size: STRING_32; option : STRING_32; quantity : INTEGER_8 ) : BOOLEAN
|
||||
|
||||
@@ -41,41 +41,39 @@ feature -- HTTP Methods
|
||||
-- If the GET request is not SUCCESS, we response with
|
||||
-- 404 Resource not found
|
||||
local
|
||||
joc : JSON_ORDER_CONVERTER
|
||||
l_order : detachable ORDER
|
||||
jv : detachable JSON_VALUE
|
||||
id : STRING
|
||||
uri : LIST [READABLE_STRING_32]
|
||||
h : EWF_HEADER
|
||||
do
|
||||
if attached req.orig_path_info as orig_path then
|
||||
uri := orig_path.split ('/')
|
||||
id := uri.at (3)
|
||||
create joc.make
|
||||
json.add_converter(joc)
|
||||
if db_access.orders.has_key (id) then
|
||||
l_order := db_access.orders.item (id)
|
||||
jv ?= json.value (l_order)
|
||||
if attached jv as j then
|
||||
create h.make
|
||||
h.put_status ({HTTP_STATUS_CODE}.ok)
|
||||
h.put_content_type ("application/json")
|
||||
if attached req.request_time as time then
|
||||
h.add_header ("Date:" +time.formatted_out ("ddd,[0]dd mmm yyyy [0]hh:[0]mi:[0]ss.ff2") + " GMT")
|
||||
end
|
||||
if l_order /= Void then
|
||||
h.add_header ("Etag: " + l_order.etag)
|
||||
end
|
||||
res.set_status_code ({HTTP_STATUS_CODE}.ok)
|
||||
res.write_headers_string (h.string)
|
||||
res.write_string (j.representation)
|
||||
end
|
||||
id := get_order_id_from_path (orig_path)
|
||||
if attached retrieve_order (id) as l_order then
|
||||
compute_response_get (ctx, req, res, l_order)
|
||||
else
|
||||
handle_resource_not_found_response ("The following resource" + orig_path + " is not found ", ctx, req, res)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
compute_response_get (ctx: C; req: WGI_REQUEST; res: WGI_RESPONSE_BUFFER; l_order : ORDER)
|
||||
local
|
||||
h: EWF_HEADER
|
||||
l_msg : STRING
|
||||
do
|
||||
create h.make
|
||||
h.put_status ({HTTP_STATUS_CODE}.ok)
|
||||
h.put_content_type_application_json
|
||||
if attached {JSON_VALUE} json.value (l_order) as jv then
|
||||
l_msg := jv.representation
|
||||
h.put_content_length (l_msg.count)
|
||||
if attached req.request_time as time then
|
||||
h.add_header ("Date:" + time.formatted_out ("ddd,[0]dd mmm yyyy [0]hh:[0]mi:[0]ss.ff2") + " GMT")
|
||||
end
|
||||
res.set_status_code ({HTTP_STATUS_CODE}.ok)
|
||||
res.write_headers_string (h.string)
|
||||
res.write_string (l_msg)
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
do_put (ctx: C; req: WGI_REQUEST; res: WGI_RESPONSE_BUFFER)
|
||||
local
|
||||
l_post: STRING
|
||||
@@ -118,15 +116,13 @@ feature -- HTTP Methods
|
||||
|
||||
do_delete (ctx: C; req: WGI_REQUEST; res: WGI_RESPONSE_BUFFER)
|
||||
local
|
||||
uri: LIST [READABLE_STRING_32]
|
||||
id: STRING
|
||||
h : EWF_HEADER
|
||||
do
|
||||
fixme ("TODO handle an Internal Server Error")
|
||||
fixme ("Refactor the code, create new abstractions")
|
||||
if attached req.orig_path_info as orig_path then
|
||||
uri := orig_path.split ('/')
|
||||
id := uri.at (3)
|
||||
id := get_order_id_from_path (orig_path)
|
||||
if db_access.orders.has_key (id) then
|
||||
delete_order( id)
|
||||
create h.make
|
||||
@@ -148,7 +144,7 @@ feature -- HTTP Methods
|
||||
-- POST is used for creation and the server determines the URI
|
||||
-- of the created resource.
|
||||
-- If the request post is SUCCESS, the server will create the order and will response with
|
||||
-- HTTP_RESPONSE 201 CREATED
|
||||
-- HTTP_RESPONSE 201 CREATED, the Location header will contains the newly created order's URI
|
||||
-- if the request post is not SUCCESS, the server will response with
|
||||
-- HTTP_RESPONSE 400 BAD REQUEST, the client send a bad request
|
||||
-- HTTP_RESPONSE 500 INTERNAL_SERVER_ERROR, when the server can deliver the request
|
||||
@@ -170,8 +166,13 @@ feature -- HTTP Methods
|
||||
h: EWF_HEADER
|
||||
l_msg : STRING
|
||||
l_location : STRING
|
||||
joc : JSON_ORDER_CONVERTER
|
||||
do
|
||||
create h.make
|
||||
|
||||
create joc.make
|
||||
json.add_converter(joc)
|
||||
|
||||
h.put_status ({HTTP_STATUS_CODE}.created)
|
||||
h.put_content_type_application_json
|
||||
if attached {JSON_VALUE} json.value (l_order) as jv then
|
||||
@@ -190,8 +191,21 @@ feature -- HTTP Methods
|
||||
end
|
||||
end
|
||||
|
||||
feature {NONE} -- URI helper methods
|
||||
|
||||
get_order_id_from_path ( a_path: READABLE_STRING_32) : STRING
|
||||
do
|
||||
Result := a_path.split ('/').at(3)
|
||||
end
|
||||
|
||||
feature {NONE} -- Implementation Repository Layer
|
||||
|
||||
retrieve_order ( id : STRING) : detachable ORDER
|
||||
-- get the order by id if it exist, in other case, Void
|
||||
do
|
||||
Result := db_access.orders.item (id)
|
||||
end
|
||||
|
||||
save_order (an_order: ORDER)
|
||||
-- save the order to the repository
|
||||
local
|
||||
@@ -227,8 +241,8 @@ feature {NONE} -- Implementation Repository Layer
|
||||
-- extract an object Order from the request, or Void
|
||||
-- if the request is invalid
|
||||
local
|
||||
joc : JSON_ORDER_CONVERTER
|
||||
parser : JSON_PARSER
|
||||
joc : JSON_ORDER_CONVERTER
|
||||
do
|
||||
create joc.make
|
||||
json.add_converter(joc)
|
||||
|
||||
Reference in New Issue
Block a user