Рефакторинг обработчиков. Часть 2 #21
This commit is contained in:
@@ -1,106 +1,143 @@
|
||||
%%%-------------------------------------------------------------------
|
||||
%%% @doc Обработчик полнотекстового поиска (клиентский API).
|
||||
%%%
|
||||
%%% GET – выполняет поиск по календарям и событиям.
|
||||
%%% @end
|
||||
%%%-------------------------------------------------------------------
|
||||
-module(handler_search).
|
||||
-include("records.hrl").
|
||||
-behaviour(cowboy_handler).
|
||||
|
||||
-export([init/2]).
|
||||
-export([trails/0]).
|
||||
|
||||
-include("records.hrl").
|
||||
|
||||
%%% cowboy_handler callback
|
||||
-spec init(cowboy_req:req(), any()) -> {ok, cowboy_req:req(), any()}.
|
||||
init(Req, Opts) ->
|
||||
handle(Req, Opts).
|
||||
|
||||
%%% Swagger metadata
|
||||
-spec trails() -> [map()].
|
||||
trails() ->
|
||||
[
|
||||
#{
|
||||
path => <<"/v1/search">>,
|
||||
method => <<"GET">>,
|
||||
description => <<"Search calendars and events">>,
|
||||
tags => [<<"Search">>],
|
||||
parameters => [
|
||||
#{name => <<"type">>, in => <<"query">>, schema => #{type => string, enum => [<<"calendar">>, <<"event">>]}, description => <<"Type of entities to search">>},
|
||||
#{name => <<"q">>, in => <<"query">>, schema => #{type => string}, description => <<"Search query">>},
|
||||
#{name => <<"limit">>, in => <<"query">>, schema => #{type => integer}, description => <<"Maximum results per page">>},
|
||||
#{name => <<"offset">>, in => <<"query">>, schema => #{type => integer}, description => <<"Offset for pagination">>},
|
||||
#{name => <<"tags">>, in => <<"query">>, schema => #{type => string}, description => <<"Comma-separated tags">>},
|
||||
#{name => <<"sort">>, in => <<"query">>, schema => #{type => string, enum => [<<"start_time">>, <<"created_at">>, <<"title">>]}, description => <<"Field to sort by">>},
|
||||
#{name => <<"order">>, in => <<"query">>, schema => #{type => string, enum => [<<"asc">>, <<"desc">>]}, description => <<"Sort order">>},
|
||||
#{name => <<"lat">>, in => <<"query">>, schema => #{type => number, format => float}, description => <<"Latitude for geo search">>},
|
||||
#{name => <<"lon">>, in => <<"query">>, schema => #{type => number, format => float}, description => <<"Longitude for geo search">>},
|
||||
#{name => <<"radius">>, in => <<"query">>, schema => #{type => integer}, description => <<"Radius in km for geo search">>},
|
||||
#{name => <<"from">>, in => <<"query">>, schema => #{type => string, format => <<"date-time">>}, description => <<"Start datetime (ISO8601)">>},
|
||||
#{name => <<"to">>, in => <<"query">>, schema => #{type => string, format => <<"date-time">>}, description => <<"End datetime (ISO8601)">>}
|
||||
],
|
||||
responses => #{
|
||||
200 => #{
|
||||
description => <<"Search results with pagination">>,
|
||||
content => #{<<"application/json">> => #{schema => #{
|
||||
type => object,
|
||||
properties => #{
|
||||
total => #{type => integer},
|
||||
limit => #{type => integer},
|
||||
offset => #{type => integer},
|
||||
results => #{type => array, items => #{type => object}}
|
||||
}
|
||||
}}}
|
||||
},
|
||||
400 => #{description => <<"Invalid parameters">>},
|
||||
500 => #{description => <<"Search failed">>}
|
||||
}
|
||||
}
|
||||
].
|
||||
|
||||
%%%===================================================================
|
||||
%%% HTTP-методы
|
||||
%%%===================================================================
|
||||
|
||||
%% @private
|
||||
-spec handle(cowboy_req:req(), any()) -> {ok, cowboy_req:req(), any()}.
|
||||
handle(Req, _Opts) ->
|
||||
case cowboy_req:method(Req) of
|
||||
<<"GET">> -> search(Req);
|
||||
_ -> send_error(Req, 405, <<"Method not allowed">>)
|
||||
<<"GET">> -> search(Req);
|
||||
_ -> handler_utils:send_error(Req, 405, <<"Method not allowed">>)
|
||||
end.
|
||||
|
||||
%% @doc GET /v1/search — полнотекстовый поиск с фильтрами.
|
||||
-spec search(cowboy_req:req()) -> {ok, binary(), cowboy_req:req()}.
|
||||
search(Req) ->
|
||||
case handler_auth:authenticate(Req) of
|
||||
case handler_utils:auth_user(Req) of
|
||||
{ok, UserId, Req1} ->
|
||||
Qs = cowboy_req:parse_qs(Req1),
|
||||
|
||||
Type = proplists:get_value(<<"type">>, Qs, undefined),
|
||||
Type = proplists:get_value(<<"type">>, Qs, undefined),
|
||||
Query = proplists:get_value(<<"q">>, Qs, undefined),
|
||||
|
||||
Params = parse_params(Qs),
|
||||
|
||||
case logic_search:search(Type, Query, UserId, Params) of
|
||||
{ok, Total, Results} ->
|
||||
Response = #{
|
||||
total => Total,
|
||||
limit => maps:get(limit, Params, 20),
|
||||
offset => maps:get(offset, Params, 0),
|
||||
total => Total,
|
||||
limit => maps:get(limit, Params, 20),
|
||||
offset => maps:get(offset, Params, 0),
|
||||
results => Results
|
||||
},
|
||||
send_json(Req1, 200, Response);
|
||||
handler_utils:send_json(Req1, 200, Response);
|
||||
{error, _} ->
|
||||
send_error(Req1, 500, <<"Search failed">>)
|
||||
handler_utils:send_error(Req1, 500, <<"Search failed">>)
|
||||
end;
|
||||
{error, Code, Message, Req1} ->
|
||||
send_error(Req1, Code, Message)
|
||||
handler_utils:send_error(Req1, Code, Message)
|
||||
end.
|
||||
|
||||
%%%===================================================================
|
||||
%%% Внутренние функции
|
||||
%%%===================================================================
|
||||
|
||||
%% @private Собирает карту параметров для поискового движка.
|
||||
-spec parse_params(cowboy_req:qs()) -> map().
|
||||
parse_params(Qs) ->
|
||||
Params = #{
|
||||
limit => parse_int_param(Qs, <<"limit">>, 20),
|
||||
limit => parse_int_param(Qs, <<"limit">>, 20),
|
||||
offset => parse_int_param(Qs, <<"offset">>, 0),
|
||||
tags => proplists:get_value(<<"tags">>, Qs),
|
||||
sort => proplists:get_value(<<"sort">>, Qs, <<"start_time">>),
|
||||
order => proplists:get_value(<<"order">>, Qs, <<"asc">>)
|
||||
tags => proplists:get_value(<<"tags">>, Qs),
|
||||
sort => proplists:get_value(<<"sort">>, Qs, <<"start_time">>),
|
||||
order => proplists:get_value(<<"order">>, Qs, <<"asc">>)
|
||||
},
|
||||
|
||||
Params1 = case {parse_float_param(Qs, <<"lat">>), parse_float_param(Qs, <<"lon">>)} of
|
||||
{{ok, Lat}, {ok, Lon}} ->
|
||||
Radius = parse_int_param(Qs, <<"radius">>, 10),
|
||||
Params#{lat => Lat, lon => Lon, radius => Radius};
|
||||
_ -> Params
|
||||
end,
|
||||
|
||||
Params2 = case {parse_datetime_param(Qs, <<"from">>), parse_datetime_param(Qs, <<"to">>)} of
|
||||
{{ok, From}, {ok, To}} ->
|
||||
Params1#{from => From, to => To};
|
||||
{{ok, From}, error} ->
|
||||
Params1#{from => From};
|
||||
{error, {ok, To}} ->
|
||||
Params1#{to => To};
|
||||
_ -> Params1
|
||||
{{ok, From}, {ok, To}} -> Params1#{from => From, to => To};
|
||||
{{ok, From}, error} -> Params1#{from => From};
|
||||
{error, {ok, To}} -> Params1#{to => To};
|
||||
_ -> Params1
|
||||
end,
|
||||
|
||||
Params2.
|
||||
|
||||
-spec parse_int_param(cowboy_req:qs(), binary(), integer()) -> integer().
|
||||
parse_int_param(Qs, Key, Default) ->
|
||||
case proplists:get_value(Key, Qs) of
|
||||
undefined -> Default;
|
||||
Val -> binary_to_integer(Val)
|
||||
end.
|
||||
handler_utils:parse_int_qs(proplists:get_value(Key, Qs), Default).
|
||||
|
||||
-spec parse_float_param(cowboy_req:qs(), binary()) -> {ok, float()} | error.
|
||||
parse_float_param(Qs, Key) ->
|
||||
case proplists:get_value(Key, Qs) of
|
||||
undefined -> error;
|
||||
Val -> {ok, binary_to_float(Val)}
|
||||
end.
|
||||
|
||||
-spec parse_datetime_param(cowboy_req:qs(), binary()) -> {ok, calendar:datetime()} | error.
|
||||
parse_datetime_param(Qs, Key) ->
|
||||
case proplists:get_value(Key, Qs) of
|
||||
undefined -> error;
|
||||
Val ->
|
||||
try
|
||||
[DateStr, TimeStr] = string:split(Val, "T"),
|
||||
TimeStrNoZ = string:trim(TimeStr, trailing, "Z"),
|
||||
|
||||
[Y, M, D] = [binary_to_integer(X) || X <- string:split(DateStr, "-", all)],
|
||||
[H, Min, S] = [binary_to_integer(X) || X <- string:split(TimeStrNoZ, ":", all)],
|
||||
|
||||
{ok, {{Y, M, D}, {H, Min, S}}}
|
||||
catch
|
||||
_:_ -> error
|
||||
end
|
||||
end.
|
||||
|
||||
send_json(Req, Status, Data) ->
|
||||
Body = jsx:encode(Data),
|
||||
cowboy_req:reply(Status, #{<<"content-type">> => <<"application/json">>}, Body, Req),
|
||||
{ok, Body, []}.
|
||||
|
||||
send_error(Req, Status, Message) ->
|
||||
Body = jsx:encode(#{error => Message}),
|
||||
cowboy_req:reply(Status, #{<<"content-type">> => <<"application/json">>}, Body, Req),
|
||||
{ok, Body, []}.
|
||||
Val -> handler_utils:parse_datetime(Val)
|
||||
end.
|
||||
Reference in New Issue
Block a user