Рефакторинг админских обработчиков - пагинация, сортировка, фильтрация. Часть 1
This commit is contained in:
@@ -6,7 +6,6 @@
|
||||
%%%-------------------------------------------------------------------
|
||||
-module(admin_handler_report_by_id).
|
||||
-behaviour(cowboy_handler).
|
||||
|
||||
-export([init/2]).
|
||||
-export([trails/0]).
|
||||
|
||||
@@ -17,44 +16,35 @@ init(Req, _Opts) ->
|
||||
case cowboy_req:method(Req) of
|
||||
<<"GET">> -> get_report(Req);
|
||||
<<"PUT">> -> update_report(Req);
|
||||
_ -> handler_utils:send_error(Req, 405, <<"Method not allowed">>)
|
||||
_ -> handler_utils:send_error(Req, 405, <<"Method not allowed">>)
|
||||
end.
|
||||
|
||||
-spec trails() -> [map()].
|
||||
trails() ->
|
||||
BaseParams = [
|
||||
#{
|
||||
name => <<"id">>,
|
||||
in => <<"path">>,
|
||||
description => <<"Report ID">>,
|
||||
required => true,
|
||||
schema => #{type => string}
|
||||
}
|
||||
#{ name => <<"id">>, in => <<"path">>, description => <<"Report ID">>, required => true, schema => #{type => string} }
|
||||
],
|
||||
[
|
||||
#{ % GET
|
||||
path => <<"/v1/admin/reports/:id">>,
|
||||
method => <<"GET">>,
|
||||
#{ % GET
|
||||
path => <<"/v1/admin/reports/:id">>,
|
||||
method => <<"GET">>,
|
||||
description => <<"Get report by ID (admin)">>,
|
||||
tags => [<<"Reports">>],
|
||||
parameters => BaseParams,
|
||||
responses => #{
|
||||
200 => #{
|
||||
description => <<"Report details">>,
|
||||
content => #{<<"application/json">> => #{schema => report_schema()}}
|
||||
},
|
||||
tags => [<<"Reports">>],
|
||||
parameters => BaseParams,
|
||||
responses => #{
|
||||
200 => #{ description => <<"Report details">>, content => #{<<"application/json">> => #{schema => report_schema()}} },
|
||||
404 => #{description => <<"Report not found">>}
|
||||
}
|
||||
},
|
||||
#{ % PUT
|
||||
path => <<"/v1/admin/reports/:id">>,
|
||||
method => <<"PUT">>,
|
||||
#{ % PUT
|
||||
path => <<"/v1/admin/reports/:id">>,
|
||||
method => <<"PUT">>,
|
||||
description => <<"Update report status (admin)">>,
|
||||
tags => [<<"Reports">>],
|
||||
parameters => BaseParams,
|
||||
tags => [<<"Reports">>],
|
||||
parameters => BaseParams,
|
||||
requestBody => #{
|
||||
required => true,
|
||||
content => #{<<"application/json">> => #{schema => report_update_schema()}}
|
||||
content => #{<<"application/json">> => #{schema => report_update_schema()}}
|
||||
},
|
||||
responses => #{
|
||||
200 => #{description => <<"Updated report">>},
|
||||
@@ -65,15 +55,15 @@ trails() ->
|
||||
|
||||
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}
|
||||
}
|
||||
@@ -81,7 +71,7 @@ report_schema() ->
|
||||
|
||||
report_update_schema() ->
|
||||
#{
|
||||
type => object,
|
||||
type => object,
|
||||
properties => #{
|
||||
status => #{type => string, enum => [<<"reviewed">>, <<"dismissed">>]}
|
||||
}
|
||||
@@ -114,6 +104,8 @@ update_report(Req) ->
|
||||
#{<<"status">> := Status} ->
|
||||
case logic_report:update_report_status(AdminId, ReportId, Status) of
|
||||
{ok, Report} ->
|
||||
% Аудит изменения статуса жалобы
|
||||
admin_utils:log_admin_action(AdminId, <<"update_report_status">>, <<"report">>, ReportId, Req2),
|
||||
handler_utils:send_json(Req2, 200, handler_utils:report_to_json(Report));
|
||||
{error, not_found} ->
|
||||
handler_utils:send_error(Req2, 404, <<"Report not found">>);
|
||||
|
||||
Reference in New Issue
Block a user