Stage 3.3
This commit is contained in:
@@ -0,0 +1,141 @@
|
||||
-module(handler_event_occurrences).
|
||||
-include("records.hrl").
|
||||
|
||||
-export([init/2]).
|
||||
|
||||
init(Req, Opts) ->
|
||||
handle(Req, Opts).
|
||||
|
||||
handle(Req, _Opts) ->
|
||||
case cowboy_req:method(Req) of
|
||||
<<"GET">> -> get_occurrences(Req);
|
||||
<<"DELETE">> -> cancel_occurrence(Req);
|
||||
_ -> send_error(Req, 405, <<"Method not allowed">>)
|
||||
end.
|
||||
|
||||
%% GET /v1/events/:id/occurrences - получение вхождений повторяющегося события
|
||||
get_occurrences(Req) ->
|
||||
case handler_auth:authenticate(Req) of
|
||||
{ok, UserId, Req1} ->
|
||||
EventId = cowboy_req:binding(id, Req1),
|
||||
Qs = cowboy_req:parse_qs(Req1),
|
||||
|
||||
% Параметры диапазона
|
||||
From = proplists:get_value(<<"from">>, Qs, undefined),
|
||||
To = proplists:get_value(<<"to">>, Qs, undefined),
|
||||
|
||||
case {From, To} of
|
||||
{undefined, _} ->
|
||||
send_error(Req1, 400, <<"Missing 'from' parameter">>);
|
||||
{_, undefined} ->
|
||||
send_error(Req1, 400, <<"Missing 'to' parameter">>);
|
||||
{FromStr, ToStr} ->
|
||||
case {parse_datetime(FromStr), parse_datetime(ToStr)} of
|
||||
{{ok, FromDt}, {ok, ToDt}} ->
|
||||
case logic_event:get_occurrences(UserId, EventId, ToDt) of
|
||||
{ok, Occurrences} ->
|
||||
% Фильтруем по from
|
||||
Filtered = filter_from(Occurrences, FromDt),
|
||||
Response = occurrences_to_json(Filtered),
|
||||
send_json(Req1, 200, Response);
|
||||
{error, not_recurring} ->
|
||||
send_error(Req1, 400, <<"Event is not recurring">>);
|
||||
{error, access_denied} ->
|
||||
send_error(Req1, 403, <<"Access denied">>);
|
||||
{error, not_found} ->
|
||||
send_error(Req1, 404, <<"Event not found">>);
|
||||
{error, _} ->
|
||||
send_error(Req1, 500, <<"Internal server error">>)
|
||||
end;
|
||||
_ ->
|
||||
send_error(Req1, 400, <<"Invalid date format. Use ISO 8601">>)
|
||||
end
|
||||
end;
|
||||
{error, Code, Message, Req1} ->
|
||||
send_error(Req1, Code, Message)
|
||||
end.
|
||||
|
||||
%% DELETE /v1/events/:id/occurrences/:start_time - отмена вхождения
|
||||
cancel_occurrence(Req) ->
|
||||
case handler_auth:authenticate(Req) of
|
||||
{ok, UserId, Req1} ->
|
||||
EventId = cowboy_req:binding(id, Req1),
|
||||
StartTimeStr = cowboy_req:binding(start_time, Req1),
|
||||
|
||||
case parse_datetime(StartTimeStr) of
|
||||
{ok, StartTime} ->
|
||||
case logic_event:cancel_occurrence(UserId, EventId, StartTime) of
|
||||
{ok, cancelled} ->
|
||||
send_json(Req1, 200, #{status => <<"cancelled">>});
|
||||
{error, not_recurring} ->
|
||||
send_error(Req1, 400, <<"Event is not recurring">>);
|
||||
{error, access_denied} ->
|
||||
send_error(Req1, 403, <<"Access denied">>);
|
||||
{error, not_found} ->
|
||||
send_error(Req1, 404, <<"Event not found">>);
|
||||
{error, _} ->
|
||||
send_error(Req1, 500, <<"Internal server error">>)
|
||||
end;
|
||||
{error, _} ->
|
||||
send_error(Req1, 400, <<"Invalid start_time format">>)
|
||||
end;
|
||||
{error, Code, Message, Req1} ->
|
||||
send_error(Req1, Code, Message)
|
||||
end.
|
||||
|
||||
%% Вспомогательные функции
|
||||
parse_datetime(Str) ->
|
||||
try
|
||||
[DateStr, TimeStr] = string:split(Str, "T"),
|
||||
TimeStrNoZ = string:trim(TimeStr, trailing, "Z"),
|
||||
|
||||
[YearStr, MonthStr, DayStr] = string:split(DateStr, "-", all),
|
||||
[HourStr, MinuteStr, SecondStr] = string:split(TimeStrNoZ, ":", all),
|
||||
|
||||
Year = binary_to_integer(YearStr),
|
||||
Month = binary_to_integer(MonthStr),
|
||||
Day = binary_to_integer(DayStr),
|
||||
Hour = binary_to_integer(HourStr),
|
||||
Minute = binary_to_integer(MinuteStr),
|
||||
Second = binary_to_integer(SecondStr),
|
||||
|
||||
{ok, {{Year, Month, Day}, {Hour, Minute, Second}}}
|
||||
catch
|
||||
_:_ -> {error, invalid_format}
|
||||
end.
|
||||
|
||||
filter_from(Occurrences, From) ->
|
||||
lists:filter(fun
|
||||
({virtual, Occ}) -> Occ >= From;
|
||||
({materialized, Event}) -> Event#event.start_time >= From
|
||||
end, Occurrences).
|
||||
|
||||
occurrences_to_json(Occurrences) ->
|
||||
lists:map(fun occurrence_to_json/1, Occurrences).
|
||||
|
||||
occurrence_to_json({virtual, Occ}) ->
|
||||
#{
|
||||
start_time => datetime_to_iso8601(Occ),
|
||||
is_virtual => true
|
||||
};
|
||||
occurrence_to_json({materialized, Event}) ->
|
||||
#{
|
||||
id => Event#event.id,
|
||||
start_time => datetime_to_iso8601(Event#event.start_time),
|
||||
duration => Event#event.duration,
|
||||
specialist_id => Event#event.specialist_id,
|
||||
is_virtual => false,
|
||||
status => Event#event.status
|
||||
}.
|
||||
|
||||
datetime_to_iso8601({{Year, Month, Day}, {Hour, Minute, Second}}) ->
|
||||
iolist_to_binary(io_lib:format("~4..0B-~2..0B-~2..0BT~2..0B:~2..0B:~2..0BZ",
|
||||
[Year, Month, Day, Hour, Minute, Second])).
|
||||
|
||||
send_json(Req, Status, Data) ->
|
||||
Body = jsx:encode(Data),
|
||||
cowboy_req:reply(Status, #{<<"content-type">> => <<"application/json">>}, Body, Req).
|
||||
|
||||
send_error(Req, Status, Message) ->
|
||||
Body = jsx:encode(#{error => Message}),
|
||||
cowboy_req:reply(Status, #{<<"content-type">> => <<"application/json">>}, Body, Req).
|
||||
Reference in New Issue
Block a user