Skip to content

Commit

Permalink
working on enhancement
Browse files Browse the repository at this point in the history
  • Loading branch information
ulfa committed Aug 22, 2011
1 parent b93a00e commit 6ce6e50
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 12 deletions.
32 changes: 26 additions & 6 deletions entity_db.erl
Original file line number Diff line number Diff line change
Expand Up @@ -18,34 +18,54 @@
%%% Description :
%%% Created :
%%% -------------------------------------------------------------------
-module({{appid}}_db).
-module({{entity}}_db).
%% --------------------------------------------------------------------
%% Include files
%% --------------------------------------------------------------------
-include_lib("stdlib/include/qlc.hrl").
-include("../include/wurfler.hrl").
-include("../include/{{appid}}.hrl").
%% --------------------------------------------------------------------
%% External exports
%% --------------------------------------------------------------------
-export([create_db/0]).
-export([create/1, update/1, delete/1, find_by_id/1]).
-export([create/1, update/1, delete/1, find_by_id/1, find_free_id/1]).

create_db() ->
case mnesia:create_schema([node()]) of
{error, Reason} -> error_logger:info_msg(Reason),
false;
_ -> application:start(mnesia),
mnesia:create_table({{entity}},[{disc_copies, [node()]}, {attributes, record_info(fields, {{entity}})}]).
mnesia:create_table({{entity}},[{disc_copies, [node()]}, {attributes, record_info(fields, {{entity}})}]),
mnesia:wait_for_tables([{{entity}}], 100000),
application:stop(mnesia)
end.

create(Entity) ->
mnesia:activity(transaction, fun() -> mnesia:write(Entity, write) end).

read(Id) ->
find_by_id(Id) ->
mnesia:activity(transaction, fun() -> mnesia:read(Id) end).

update(Entity) ->
ok.

delete(Id) ->
mnesia:activity(transaction, fun() -> mnesia:delete({{entity}}, Id) end).
mnesia:activity(transaction, fun() -> mnesia:delete({{entity}}, Id) end).

find_free_id(Id) ->
case find_by_id(Id) of
[] -> Id;
_ -> find_free_id(Id, 0)
end.

find_free_id(Id, Count) ->
case find_by_id(Id ++ integer_to_list(Count)) of
[] -> Id ++ integer_to_list(Count);
_ -> find_free_id(Id, Count + 1)
end.
%% --------------------------------------------------------------------
%%% Test functions
%% --------------------------------------------------------------------
-include_lib("eunit/include/eunit.hrl").
-ifdef(TEST).
-endif.
32 changes: 28 additions & 4 deletions entity_resource.erl
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
%% Include files
%% --------------------------------------------------------------------
-include_lib("../deps/webmachine/include/webmachine.hrl").
--include("../include/{{appid}}.hrl").
-include("../include/{{appid}}.hrl").
%% --------------------------------------------------------------------
%% record definitions
%% --------------------------------------------------------------------
Expand Down Expand Up @@ -99,7 +99,10 @@ allowed_methods(ReqData, Context) ->
% and should return true if the deletion succeeded.
%
delete_resource(ReqData, Context) ->
{false, ReqData, Context}.
case delete(wrq:disp_path(ReqData)) of
ok -> {true, ReqData, Context};
_ -> {false, ReqData, Context}
end.
%
% This is only called after a successful delete_resource call, and should
% return false if the deletion was accepted but cannot yet be guaranteed to have finished.
Expand All @@ -122,7 +125,14 @@ post_is_create(ReqData, Context) ->
% for all subsequent resource function calls in the course of this request.
%
create_path(ReqData, Context) ->
{undefined, ReqData, Context}.
case wrq:get_req_header("slug", ReqData) of
undefined -> {euuid:random(), ReqData, Context};
Slug ->
case {{appid}}_db:find_by_id(Slug) of
[] -> {Slug, ReqData, Context};
_ -> { {{appid}}_db:find_free_id(Slug), ReqData, Context}
end
end.
%
% If post_is_create returns false, then this will be called to process any POST requests.
% If it succeeds, it should return true.
Expand All @@ -145,7 +155,7 @@ content_types_provided(ReqData, Context) ->
% want to use wrq:req_body(ReqData) to access the incoming request body.
%
content_types_accepted(ReqData, Context) ->
{[], ReqData, Context}.
{[{"application/json", accept_content_json}, {"text/xml", accept_content_xml}, {"text/html", accept_content_html}], ReqData, Context}.
%
% If this is anything other than the atom no_charset, it must be a list of pairs where
% each pair is of the form Charset, Converter where Charset is a string naming a charset
Expand Down Expand Up @@ -221,11 +231,25 @@ finish_request(ReqData, Context) ->
%% --------------------------------------------------------------------
%%% Additional functions
%% --------------------------------------------------------------------
accept_content_json(ReqData, Context) ->
Content = wrq:req_body(ReqData),
{true, ReqData, Context}.
accept_content_xml(ReqData, Context) ->
{true, ReqData, Context}.
accept_content_html(ReqData, Context) ->
{true, ReqData, Context}.

to_html(ReqData, Context) ->
{io_lib:format("<html><body>~s</body><html>", [erlang:iolist_to_binary("Hello Template")]), ReqData, Context}.
to_json(ReqData, Context) ->
{undefined, ReqData, Context}.
to_xml(ReqData, Context) ->
{undefined, ReqData, Context}.
%% --------------------------------------------------------------------
%%% Internal functions
%% --------------------------------------------------------------------
hash_body(Body) ->
mochihex:to_hex(binary_to_list(crypto:sha(Body))).
create(Entity) ->
{{appid}}_db:create(Entity).
update(Entity) ->
Expand Down
2 changes: 2 additions & 0 deletions wm_service.erl
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ init([]) ->
%% {error, Reason}
%% --------------------------------------------------------------------
start() ->
{{entity}}_db:create_db(),
ensure_started(crypto),
ensure_started(mnesia),
ensure_started(webmachine),
Expand All @@ -93,6 +94,7 @@ start_link(_Type, _Args) ->
%% {error, Reason}
%% --------------------------------------------------------------------
start(_Type, _Args) ->
{{entity}}_db:create_db(),
ensure_started(crypto),
ensure_started(mnesia),
ensure_started(webmachine),
Expand Down
7 changes: 5 additions & 2 deletions wm_service.template
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{variables, [{appid, "myapp"}, {author, "Ulf"}, {date,""}, {email, "uaforum1@googlemail.com"}, {port, 8000}]}.
{variables, [{appid, "myapp"}, {author, "Ulf"}, {date,""}, {email, "uaforum1@googlemail.com"}, {port, 8000}, {entity, ""}]}.
{template, "rebar.config", "rebar.config"}.
{template, "wm_service.app.src", "src/{{appid}}_service.app.src"}.
{template, "dispatch.conf", "priv/dispatch.conf"}.
Expand All @@ -10,4 +10,7 @@
{chmod, 8#744, "test.sh"}.
{template, "test.spec.in", "test/test.spec.in"}.
{template, "install.sh", "install.sh"}.
{template, "common_SUITE.erl", "test/{{appid}}_SUITE.erl"}.
{template, "common_SUITE.erl", "test/{{appid}}_SUITE.erl"}.
{template, "entity_resource.erl", "src/{{appid}}_resource.erl"}.
{template, "entity_db.erl", "src/{{entity}}_db.erl"}.
{template, "wm_service.hrl", "include/{{appid}}.hrl"}.

0 comments on commit 6ce6e50

Please sign in to comment.