Рефакторинг админских обработчиков - пагинация, сортировка, фильтрация. Финал

This commit is contained in:
2026-05-26 21:50:12 +03:00
parent ed5b275efb
commit 19b3d0dba0
21 changed files with 1833 additions and 421 deletions
+24 -4
View File
@@ -32,6 +32,12 @@ trails() ->
description => <<"Filter by target type">>},
#{name => <<"q">>, in => <<"query">>, schema => #{type => string},
description => <<"Search in reason">>},
#{name => <<"sort">>, in => <<"query">>,
schema => #{type => string, enum => [<<"created_at">>, <<"status">>]},
description => <<"Sort field">>},
#{name => <<"order">>, in => <<"query">>,
schema => #{type => string, enum => [<<"asc">>, <<"desc">>]},
description => <<"Sort direction">>},
#{name => <<"limit">>, in => <<"query">>, schema => #{type => integer},
description => <<"Page size">>},
#{name => <<"offset">>, in => <<"query">>, schema => #{type => integer},
@@ -69,7 +75,11 @@ list_reports(Req) ->
case handler_utils:auth_admin(Req) of
{ok, AdminId, Req1} ->
Filters = parse_report_filters(Req1),
Pagination = handler_utils:parse_pagination_params(Req1),
Pagination0 = handler_utils:parse_pagination_params(Req1),
Pagination = Pagination0#{
sort => maps:get(sort, Pagination0, <<"created_at">>),
order => maps:get(order, Pagination0, <<"desc">>)
},
case logic_report:list_reports(AdminId) of
{ok, AllReports} ->
Filtered = apply_report_filters(AllReports, Filters),
@@ -89,9 +99,19 @@ list_reports(Req) ->
parse_report_filters(Req) ->
Qs = cowboy_req:parse_qs(Req),
#{ status => proplists:get_value(<<"status">>, Qs),
target_type => proplists:get_value(<<"target_type">>, Qs),
q => proplists:get_value(<<"q">>, Qs) }.
StatusBin = proplists:get_value(<<"status">>, Qs),
TargetBin = proplists:get_value(<<"target_type">>, Qs),
#{
status => convert_to_atom(StatusBin),
target_type => convert_to_atom(TargetBin),
q => proplists:get_value(<<"q">>, Qs)
}.
convert_to_atom(undefined) -> undefined;
convert_to_atom(Bin) when is_binary(Bin) ->
try binary_to_existing_atom(Bin, utf8)
catch error:badarg -> Bin
end.
apply_report_filters(Reports, Filters) ->
Status = maps:get(status, Filters, undefined),