-
Notifications
You must be signed in to change notification settings - Fork 2
/
users_callback.erl
42 lines (38 loc) · 1.24 KB
/
users_callback.erl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
%% An <code>erf</code> callback for the users REST API.
-module(users_callback).
%%% EXTERNAL EXPORTS
-export([
create_user/1,
get_user/1,
delete_user/1
]).
%%%-------------------------------------------------------
%%% EXTERNAL EXPORTS
%%%-------------------------------------------------------
create_user(#{body := Body} = _Request) ->
Id = base64:encode(crypto:strong_rand_bytes(16)),
ets:insert(users, {Id, Body#{<<"id">> => Id}}),
{201, [], Body#{<<"id">> => Id}}.
get_user(#{path_parameters := PathParameters} = _Request) ->
Id = proplists:get_value(<<"userId">>, PathParameters),
case ets:lookup(users, Id) of
[] ->
{404, [], #{
<<"message">> =>
<<"User ", Id/binary, " not found">>
}};
[{Id, User}] ->
{200, [], User}
end.
delete_user(#{path_parameters := PathParameters} = _Request) ->
Id = proplists:get_value(<<"userId">>, PathParameters),
case ets:lookup(users, Id) of
[] ->
{404, [], #{
<<"message">> =>
<<"User ", Id/binary, " not found">>
}};
[_User] ->
ets:delete(users, Id),
{204, [], #{<<"id">> => Id}}
end.