Skip to content

Commit

Permalink
started implementing put a device
Browse files Browse the repository at this point in the history
  • Loading branch information
ulfa committed Mar 23, 2011
1 parent b235de3 commit e362b9e
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 3 deletions.
17 changes: 14 additions & 3 deletions src/device_resource.erl
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,24 @@
%% API Functions
%%
init(_Config) ->
%%{{trace, "/tmp"}, #context{device=[]}}.
{ok, #context{device=[]}}.
{{trace, "/tmp"}, #context{device=[]}}.
%%{ok, #context{device=[]}}.

content_types_provided(ReqData, Context) ->
{[{"text/xml", to_xml}, {"text/html", to_html}],ReqData, Context}.

content_types_accepted(ReqData, Context) ->
{[{"application/x-www-form-urlencoded", save_device}], ReqData, Context}.

save_device(ReqData, Context) ->
case wrq:method(ReqData) of
'PUT' -> io:format("1... ~p~n", [wrq:req_body(ReqData)]),
{true, wrq:set_resp_body(<<"fuck">>, ReqData), Context};
_ -> {false, ReqData, Context}
end.

allowed_methods(ReqData, Context) ->
{['GET', 'DELETE', 'POST'], ReqData, Context}.
{['GET', 'DELETE', 'POST', 'PUT'], ReqData, Context}.

to_html(ReqData, #context{device=Device, group_name=Group_Name}=Context) ->
D = Device#device{groups=delete_caps_from_groups(Device#device.groups, Group_Name, [])},
Expand All @@ -56,6 +66,7 @@ to_xml(ReqData, #context{device = Device} = Context) ->
{D, ReqData, Context}.

resource_exists(ReqData, Context) ->
io:format("2...~n"),
Device = wrq:path_info(device, ReqData),
Group = get_group(ReqData),
process_request(Device, Group, ReqData, Context).
Expand Down
9 changes: 9 additions & 0 deletions src/wurfler.erl
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
-export([start_link/0, start/0]).
-export([searchByUA/1, searchByCapabilities/3, getDeviceById/1, getAllCapabilities/1, getVersion/0]).
-export([get_brands/0, get_brand/1, get_devices_by_model/1, check_device/3, delete_device/1, delete_brand/1]).
-export([saveDevice/2]).
-compile([export_all]).
-define(TIMEOUT, infinity).
-define(CONTAINS, fun({device, [{model_name, Model_Name},_], []}) ->
Expand Down Expand Up @@ -69,6 +70,8 @@ getVersion() ->
gen_server:call(?MODULE, {version}).
check_device(Capabilities, Key, Id) ->
gen_server:call(?MODULE, {check_device, Capabilities, Key, Id}, ?TIMEOUT).
saveDevice(newdevice, Device_Tuple) ->
gen_server:call(?MODULE, {save_new_device, Device_Tuple}, ?TIMEOUT).
%% @spec delete_device(Id::string()) -> List of device Ids
%% List = [string()]
%% @doc This functions gets an device Id and than seeks all device which depends
Expand Down Expand Up @@ -136,8 +139,12 @@ handle_call({delete_device, Id}, _From, State) ->
handle_call({delete_brand, Brand}, _From, State) ->
Result = deleteBrand(Brand),
{reply, Result, State};
handle_call({save_new_device, Device_Tuple}, _From, State) ->
Result = save_new_device(Device_Tuple),
{reply, Result, State};
handle_call({version}, _From, State) ->
{reply, get_version(), State}.

%% --------------------------------------------------------------------
%% Function: handle_cast/2
%% Description: Handling cast messages
Expand Down Expand Up @@ -173,6 +180,8 @@ code_change(_OldVsn, State, _Extra) ->
%% --------------------------------------------------------------------
%%% Internal functions
%% --------------------------------------------------------------------
save_new_device(Device_Tuple) ->
ok.
get_version() ->
{ok, Version} = application:get_key(wurflerservice, vsn),
Version.
Expand Down
4 changes: 4 additions & 0 deletions src/wurfler_db.erl
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ create_db() ->
application:start(mnesia),
mnesia:create_table(devicesTbl,[{type, set},{index, [user_agent,fall_back, actual_device_root]},
{record_name, device},{disc_copies, [node()]}, {attributes, record_info(fields, device)}]),
mnesia:create_table(new_devicesTbl,[{type, set},{index, [user_agent,fall_back, actual_device_root]},
{record_name, device},{disc_copies, [node()]}, {attributes, record_info(fields, device)}]),
mnesia:create_table(brand_index,[{disc_copies, [node()]}, {attributes, record_info(fields, brand_index)}]),
mnesia:create_table(changed_caps_devices,[{record_name, capabilities_devices}, {disc_copies, [node()]}, {attributes, record_info(fields, capabilities_devices)}]),
mnesia:create_table(capabilities_devices,[{disc_copies, [node()]}, {attributes, record_info(fields, capabilities_devices)}]),
Expand All @@ -62,6 +64,8 @@ create_db() ->
%% --------------------------------------------------------------------
%% save functions
%% --------------------------------------------------------------------
save_device(new_devicesTbl, Device) ->
mnesia:activity(transaction, fun() -> mnesia:write(new_devicesTbl, Device, write) end);
save_device(devicesTbl, Device)->
mnesia:activity(transaction, fun() -> mnesia:write(devicesTbl, Device, write) end).

Expand Down

0 comments on commit e362b9e

Please sign in to comment.