Updated, improved and cleaned RESTbucks example.
Moved it under "rest" subfolder.
This commit is contained in:
44
examples/rest/restbucks_CRUD/src/database/basic_database.e
Normal file
44
examples/rest/restbucks_CRUD/src/database/basic_database.e
Normal file
@@ -0,0 +1,44 @@
|
||||
note
|
||||
description: "[
|
||||
Basic database for simple example.
|
||||
|
||||
(no concurrency access control, ...)
|
||||
]"
|
||||
date: "$Date$"
|
||||
revision: "$Revision$"
|
||||
|
||||
deferred class
|
||||
BASIC_DATABASE
|
||||
|
||||
feature -- Access
|
||||
|
||||
count_of (a_entry_type: TYPE [detachable ANY]): INTEGER
|
||||
deferred
|
||||
end
|
||||
|
||||
has (a_entry_type: TYPE [detachable ANY]; a_id: READABLE_STRING_GENERAL): BOOLEAN
|
||||
-- Has entry of type `a_entry_type` associated with id `a_id`?
|
||||
deferred
|
||||
end
|
||||
|
||||
item (a_entry_type: TYPE [detachable ANY]; a_id: READABLE_STRING_GENERAL): detachable ANY
|
||||
deferred
|
||||
end
|
||||
|
||||
save (a_entry_type: TYPE [detachable ANY]; a_entry: detachable ANY; cl_entry_id: CELL [detachable READABLE_STRING_GENERAL])
|
||||
deferred
|
||||
ensure
|
||||
has_id: cl_entry_id.item /= Void
|
||||
end
|
||||
|
||||
delete (a_entry_type: TYPE [detachable ANY]; a_id: READABLE_STRING_GENERAL)
|
||||
require
|
||||
has_item: has (a_entry_type, a_id)
|
||||
deferred
|
||||
ensure
|
||||
has_not_item: not has (a_entry_type, a_id)
|
||||
end
|
||||
note
|
||||
copyright: "2011-2017, Javier Velilla and others"
|
||||
license: "Eiffel Forum License v2 (see http://www.eiffel.com/licensing/forum.txt)"
|
||||
end
|
||||
@@ -0,0 +1,177 @@
|
||||
note
|
||||
description: "[
|
||||
Basic database for simple example using JSON files.
|
||||
|
||||
(no concurrency access control, ...)
|
||||
]"
|
||||
date: "$Date$"
|
||||
revision: "$Revision$"
|
||||
|
||||
class
|
||||
BASIC_JSON_FS_DATABASE
|
||||
|
||||
inherit
|
||||
BASIC_DATABASE
|
||||
|
||||
create
|
||||
make
|
||||
|
||||
feature {NONE} -- Initialization
|
||||
|
||||
make (a_location: PATH)
|
||||
local
|
||||
d: DIRECTORY
|
||||
do
|
||||
location := a_location
|
||||
create serialization
|
||||
ensure_directory_exists (a_location)
|
||||
end
|
||||
|
||||
feature -- Access serialization
|
||||
|
||||
serialization: JSON_SERIALIZATION
|
||||
|
||||
feature -- Access
|
||||
|
||||
location: PATH
|
||||
|
||||
feature -- Access
|
||||
|
||||
count_of (a_entry_type: TYPE [detachable ANY]): INTEGER
|
||||
local
|
||||
d: DIRECTORY
|
||||
do
|
||||
create d.make_with_path (location.extended (entry_type_name (a_entry_type)))
|
||||
if d.exists then
|
||||
across
|
||||
d.entries as ic
|
||||
loop
|
||||
if attached ic.item.extension as e and then e.is_case_insensitive_equal ("json") then
|
||||
Result := Result + 1
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
has (a_entry_type: TYPE [detachable ANY]; a_id: READABLE_STRING_GENERAL): BOOLEAN
|
||||
-- Has entry of type `a_entry_type` associated with id `a_id`?
|
||||
local
|
||||
fut: FILE_UTILITIES
|
||||
do
|
||||
Result := fut.file_path_exists (entry_path (a_entry_type, a_id))
|
||||
end
|
||||
|
||||
item (a_entry_type: TYPE [detachable ANY]; a_id: READABLE_STRING_GENERAL): detachable ANY
|
||||
local
|
||||
f: RAW_FILE
|
||||
s: STRING
|
||||
do
|
||||
create f.make_with_path (entry_path (a_entry_type, a_id))
|
||||
if f.exists then
|
||||
create s.make (f.count)
|
||||
f.open_read
|
||||
from
|
||||
until
|
||||
f.exhausted or f.end_of_file
|
||||
loop
|
||||
f.read_stream (1_024)
|
||||
s.append (f.last_string)
|
||||
end
|
||||
f.close
|
||||
Result := serialization.from_json_string (s, a_entry_type)
|
||||
end
|
||||
end
|
||||
|
||||
save (a_entry_type: TYPE [detachable ANY]; a_entry: detachable ANY; cl_entry_id: CELL [detachable READABLE_STRING_GENERAL])
|
||||
local
|
||||
f: RAW_FILE
|
||||
l_id: detachable READABLE_STRING_GENERAL
|
||||
do
|
||||
l_id := cl_entry_id.item
|
||||
if l_id = Void then
|
||||
l_id := next_identifier (a_entry_type)
|
||||
cl_entry_id.replace (l_id)
|
||||
end
|
||||
create f.make_with_path (entry_path (a_entry_type, l_id))
|
||||
ensure_directory_exists (f.path.parent)
|
||||
f.open_write
|
||||
f.put_string (serialization.to_json (a_entry).representation)
|
||||
f.close
|
||||
end
|
||||
|
||||
delete (a_entry_type: TYPE [detachable ANY]; a_id: READABLE_STRING_GENERAL)
|
||||
local
|
||||
f: RAW_FILE
|
||||
do
|
||||
create f.make_with_path (entry_path (a_entry_type, a_id))
|
||||
if f.exists and then f.is_access_writable then
|
||||
f.delete
|
||||
end
|
||||
end
|
||||
|
||||
feature {NONE} -- Implementation
|
||||
|
||||
ensure_directory_exists (dn: PATH)
|
||||
local
|
||||
d: DIRECTORY
|
||||
do
|
||||
create d.make_with_path (dn)
|
||||
if not d.exists then
|
||||
d.recursive_create_dir
|
||||
end
|
||||
end
|
||||
|
||||
entry_path (a_entry_type: TYPE [detachable ANY]; a_id: READABLE_STRING_GENERAL): PATH
|
||||
do
|
||||
Result := location.extended (entry_type_name (a_entry_type)).extended (a_id).appended_with_extension ("json")
|
||||
end
|
||||
|
||||
entry_type_name (a_entry_type: TYPE [detachable ANY]): STRING
|
||||
do
|
||||
Result := a_entry_type.name.as_lower
|
||||
Result.prune_all ('!')
|
||||
end
|
||||
|
||||
last_id_file_path (a_entry_type: TYPE [detachable ANY]): PATH
|
||||
do
|
||||
Result := location.extended (entry_type_name (a_entry_type)).extended ("last-id")
|
||||
end
|
||||
|
||||
next_identifier (a_entry_type: TYPE [detachable ANY]): STRING_8
|
||||
local
|
||||
i: NATURAL_64
|
||||
f: RAW_FILE
|
||||
s: STRING
|
||||
do
|
||||
create f.make_with_path (last_id_file_path (a_entry_type))
|
||||
ensure_directory_exists (f.path.parent)
|
||||
if f.exists then
|
||||
create s.make (f.count)
|
||||
f.open_read
|
||||
f.read_line
|
||||
s := f.last_string
|
||||
f.close
|
||||
if s.is_natural_64 then
|
||||
i := s.to_natural_64
|
||||
end
|
||||
end
|
||||
from
|
||||
i := i + 1
|
||||
Result := i.out
|
||||
until
|
||||
not has (a_entry_type, Result)
|
||||
loop
|
||||
i := i + 1
|
||||
Result := i.out
|
||||
end
|
||||
f.open_write
|
||||
f.put_string (Result)
|
||||
f.close
|
||||
end
|
||||
|
||||
invariant
|
||||
|
||||
note
|
||||
copyright: "2011-2017, Javier Velilla and others"
|
||||
license: "Eiffel Forum License v2 (see http://www.eiffel.com/licensing/forum.txt)"
|
||||
end
|
||||
@@ -0,0 +1,102 @@
|
||||
note
|
||||
description: "[
|
||||
Basic database for simple example based on memory.
|
||||
]"
|
||||
date: "$Date$"
|
||||
revision: "$Revision$"
|
||||
|
||||
class
|
||||
BASIC_MEMORY_DATABASE
|
||||
|
||||
inherit
|
||||
BASIC_DATABASE
|
||||
|
||||
create
|
||||
make
|
||||
|
||||
feature {NONE} -- Initialization
|
||||
|
||||
make
|
||||
do
|
||||
create collections.make (0)
|
||||
end
|
||||
|
||||
collections: HASH_TABLE [STRING_TABLE [detachable ANY], TYPE [detachable ANY]]
|
||||
|
||||
feature -- Access
|
||||
|
||||
count_of (a_entry_type: TYPE [detachable ANY]): INTEGER
|
||||
do
|
||||
if attached collections.item (a_entry_type) as tb then
|
||||
Result := tb.count
|
||||
end
|
||||
end
|
||||
|
||||
has (a_entry_type: TYPE [detachable ANY]; a_id: READABLE_STRING_GENERAL): BOOLEAN
|
||||
-- Has entry of type `a_entry_type` associated with id `a_id`?
|
||||
do
|
||||
if attached collections.item (a_entry_type) as tb then
|
||||
Result := tb.has_key (a_id)
|
||||
end
|
||||
end
|
||||
|
||||
item (a_entry_type: TYPE [detachable ANY]; a_id: READABLE_STRING_GENERAL): detachable ANY
|
||||
do
|
||||
if attached collections.item (a_entry_type) as tb then
|
||||
Result := tb.item (a_id)
|
||||
end
|
||||
end
|
||||
|
||||
save (a_entry_type: TYPE [detachable ANY]; a_entry: detachable ANY; cl_entry_id: CELL [detachable READABLE_STRING_GENERAL])
|
||||
local
|
||||
tb: detachable STRING_TABLE [detachable ANY]
|
||||
l_id: detachable READABLE_STRING_GENERAL
|
||||
do
|
||||
tb := collections.item (a_entry_type)
|
||||
if tb = Void then
|
||||
create tb.make (100)
|
||||
collections.force (tb, a_entry_type)
|
||||
end
|
||||
l_id := cl_entry_id.item
|
||||
if l_id = Void then
|
||||
l_id := next_identifier (a_entry_type)
|
||||
cl_entry_id.replace (l_id)
|
||||
end
|
||||
tb.force (a_entry, l_id)
|
||||
end
|
||||
|
||||
delete (a_entry_type: TYPE [detachable ANY]; a_id: READABLE_STRING_GENERAL)
|
||||
do
|
||||
if attached collections.item (a_entry_type) as tb then
|
||||
tb.remove (a_id)
|
||||
end
|
||||
end
|
||||
|
||||
feature {NONE} -- Implementation
|
||||
|
||||
next_identifier (a_entry_type: TYPE [detachable ANY]): STRING_8
|
||||
local
|
||||
i: INTEGER
|
||||
f: RAW_FILE
|
||||
s: STRING
|
||||
do
|
||||
if attached collections.item (a_entry_type) as tb then
|
||||
i := tb.count
|
||||
else
|
||||
i := 0
|
||||
end
|
||||
from
|
||||
i := i + 1
|
||||
Result := i.out
|
||||
until
|
||||
not has (a_entry_type, Result)
|
||||
loop
|
||||
i := i + 1
|
||||
Result := i.out
|
||||
end
|
||||
end
|
||||
|
||||
note
|
||||
copyright: "2011-2017, Javier Velilla and others"
|
||||
license: "Eiffel Forum License v2 (see http://www.eiffel.com/licensing/forum.txt)"
|
||||
end
|
||||
173
examples/rest/restbucks_CRUD/src/database/restbucks_api.e
Normal file
173
examples/rest/restbucks_CRUD/src/database/restbucks_api.e
Normal file
@@ -0,0 +1,173 @@
|
||||
note
|
||||
description: "Summary description for {RESTBUCKS_API}."
|
||||
date: "$Date$"
|
||||
revision: "$Revision$"
|
||||
|
||||
class
|
||||
RESTBUCKS_API
|
||||
|
||||
create
|
||||
make
|
||||
|
||||
feature {NONE} -- Initialization
|
||||
|
||||
make
|
||||
local
|
||||
db: BASIC_JSON_FS_DATABASE
|
||||
do
|
||||
create db.make (database_path)
|
||||
db.serialization.register (create {ORDER_JSON_SERIALIZATION}, {ORDER})
|
||||
database := db
|
||||
end
|
||||
|
||||
feature -- Access
|
||||
|
||||
orders_count: INTEGER
|
||||
-- Number of existing orders.
|
||||
do
|
||||
Result := database.count_of ({ORDER})
|
||||
end
|
||||
|
||||
has_order (a_id: READABLE_STRING_GENERAL): BOOLEAN
|
||||
do
|
||||
Result := database.has ({ORDER}, a_id)
|
||||
end
|
||||
|
||||
order (a_id: READABLE_STRING_GENERAL): detachable ORDER
|
||||
do
|
||||
if attached {ORDER} database.item ({ORDER}, a_id) as o then
|
||||
Result := o
|
||||
end
|
||||
end
|
||||
|
||||
feature -- Element change
|
||||
|
||||
submit_order (a_order: ORDER)
|
||||
-- Submit new order `a_order`.
|
||||
require
|
||||
no_id: not a_order.has_id
|
||||
do
|
||||
a_order.mark_submitted
|
||||
save_order (a_order)
|
||||
end
|
||||
|
||||
save_order (a_order: ORDER)
|
||||
local
|
||||
cl: CELL [detachable READABLE_STRING_GENERAL]
|
||||
do
|
||||
a_order.add_revision
|
||||
if a_order.has_id then
|
||||
create cl.put (a_order.id)
|
||||
else
|
||||
create cl.put (Void)
|
||||
end
|
||||
database.save ({ORDER}, a_order, cl)
|
||||
if attached cl.item as l_new_id then
|
||||
if l_new_id.is_valid_as_string_8 then
|
||||
a_order.set_id (l_new_id.to_string_8)
|
||||
else
|
||||
check valid_id: False end
|
||||
end
|
||||
end
|
||||
ensure
|
||||
has_id: a_order.has_id
|
||||
incremented_revision: a_order.revision > old (a_order.revision)
|
||||
end
|
||||
|
||||
delete_order (a_order: ORDER)
|
||||
do
|
||||
database.delete ({ORDER}, a_order.id)
|
||||
end
|
||||
|
||||
feature -- Access: order status
|
||||
|
||||
is_valid_status_state (a_status: STRING): BOOLEAN
|
||||
-- Is `a_status' a valid order state
|
||||
do
|
||||
Result := Order_states.has (a_status.as_lower)
|
||||
end
|
||||
|
||||
Order_states : ARRAY [STRING]
|
||||
-- List of valid status states
|
||||
once
|
||||
Result := <<
|
||||
status_unset,
|
||||
status_submitted,
|
||||
status_pay, status_payed,
|
||||
status_cancel, status_canceled,
|
||||
status_prepare, status_prepared,
|
||||
status_deliver,
|
||||
status_completed
|
||||
>>
|
||||
Result.compare_objects
|
||||
end
|
||||
|
||||
is_valid_transition (a_order: ORDER; a_new_status: STRING): BOOLEAN
|
||||
-- Is transition from `a_order.status` to `a_new_status` valid for `a_order`?
|
||||
local
|
||||
l_order_status: READABLE_STRING_GENERAL
|
||||
l_new_status: STRING
|
||||
do
|
||||
l_order_status := a_order.status
|
||||
l_new_status := a_new_status.as_lower
|
||||
if l_order_status.same_string (l_new_status) then
|
||||
-- Same status is valid, if it is a valid status
|
||||
Result := is_valid_status_state (l_new_status)
|
||||
else
|
||||
if l_order_status.same_string (status_submitted) then
|
||||
Result := l_new_status.same_string (status_pay)
|
||||
or l_new_status.same_string (status_cancel)
|
||||
elseif l_order_status.same_string (status_pay) then
|
||||
Result := l_new_status.same_string (status_payed)
|
||||
elseif l_order_status.same_string (status_cancel) then
|
||||
Result := l_new_status.same_string (status_canceled)
|
||||
elseif l_order_status.same_string (status_payed) then
|
||||
Result := l_new_status.same_string (status_prepared)
|
||||
elseif l_order_status.same_string (status_prepared) then
|
||||
Result := l_new_status.same_string (status_deliver)
|
||||
elseif l_order_status.same_string (status_deliver) then
|
||||
Result := l_new_status.same_string (status_completed)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
is_state_valid_to_update (a_status : STRING) : BOOLEAN
|
||||
-- Is it possible to update order with status `a_status`?
|
||||
do
|
||||
if
|
||||
a_status.same_string (status_submitted)
|
||||
or else a_status.same_string (status_pay)
|
||||
or else a_status.same_string (status_payed)
|
||||
then
|
||||
Result := True
|
||||
end
|
||||
end
|
||||
|
||||
feature -- Constants: order status
|
||||
|
||||
status_unset: STRING = "unset"
|
||||
status_submitted: STRING = "submitted"
|
||||
status_pay: STRING = "pay"
|
||||
status_payed: STRING = "payed"
|
||||
status_cancel: STRING = "cancel"
|
||||
status_canceled: STRING = "canceled"
|
||||
status_prepare: STRING = "prepare"
|
||||
status_prepared: STRING = "prepared"
|
||||
status_deliver: STRING = "deliver"
|
||||
status_completed: STRING = "completed"
|
||||
|
||||
feature {NONE} -- Access
|
||||
|
||||
database: BASIC_DATABASE
|
||||
|
||||
feature {NONE} -- Implementation
|
||||
|
||||
database_path: PATH
|
||||
once
|
||||
create Result.make_from_string ("db")
|
||||
end
|
||||
|
||||
;note
|
||||
copyright: "2011-2017, Javier Velilla and others"
|
||||
license: "Eiffel Forum License v2 (see http://www.eiffel.com/licensing/forum.txt)"
|
||||
end
|
||||
@@ -0,0 +1,61 @@
|
||||
note
|
||||
description: "Summary description for {SHARED_RESTBUCKS_API}."
|
||||
date: "$Date$"
|
||||
revision: "$Revision$"
|
||||
|
||||
class
|
||||
SHARED_RESTBUCKS_API
|
||||
|
||||
feature -- Access: bridget to api
|
||||
|
||||
has_order (a_id: READABLE_STRING_GENERAL): BOOLEAN
|
||||
do
|
||||
Result := api.has_order (a_id)
|
||||
end
|
||||
|
||||
order (a_id: READABLE_STRING_GENERAL): detachable ORDER
|
||||
do
|
||||
Result := api.order (a_id)
|
||||
end
|
||||
|
||||
feature -- Element change
|
||||
|
||||
submit_order (a_order: ORDER)
|
||||
-- Submit new order `a_order`.
|
||||
require
|
||||
no_id: not a_order.has_id
|
||||
do
|
||||
api.submit_order (a_order)
|
||||
ensure
|
||||
a_order.has_id
|
||||
a_order.is_submitted
|
||||
end
|
||||
|
||||
update_order (a_order: ORDER)
|
||||
-- Update the order to the repository
|
||||
require
|
||||
a_order.has_id
|
||||
do
|
||||
api.save_order (a_order)
|
||||
ensure
|
||||
a_order_with_id: a_order.has_id
|
||||
end
|
||||
|
||||
delete_order (a_order: ORDER)
|
||||
require
|
||||
a_order_with_id: a_order.has_id
|
||||
do
|
||||
api.delete_order (a_order)
|
||||
end
|
||||
|
||||
feature -- Access
|
||||
|
||||
api: RESTBUCKS_API
|
||||
once
|
||||
create Result.make
|
||||
end
|
||||
|
||||
note
|
||||
copyright: "2011-2017, Javier Velilla and others"
|
||||
license: "Eiffel Forum License v2 (see http://www.eiffel.com/licensing/forum.txt)"
|
||||
end
|
||||
Reference in New Issue
Block a user