Рефакторинг админских обработчиков - пагинация, сортировка, фильтрация. Часть 1
This commit is contained in:
@@ -6,7 +6,6 @@
|
||||
%%%-------------------------------------------------------------------
|
||||
-module(admin_handler_me).
|
||||
-behaviour(cowboy_handler).
|
||||
|
||||
-export([init/2]).
|
||||
-export([trails/0]).
|
||||
|
||||
@@ -18,33 +17,33 @@ init(Req, _Opts) ->
|
||||
case cowboy_req:method(Req) of
|
||||
<<"GET">> -> get_me(Req);
|
||||
<<"PUT">> -> update_me(Req);
|
||||
_ -> handler_utils:send_error(Req, 405, <<"Method not allowed">>)
|
||||
_ -> handler_utils:send_error(Req, 405, <<"Method not allowed">>)
|
||||
end.
|
||||
|
||||
%%% Swagger metadata
|
||||
-spec trails() -> [map()].
|
||||
trails() ->
|
||||
[
|
||||
#{ % GET
|
||||
path => <<"/v1/admin/me">>,
|
||||
method => <<"GET">>,
|
||||
#{ % GET
|
||||
path => <<"/v1/admin/me">>,
|
||||
method => <<"GET">>,
|
||||
description => <<"Get current admin profile">>,
|
||||
tags => [<<"Admins">>],
|
||||
responses => #{
|
||||
tags => [<<"Admins">>],
|
||||
responses => #{
|
||||
200 => #{
|
||||
description => <<"Admin profile">>,
|
||||
content => #{<<"application/json">> => #{schema => admin_schema()}}
|
||||
content => #{<<"application/json">> => #{schema => admin_schema()}}
|
||||
}
|
||||
}
|
||||
},
|
||||
#{ % PUT
|
||||
path => <<"/v1/admin/me">>,
|
||||
method => <<"PUT">>,
|
||||
#{ % PUT
|
||||
path => <<"/v1/admin/me">>,
|
||||
method => <<"PUT">>,
|
||||
description => <<"Update current admin profile">>,
|
||||
tags => [<<"Admins">>],
|
||||
tags => [<<"Admins">>],
|
||||
requestBody => #{
|
||||
required => true,
|
||||
content => #{<<"application/json">> => #{schema => admin_update_schema()}}
|
||||
content => #{<<"application/json">> => #{schema => admin_update_schema()}}
|
||||
},
|
||||
responses => #{
|
||||
200 => #{description => <<"Updated profile">>}
|
||||
@@ -55,28 +54,28 @@ trails() ->
|
||||
-spec admin_schema() -> map().
|
||||
admin_schema() ->
|
||||
#{
|
||||
type => object,
|
||||
type => object,
|
||||
properties => #{
|
||||
id => #{type => string},
|
||||
email => #{type => string},
|
||||
role => #{type => string},
|
||||
status => #{type => string},
|
||||
nickname => #{type => string, nullable => true},
|
||||
avatar_url => #{type => string, nullable => true},
|
||||
timezone => #{type => string, nullable => true},
|
||||
language => #{type => string, nullable => true},
|
||||
phone => #{type => string, nullable => true},
|
||||
preferences => #{type => object, nullable => true},
|
||||
last_login => #{type => string, format => <<"date-time">>},
|
||||
created_at => #{type => string, format => <<"date-time">>},
|
||||
updated_at => #{type => string, format => <<"date-time">>}
|
||||
id => #{type => string},
|
||||
email => #{type => string},
|
||||
role => #{type => string},
|
||||
status => #{type => string},
|
||||
nickname => #{type => string, nullable => true},
|
||||
avatar_url => #{type => string, nullable => true},
|
||||
timezone => #{type => string, nullable => true},
|
||||
language => #{type => string, nullable => true},
|
||||
phone => #{type => string, nullable => true},
|
||||
preferences => #{type => object, nullable => true},
|
||||
last_login => #{type => string, format => <<"date-time">>},
|
||||
created_at => #{type => string, format => <<"date-time">>},
|
||||
updated_at => #{type => string, format => <<"date-time">>}
|
||||
}
|
||||
}.
|
||||
|
||||
-spec admin_update_schema() -> map().
|
||||
admin_update_schema() ->
|
||||
#{
|
||||
type => object,
|
||||
type => object,
|
||||
properties => #{
|
||||
nickname => #{type => string},
|
||||
avatar_url => #{type => string},
|
||||
@@ -95,7 +94,7 @@ get_me(Req) ->
|
||||
{ok, AdminId, Req1} ->
|
||||
case logic_admin:get_admin(AdminId) of
|
||||
{ok, Admin} ->
|
||||
handler_utils:send_json(Req1, 200, admin_to_json(Admin));
|
||||
handler_utils:send_json(Req1, 200, handler_utils:admin_to_json(Admin));
|
||||
{error, not_found} ->
|
||||
handler_utils:send_error(Req1, 404, <<"Admin not found">>);
|
||||
{error, _} ->
|
||||
@@ -116,7 +115,9 @@ update_me(Req) ->
|
||||
Updates = convert_admin_fields(Updates0),
|
||||
case logic_admin:update_admin(AdminId, Updates) of
|
||||
{ok, Admin} ->
|
||||
handler_utils:send_json(Req2, 200, admin_to_json(Admin));
|
||||
% Аудит обновления профиля
|
||||
admin_utils:log_admin_action(AdminId, <<"update_me">>, <<"admin">>, AdminId, Req2),
|
||||
handler_utils:send_json(Req2, 200, handler_utils:admin_to_json(Admin));
|
||||
{error, not_found} ->
|
||||
handler_utils:send_error(Req2, 404, <<"Admin not found">>);
|
||||
{error, _} ->
|
||||
@@ -142,23 +143,4 @@ convert_admin_fields(Updates) ->
|
||||
({<<"phone">>, V}) -> {phone, V};
|
||||
({<<"preferences">>, V}) -> {preferences, V};
|
||||
(Other) -> Other
|
||||
end, Updates).
|
||||
|
||||
%% @private Формирует JSON-представление администратора.
|
||||
-spec admin_to_json(#admin{}) -> map().
|
||||
admin_to_json(Admin) ->
|
||||
#{
|
||||
id => Admin#admin.id,
|
||||
email => Admin#admin.email,
|
||||
role => Admin#admin.role,
|
||||
status => Admin#admin.status,
|
||||
nickname => Admin#admin.nickname,
|
||||
avatar_url => Admin#admin.avatar_url,
|
||||
timezone => Admin#admin.timezone,
|
||||
language => Admin#admin.language,
|
||||
phone => Admin#admin.phone,
|
||||
preferences => Admin#admin.preferences,
|
||||
last_login => handler_utils:datetime_to_iso8601(Admin#admin.last_login),
|
||||
created_at => handler_utils:datetime_to_iso8601(Admin#admin.created_at),
|
||||
updated_at => handler_utils:datetime_to_iso8601(Admin#admin.updated_at)
|
||||
}.
|
||||
end, Updates).
|
||||
Reference in New Issue
Block a user