Реализовать модуль регистрации ботов #19
This commit is contained in:
+42
-1
@@ -7,6 +7,7 @@
|
||||
-export([list_users/0]).
|
||||
-export([block/2, unblock/2]).
|
||||
-export([count_users/0, count_users_by_date/2]).
|
||||
-export([create_bot/2, delete_bot/1]).
|
||||
|
||||
%% Создание пользователя
|
||||
create(Email, Password) ->
|
||||
@@ -170,4 +171,44 @@ set_field(email, Value, U) -> U#user{email = Value};
|
||||
set_field(password_hash, Value, U) -> U#user{password_hash = Value};
|
||||
set_field(role, Value, U) when Value =:= user; Value =:= admin -> U#user{role = Value};
|
||||
set_field(status, Value, U) when Value =:= active; Value =:= frozen; Value =:= deleted -> U#user{status = Value};
|
||||
set_field(_, _, U) -> U.
|
||||
set_field(_, _, U) -> U.
|
||||
|
||||
%% ------------------------------------------------------------------
|
||||
%% API для ботов
|
||||
%% ------------------------------------------------------------------
|
||||
|
||||
-spec create_bot(Email :: binary(), Password :: binary()) ->
|
||||
{ok, User :: #user{}} | {error, duplicate_email | invalid_email}.
|
||||
create_bot(Email, Password) ->
|
||||
case email_exists(Email) of
|
||||
true ->
|
||||
{error, email_exists};
|
||||
false ->
|
||||
case mnesia:dirty_index_read(user, Email, email) of
|
||||
[] ->
|
||||
{ok, PasswordHash} = logic_auth:hash_password(Password),
|
||||
Id = generate_id(),
|
||||
User = #user{
|
||||
id = Id,
|
||||
email = Email,
|
||||
password_hash = PasswordHash,
|
||||
role = bot,
|
||||
status = active,
|
||||
created_at = calendar:universal_time(),
|
||||
updated_at = calendar:universal_time()
|
||||
},
|
||||
ok = mnesia:dirty_write(User),
|
||||
{ok, User};
|
||||
_ ->
|
||||
{error, duplicate_email}
|
||||
end
|
||||
end.
|
||||
|
||||
-spec delete_bot(Id :: binary()) -> ok | {error, not_found}.
|
||||
delete_bot(Id) ->
|
||||
case mnesia:dirty_read({user, Id}) of
|
||||
[#user{role = bot}] ->
|
||||
mnesia:dirty_delete({user, Id}),
|
||||
ok;
|
||||
[] -> {error, not_found}
|
||||
end.
|
||||
Reference in New Issue
Block a user