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

This commit is contained in:
2026-05-26 13:04:31 +03:00
parent 01dbc6d6cb
commit ed5b275efb
24 changed files with 1009 additions and 1126 deletions
+40 -47
View File
@@ -5,7 +5,6 @@
%%%-------------------------------------------------------------------
-module(admin_handler_reports).
-behaviour(cowboy_handler).
-export([init/2]).
-export([trails/0]).
@@ -15,47 +14,49 @@
init(Req, _Opts) ->
case cowboy_req:method(Req) of
<<"GET">> -> list_reports(Req);
_ -> handler_utils:send_error(Req, 405, <<"Method not allowed">>)
_ -> handler_utils:send_error(Req, 405, <<"Method not allowed">>)
end.
-spec trails() -> [map()].
trails() ->
[
#{
path => <<"/v1/admin/reports">>,
method => <<"GET">>,
description => <<"List all reports (admin)">>,
tags => [<<"Reports">>],
parameters => [
#{name => <<"status">>, in => <<"query">>, schema => #{type => string, enum => [<<"pending">>, <<"reviewed">>, <<"dismissed">>]}, description => <<"Filter by status">>},
#{name => <<"target_type">>, in => <<"query">>, schema => #{type => string, enum => [<<"calendar">>, <<"event">>, <<"review">>]}, description => <<"Filter by target type">>},
#{name => <<"q">>, in => <<"query">>, schema => #{type => string}, description => <<"Search in reason">>},
#{name => <<"limit">>, in => <<"query">>, schema => #{type => integer}, description => <<"Page size">>},
#{name => <<"offset">>, in => <<"query">>, schema => #{type => integer}, description => <<"Offset">>}
],
responses => #{
200 => #{
description => <<"Array of reports">>,
content => #{<<"application/json">> => #{schema => #{
type => array,
items => report_schema()
}}}
}
}
[ #{ path => <<"/v1/admin/reports">>,
method => <<"GET">>,
description => <<"List all reports (admin)">>,
tags => [<<"Reports">>],
parameters => [
#{name => <<"status">>, in => <<"query">>,
schema => #{type => string, enum => [<<"pending">>, <<"reviewed">>, <<"dismissed">>]},
description => <<"Filter by status">>},
#{name => <<"target_type">>, in => <<"query">>,
schema => #{type => string, enum => [<<"calendar">>, <<"event">>, <<"review">>]},
description => <<"Filter by target type">>},
#{name => <<"q">>, in => <<"query">>, schema => #{type => string},
description => <<"Search in reason">>},
#{name => <<"limit">>, in => <<"query">>, schema => #{type => integer},
description => <<"Page size">>},
#{name => <<"offset">>, in => <<"query">>, schema => #{type => integer},
description => <<"Offset">>}
],
responses => #{
200 => #{ description => <<"Array of reports">>,
content => #{<<"application/json">> => #{schema => #{
type => array,
items => report_schema()
}}} }
}
}
].
report_schema() ->
#{
type => object,
#{ type => object,
properties => #{
id => #{type => string},
id => #{type => string},
reporter_id => #{type => string},
target_type => #{type => string, enum => [<<"calendar">>, <<"event">>, <<"review">>]},
target_id => #{type => string},
reason => #{type => string},
status => #{type => string, enum => [<<"pending">>, <<"reviewed">>, <<"dismissed">>]},
created_at => #{type => string, format => <<"date-time">>},
target_id => #{type => string},
reason => #{type => string},
status => #{type => string, enum => [<<"pending">>, <<"reviewed">>, <<"dismissed">>]},
created_at => #{type => string, format => <<"date-time">>},
resolved_at => #{type => string, format => <<"date-time">>, nullable => true},
resolved_by => #{type => string, nullable => true}
}
@@ -74,9 +75,10 @@ list_reports(Req) ->
Filtered = apply_report_filters(AllReports, Filters),
Sorted = sort_reports(Filtered, Pagination),
Total = length(Sorted),
Page = lists:sublist(Sorted, maps:get(offset, Pagination) + 1, maps:get(limit, Pagination)),
Page = lists:sublist(Sorted, maps:get(offset, Pagination) + 1,
maps:get(limit, Pagination)),
Json = [handler_utils:report_to_json(R) || R <- Page],
ExtraHeaders = pagination_headers(Pagination, Total),
ExtraHeaders = handler_utils:pagination_headers(Pagination, Total),
handler_utils:send_json(Req1, 200, Json, ExtraHeaders);
{error, access_denied} ->
handler_utils:send_error(Req1, 403, <<"Admin access required">>)
@@ -87,11 +89,9 @@ list_reports(Req) ->
parse_report_filters(Req) ->
Qs = cowboy_req:parse_qs(Req),
#{
status => proplists:get_value(<<"status">>, Qs),
#{ status => proplists:get_value(<<"status">>, Qs),
target_type => proplists:get_value(<<"target_type">>, Qs),
q => proplists:get_value(<<"q">>, Qs)
}.
q => proplists:get_value(<<"q">>, Qs) }.
apply_report_filters(Reports, Filters) ->
Status = maps:get(status, Filters, undefined),
@@ -108,7 +108,8 @@ apply_report_filters(Reports, Filters) ->
case Q of
undefined -> F2;
_ -> [R || R <- F2,
string:str(binary_to_list(R#report.reason), binary_to_list(Q)) > 0]
string:str(binary_to_list(R#report.reason),
binary_to_list(Q)) > 0]
end.
sort_reports(Reports, #{sort := Sort, order := Order}) ->
@@ -124,12 +125,4 @@ sort_reports(Reports, #{sort := Sort, order := Order}) ->
report_field(#report{created_at = V}, created_at) -> V;
report_field(#report{status = V}, status) -> V;
report_field(_, _) -> undefined.
pagination_headers(#{limit := Limit, offset := Offset}, Total) ->
RangeEnd = min(Offset + Limit - 1, Total - 1),
#{
<<"content-range">> => iolist_to_binary(io_lib:format("items ~B-~B/~B", [Offset, RangeEnd, Total])),
<<"x-total-count">> => integer_to_binary(Total),
<<"access-control-expose-headers">> => <<"Content-Range, X-Total-Count">>
}.
report_field(_, _) -> undefined.