Browse Source

Fixes get(Id) for users plus get-all endpoint

The users.erl file need a minor tweak to actually
get the data from the ets-table.

Also the cowboy-handler needed a clause to return
all users if it was requested.
Fabian N.C. van 't Hooft 4 years ago
parent
commit
d981292a6e
2 changed files with 15 additions and 3 deletions
  1. 1 1
      src/erp/users.erl
  2. 14 2
      src/rest_cowboy.erl

+ 1 - 1
src/erp/users.erl

@@ -9,7 +9,7 @@ init()               -> ets:new(users, [public, named_table, {keypos, #user.id}]
 populate(Users)      -> ets:insert(users, Users).
 exists(Id)           -> X = ets:member(users, binary_to_list(Id)), io:format("Member: ~p~n",[X]), X.
 get()                -> ets:tab2list(users).
-get(Id)              -> #user{id=Id}.
+get(Id)              -> [U] = ets:lookup(users, binary_to_list(Id)), io:format("User: ~p~n",[U]), U.
 delete(Id)           -> ets:delete(users, binary_to_list(Id)).
 post(#user{} = User) -> ets:insert(users, User);
 post(Data)           -> post(from_json(Data, #user{})).

+ 14 - 2
src/rest_cowboy.erl

@@ -26,8 +26,8 @@ resource_exists(#{bindings := #{resource := Module, id := Id}} = Req, State) ->
     M = c(Module),
     io:format("EXISTS: ~p dymamic: ~p~n",[Id,M:exists(Id)]),
     {M:exists(Id), Req, State};
-resource_exists(#{bindings := #{resource := _}} = Req, State) -> io:format("EXISTS: false~n"), {false, Req, State};
-resource_exists(#{bindings := #{id := _}} = Req, State) -> io:format("EXISTS: true~n"), {true, Req, State}.
+resource_exists(#{bindings := #{resource := Module}} = Req, State) -> io:format("resource ~p: no-id~n",[Module]), {true, Req, State};
+resource_exists(#{bindings := #{id := _}} = Req, State) -> io:format("EXISTS id: true~n"), {true, Req, State}.
 
 allowed_methods(#{bindings := #{resource := _}} = Req, State) -> {[<<"GET">>, <<"POST">>], Req, State};
 allowed_methods(#{bindings := #{resource := _, id := _}} = Req, State) -> {[<<"GET">>, <<"PUT">>, <<"DELETE">>], Req, State}.
@@ -51,12 +51,20 @@ to_html(#{bindings := #{resource := Module, id := Id}} = Req, State) ->
 default_html_layout(Body) -> [<<"<html><body>">>, Body, <<"</body></html>">>].
 
 to_json(#{bindings := #{resource := Module, id := Id}} = Req, State) ->
+    io:format("~p ~p ~p~n",[?FUNCTION_NAME,Module,Id]),
     M = c(Module),
     Struct = case Id of
                  undefined -> [{M, [ M:to_json(Resource) || Resource <- M:get() ] } ];
                  _         -> M:to_json(M:get(Id)) end,
+    {iolist_to_binary(?REST_JSON:encode(Struct)), Req, State};
+
+to_json(#{bindings := #{resource := Module}} = Req, State) ->
+    io:format("~p ~p~n",[?FUNCTION_NAME,Module]),
+    M = c(Module),
+    Struct = [{M, [ M:to_json(Resource) || Resource <- M:get() ] } ],
     {iolist_to_binary(?REST_JSON:encode(Struct)), Req, State}.
 
+
 content_types_accepted(Req, State) ->
   {[{<<"application/x-www-form-urlencoded">>, handle_urlencoded_data},
     {<<"application/json">>, handle_json_data}], Req, State}.
@@ -64,6 +72,7 @@ content_types_accepted(Req, State) ->
 handle_urlencoded_data(#{bindings := #{resource := Module}} = Req0, State) ->
     {ok, Data1, Req} = cowboy_req:read_urlencoded_body(Req0),
     io:format("FORM: ~p, Data1: ~p~n",[Module,Data1]),
+
     {handle_data(c(Module), [], Data1, Req), Req, State};
 
 handle_urlencoded_data(#{bindings := #{resource := Module, id := Id}} = Req, State) ->
@@ -84,11 +93,14 @@ handle_json_data(#{bindings := #{resource := Module, id := Id}} = Req, State) ->
     {handle_data(c(Module), Id, Data, Req), Req2, State}.
 
 handle_data(Mod, Id, Data, Req) ->
+    io:format("handle_data(~p)~n",[{Mod,Id,Data,Req}]),
     Valid = case erlang:function_exported(Mod, validate, 2) of
                 true  -> Mod:validate(Id, Data);
                 false -> default_validate(Mod, Id, Data, Req) end,
+    io:format("Valid ~p Id ~p~n",[Valid,Id]),
     case {Valid, Id} of
         {false, _}         -> false;
+        {true,  []}              -> Mod:post(Data);
         {true,  <<"undefined">>} -> Mod:post(Data);
         {true,  _}         -> case erlang:function_exported(Mod, put, 2) of
                                   true  -> Mod:put(Id, Data);