Update order_handler, fix json_order_converter

This commit is contained in:
jvelilla
2011-10-11 08:18:46 -03:00
parent 19da4d6fd0
commit eb44eef885
2 changed files with 74 additions and 69 deletions

View File

@@ -6,73 +6,64 @@
class class
JSON_ORDER_CONVERTER JSON_ORDER_CONVERTER
inherit inherit
JSON_CONVERTER JSON_CONVERTER
create create
make make
feature -- Initialization feature -- Initialization
make make
do do
create object.make ("","","") create object.make ("","","")
end end
feature -- Access feature -- Access
object : ORDER object : ORDER
value: detachable JSON_OBJECT
value : detachable JSON_OBJECT
feature -- Conversion 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 -- Convert from JSON value. Returns Void if unable to convert
local local
s_name, s_key, s_option: detachable STRING_32 lstr1, lstr2, lstr3 : detachable STRING_32
q: INTEGER_8 q: detachable INTEGER_8
o: ORDER o: ORDER
i : ITEM 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 is_valid_from_json : BOOLEAN
do do
is_valid_from_json := True 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) create o.make (lstr1, lstr2, lstr3)
s_key ?= json.object (j.item (location_key), Void)
s_option ?= json.object (j.item (status_key), Void)
create o.make (s_name, s_key, s_option) if l_val /= void then
if attached {JSON_ARRAY} j.item (items_key) as l_val then
l_array := l_val.array_representation l_array := l_val.array_representation
from from
l_array.start l_array.start
until until
l_array.after l_array.after
loop loop
if attached {JSON_OBJECT} l_array.item_for_iteration as jv then jv ?= l_array.item_for_iteration
if attached {INTEGER_8} json.object (jv.item (quantity_key), Void) as l_integer then if jv /= Void then
q := l_integer lstr1 ?= json.object (jv.item (name_key), Void)
else lstr2 ?= json.object (jv.item (size_key), Void)
q := 0 lstr3 ?= json.object (jv.item (option_key), Void)
end q ?= json.object (jv.item (quantity_key),Void)
if lstr1/= Void and then lstr2 /= Void and then lstr3 /= Void then
s_name ?= json.object (jv.item (id_key), Void) if is_valid_item_customization(lstr1,lstr2,lstr3,q) then
s_key ?= json.object (jv.item (location_key), Void) create i.make (lstr1, lstr2,lstr3, q)
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)
o.add_item (i) o.add_item (i)
else else
is_valid_from_json := False is_valid_from_json := false
end end
else else
is_valid_from_json := False is_valid_from_json := false
end end
end end
@@ -84,6 +75,7 @@ feature -- Conversion
else else
Result := o Result := o
end end
end end
to_json (o: like object): like value to_json (o: like object): like value
@@ -116,7 +108,6 @@ feature -- Conversion
end end
feature {NONE} -- Implementation feature {NONE} -- Implementation
id_key: JSON_STRING id_key: JSON_STRING
once once
create Result.make_json ("id") create Result.make_json ("id")
@@ -156,12 +147,12 @@ feature -- Conversion
create Result.make_json ("quantity") create Result.make_json ("quantity")
end end
option_key : JSON_STRING option_key : JSON_STRING
once once
create Result.make_json ("option") create Result.make_json ("option")
end end
feature -- Validation feature -- Validation
is_valid_item_customization ( name : STRING_32; size: STRING_32; option : STRING_32; quantity : INTEGER_8 ) : BOOLEAN is_valid_item_customization ( name : STRING_32; size: STRING_32; option : STRING_32; quantity : INTEGER_8 ) : BOOLEAN

View File

@@ -41,41 +41,39 @@ feature -- HTTP Methods
-- If the GET request is not SUCCESS, we response with -- If the GET request is not SUCCESS, we response with
-- 404 Resource not found -- 404 Resource not found
local local
joc : JSON_ORDER_CONVERTER
l_order : detachable ORDER
jv : detachable JSON_VALUE
id : STRING id : STRING
uri : LIST [READABLE_STRING_32]
h : EWF_HEADER
do do
if attached req.orig_path_info as orig_path then if attached req.orig_path_info as orig_path then
uri := orig_path.split ('/') id := get_order_id_from_path (orig_path)
id := uri.at (3) if attached retrieve_order (id) as l_order then
create joc.make compute_response_get (ctx, req, res, l_order)
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
else else
handle_resource_not_found_response ("The following resource" + orig_path + " is not found ", ctx, req, res) handle_resource_not_found_response ("The following resource" + orig_path + " is not found ", ctx, req, res)
end end
end 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) do_put (ctx: C; req: WGI_REQUEST; res: WGI_RESPONSE_BUFFER)
local local
l_post: STRING l_post: STRING
@@ -118,15 +116,13 @@ feature -- HTTP Methods
do_delete (ctx: C; req: WGI_REQUEST; res: WGI_RESPONSE_BUFFER) do_delete (ctx: C; req: WGI_REQUEST; res: WGI_RESPONSE_BUFFER)
local local
uri: LIST [READABLE_STRING_32]
id: STRING id: STRING
h : EWF_HEADER h : EWF_HEADER
do do
fixme ("TODO handle an Internal Server Error") fixme ("TODO handle an Internal Server Error")
fixme ("Refactor the code, create new abstractions") fixme ("Refactor the code, create new abstractions")
if attached req.orig_path_info as orig_path then if attached req.orig_path_info as orig_path then
uri := orig_path.split ('/') id := get_order_id_from_path (orig_path)
id := uri.at (3)
if db_access.orders.has_key (id) then if db_access.orders.has_key (id) then
delete_order( id) delete_order( id)
create h.make create h.make
@@ -148,7 +144,7 @@ feature -- HTTP Methods
-- POST is used for creation and the server determines the URI -- POST is used for creation and the server determines the URI
-- of the created resource. -- of the created resource.
-- If the request post is SUCCESS, the server will create the order and will response with -- 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 -- 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 400 BAD REQUEST, the client send a bad request
-- HTTP_RESPONSE 500 INTERNAL_SERVER_ERROR, when the server can deliver the request -- HTTP_RESPONSE 500 INTERNAL_SERVER_ERROR, when the server can deliver the request
@@ -170,8 +166,13 @@ feature -- HTTP Methods
h: EWF_HEADER h: EWF_HEADER
l_msg : STRING l_msg : STRING
l_location : STRING l_location : STRING
joc : JSON_ORDER_CONVERTER
do do
create h.make create h.make
create joc.make
json.add_converter(joc)
h.put_status ({HTTP_STATUS_CODE}.created) h.put_status ({HTTP_STATUS_CODE}.created)
h.put_content_type_application_json h.put_content_type_application_json
if attached {JSON_VALUE} json.value (l_order) as jv then if attached {JSON_VALUE} json.value (l_order) as jv then
@@ -190,8 +191,21 @@ feature -- HTTP Methods
end end
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 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_order (an_order: ORDER)
-- save the order to the repository -- save the order to the repository
local local
@@ -227,8 +241,8 @@ feature {NONE} -- Implementation Repository Layer
-- extract an object Order from the request, or Void -- extract an object Order from the request, or Void
-- if the request is invalid -- if the request is invalid
local local
joc : JSON_ORDER_CONVERTER
parser : JSON_PARSER parser : JSON_PARSER
joc : JSON_ORDER_CONVERTER
do do
create joc.make create joc.make
json.add_converter(joc) json.add_converter(joc)