Рефакторинг обработчиков. Часть 3 #21
This commit is contained in:
@@ -88,7 +88,15 @@ review_schema() ->
|
||||
list_reviews(Req) ->
|
||||
case handler_utils:auth_admin(Req) of
|
||||
{ok, _AdminId, Req1} ->
|
||||
Filters = parse_review_filters(Req1),
|
||||
Qs = cowboy_req:parse_qs(Req1),
|
||||
Filters0 = #{
|
||||
target_type => normalize_target_type(proplists:get_value(<<"target_type">>, Qs)),
|
||||
target_id => proplists:get_value(<<"target_id">>, Qs),
|
||||
user_id => proplists:get_value(<<"user_id">>, Qs),
|
||||
status => proplists:get_value(<<"status">>, Qs)
|
||||
},
|
||||
% Убираем ключи со значением undefined
|
||||
Filters = maps:filter(fun(_, V) -> V =/= undefined end, Filters0),
|
||||
Pagination = handler_utils:parse_pagination_params(Req1),
|
||||
{ok, Total, Reviews} = logic_review:list_admin_reviews(Filters, Pagination),
|
||||
Json = [handler_utils:review_to_json(R) || R <- Reviews],
|
||||
@@ -98,6 +106,11 @@ list_reviews(Req) ->
|
||||
handler_utils:send_error(Req1, Code, Msg)
|
||||
end.
|
||||
|
||||
%% @private Преобразует бинарный target_type в атом.
|
||||
normalize_target_type(<<"event">>) -> event;
|
||||
normalize_target_type(<<"calendar">>) -> calendar;
|
||||
normalize_target_type(Other) -> Other.
|
||||
|
||||
bulk_update_reviews(Req) ->
|
||||
case handler_utils:auth_admin(Req) of
|
||||
{ok, _AdminId, Req1} ->
|
||||
@@ -118,15 +131,6 @@ bulk_update_reviews(Req) ->
|
||||
handler_utils:send_error(Req1, Code, Msg)
|
||||
end.
|
||||
|
||||
parse_review_filters(Req) ->
|
||||
Qs = cowboy_req:parse_qs(Req),
|
||||
#{
|
||||
target_type => proplists:get_value(<<"target_type">>, Qs),
|
||||
target_id => proplists:get_value(<<"target_id">>, Qs),
|
||||
user_id => proplists:get_value(<<"user_id">>, Qs),
|
||||
status => proplists:get_value(<<"status">>, Qs)
|
||||
}.
|
||||
|
||||
pagination_headers(#{limit := Limit, offset := Offset}, Total) ->
|
||||
RangeEnd = min(Offset + Limit - 1, Total - 1),
|
||||
#{
|
||||
|
||||
Reference in New Issue
Block a user