Conflicts:	examples/restbucks/src/domain/json_order_converter.e
Cosmetics
This commit is contained in:
Jocelyn Fiat
2011-10-11 14:25:22 +02:00
2 changed files with 55 additions and 51 deletions

View File

@@ -6,36 +6,30 @@
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_id, s_location, s_status: detachable STRING_32 s_id, s_location, s_status: detachable STRING_32
q: INTEGER_8 q: INTEGER_8
o: ORDER o: ORDER
i : ITEM i : ITEM
l_array : detachable LIST [JSON_VALUE] l_array : detachable ARRAYED_LIST [JSON_VALUE]
is_valid_from_json : BOOLEAN is_valid_from_json : BOOLEAN
do do
is_valid_from_json := True is_valid_from_json := True
@@ -105,7 +99,7 @@ feature -- Conversion
i := o.items.item_for_iteration i := o.items.item_for_iteration
create jv.make create jv.make
jv.put (json.value (i.name), name_key) 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.quantity), quantity_key)
jv.put (json.value (i.option), option_key) jv.put (json.value (i.option), option_key)
ja.add (jv) ja.add (jv)
@@ -115,7 +109,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")
@@ -155,12 +148,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

@@ -25,7 +25,7 @@ feature -- execute
execute (ctx: C; req: WGI_REQUEST; res: WGI_RESPONSE_BUFFER) execute (ctx: C; req: WGI_REQUEST; res: WGI_RESPONSE_BUFFER)
-- Execute request handler -- Execute request handler
do do
execute_methods(ctx,req,res) execute_methods (ctx, req, res)
end end
feature -- API DOC feature -- API DOC
@@ -41,47 +41,43 @@ 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
l_location : STRING l_location : STRING
l_order : detachable ORDER l_order : detachable ORDER
jv : detachable JSON_VALUE
h : EWF_HEADER h : EWF_HEADER
do do
fixme ("TODO handle an Internal Server Error") fixme ("TODO handle an Internal Server Error")
@@ -104,8 +100,7 @@ feature -- HTTP Methods
l_location := "http://"+host +req.request_uri+"/" + l_order.id l_location := "http://"+host +req.request_uri+"/" + l_order.id
h.add_header ("Location:"+ l_location) h.add_header ("Location:"+ l_location)
end end
jv ?= json.value (l_order) if attached {JSON_VALUE} json.value (l_order) as jv then
if jv /= Void then
h.put_content_length (jv.representation.count) h.put_content_length (jv.representation.count)
res.set_status_code ({HTTP_STATUS_CODE}.ok) res.set_status_code ({HTTP_STATUS_CODE}.ok)
res.write_headers_string (h.string) res.write_headers_string (h.string)
@@ -118,16 +113,14 @@ 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
h.put_status ({HTTP_STATUS_CODE}.no_content) h.put_status ({HTTP_STATUS_CODE}.no_content)
@@ -148,7 +141,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 +163,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
@@ -182,7 +180,7 @@ feature -- HTTP Methods
h.put_location (l_location) h.put_location (l_location)
end end
if attached req.request_time as time then 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") h.put_utc_date (time)
end end
res.set_status_code ({HTTP_STATUS_CODE}.created) res.set_status_code ({HTTP_STATUS_CODE}.created)
res.write_headers_string (h.string) res.write_headers_string (h.string)
@@ -190,8 +188,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 +238,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)