Stage 3.2 + tests

This commit is contained in:
2026-04-20 13:00:56 +03:00
parent 491b4ae179
commit 5291f70337
12 changed files with 1014 additions and 4 deletions

98
src/core/core_event.erl Normal file
View File

@@ -0,0 +1,98 @@
-module(core_event).
-include("records.hrl").
-export([create/4, get_by_id/1, list_by_calendar/1, update/2, delete/1]).
-export([generate_id/0]).
%% Создание события (одиночного)
create(CalendarId, Title, StartTime, Duration) ->
Id = generate_id(),
Event = #event{
id = Id,
calendar_id = CalendarId,
title = Title,
description = <<"">>,
event_type = single,
start_time = StartTime,
duration = Duration,
recurrence_rule = undefined,
master_id = undefined,
is_instance = false,
specialist_id = undefined,
location = undefined,
tags = [],
capacity = undefined,
online_link = undefined,
status = active,
rating_avg = 0.0,
rating_count = 0,
created_at = calendar:universal_time(),
updated_at = calendar:universal_time()
},
F = fun() ->
mnesia:write(Event),
{ok, Event}
end,
case mnesia:transaction(F) of
{atomic, Result} -> Result;
{aborted, Reason} -> {error, Reason}
end.
%% Получение события по ID
get_by_id(Id) ->
case mnesia:dirty_read(event, Id) of
[] -> {error, not_found};
[Event] -> {ok, Event}
end.
%% Список событий календаря
list_by_calendar(CalendarId) ->
Match = #event{calendar_id = CalendarId, status = active, is_instance = false, _ = '_'},
Events = mnesia:dirty_match_object(Match),
{ok, Events}.
%% Обновление события
update(Id, Updates) ->
F = fun() ->
case mnesia:read(event, Id) of
[] ->
{error, not_found};
[Event] ->
UpdatedEvent = apply_updates(Event, Updates),
mnesia:write(UpdatedEvent),
{ok, UpdatedEvent}
end
end,
case mnesia:transaction(F) of
{atomic, Result} -> Result;
{aborted, Reason} -> {error, Reason}
end.
%% Удаление события (soft delete)
delete(Id) ->
update(Id, [{status, deleted}]).
%% Внутренние функции
generate_id() ->
base64:encode(crypto:strong_rand_bytes(16), #{mode => urlsafe, padding => false}).
apply_updates(Event, Updates) ->
Updated = lists:foldl(fun({Field, Value}, E) ->
set_field(Field, Value, E)
end, Event, Updates),
Updated#event{updated_at = calendar:universal_time()}.
set_field(title, Value, E) -> E#event{title = Value};
set_field(description, Value, E) -> E#event{description = Value};
set_field(start_time, Value, E) -> E#event{start_time = Value};
set_field(duration, Value, E) -> E#event{duration = Value};
set_field(specialist_id, Value, E) -> E#event{specialist_id = Value};
set_field(location, Value, E) -> E#event{location = Value};
set_field(tags, Value, E) -> E#event{tags = Value};
set_field(capacity, Value, E) -> E#event{capacity = Value};
set_field(online_link, Value, E) -> E#event{online_link = Value};
set_field(status, Value, E) -> E#event{status = Value};
set_field(_, _, E) -> E.

View File

@@ -38,7 +38,9 @@ start_http() ->
{"/v1/refresh", handler_refresh, []},
{"/v1/user/me", handler_user_me, []},
{"/v1/calendars", handler_calendars, []},
{"/v1/calendars/:id", handler_calendar_by_id, []}
{"/v1/calendars/:id", handler_calendar_by_id, []},
{"/v1/calendars/:calendar_id/events", handler_events, []},
{"/v1/events/:id", handler_event_by_id, []}
]}
]),

View File

@@ -0,0 +1,158 @@
-module(handler_event_by_id).
-include("records.hrl").
-export([init/2]).
init(Req, Opts) ->
handle(Req, Opts).
handle(Req, _Opts) ->
case cowboy_req:method(Req) of
<<"GET">> -> get_event(Req);
<<"PUT">> -> update_event(Req);
<<"DELETE">> -> delete_event(Req);
_ -> send_error(Req, 405, <<"Method not allowed">>)
end.
%% GET /v1/events/:id - получение события
get_event(Req) ->
case handler_auth:authenticate(Req) of
{ok, UserId, Req1} ->
EventId = cowboy_req:binding(id, Req1),
case logic_event:get_event(UserId, EventId) of
{ok, Event} ->
Response = event_to_json(Event),
send_json(Req1, 200, Response);
{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, Code, Message, Req1} ->
send_error(Req1, Code, Message)
end.
%% PUT /v1/events/:id - обновление события
update_event(Req) ->
case handler_auth:authenticate(Req) of
{ok, UserId, Req1} ->
EventId = cowboy_req:binding(id, Req1),
{ok, Body, Req2} = cowboy_req:read_body(Req1),
try jsx:decode(Body, [return_maps]) of
UpdatesMap when is_map(UpdatesMap) ->
Updates = maps:to_list(UpdatesMap),
% Преобразуем строку времени в datetime если есть
UpdatesWithTime = convert_time_field(Updates),
case logic_event:update_event(UserId, EventId, UpdatesWithTime) of
{ok, Event} ->
Response = event_to_json(Event),
send_json(Req2, 200, Response);
{error, access_denied} ->
send_error(Req2, 403, <<"Access denied">>);
{error, not_found} ->
send_error(Req2, 404, <<"Event not found">>);
{error, event_in_past} ->
send_error(Req2, 400, <<"Event cannot be in the past">>);
{error, _} ->
send_error(Req2, 500, <<"Internal server error">>)
end;
_ ->
send_error(Req2, 400, <<"Invalid JSON">>)
catch
_:_ ->
send_error(Req2, 400, <<"Invalid JSON format">>)
end;
{error, Code, Message, Req1} ->
send_error(Req1, Code, Message)
end.
%% DELETE /v1/events/:id - удаление события
delete_event(Req) ->
case handler_auth:authenticate(Req) of
{ok, UserId, Req1} ->
EventId = cowboy_req:binding(id, Req1),
case logic_event:delete_event(UserId, EventId) of
{ok, _} ->
send_json(Req1, 200, #{status => <<"deleted">>});
{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, Code, Message, Req1} ->
send_error(Req1, Code, Message)
end.
%% Вспомогательные функции
convert_time_field(Updates) ->
lists:map(fun
({start_time, Value}) when is_binary(Value) ->
case parse_datetime(Value) of
{ok, DateTime} -> {start_time, DateTime};
_ -> {start_time, Value}
end;
(Other) -> Other
end, Updates).
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.
event_to_json(Event) ->
LocationJson = case Event#event.location of
undefined -> null;
#location{address = Addr, lat = Lat, lon = Lon} ->
#{address => Addr, lat => Lat, lon => Lon}
end,
#{
id => Event#event.id,
calendar_id => Event#event.calendar_id,
title => Event#event.title,
description => Event#event.description,
event_type => Event#event.event_type,
start_time => datetime_to_iso8601(Event#event.start_time),
duration => Event#event.duration,
specialist_id => Event#event.specialist_id,
location => LocationJson,
tags => Event#event.tags,
capacity => Event#event.capacity,
online_link => Event#event.online_link,
status => Event#event.status,
rating_avg => Event#event.rating_avg,
rating_count => Event#event.rating_count,
created_at => datetime_to_iso8601(Event#event.created_at),
updated_at => datetime_to_iso8601(Event#event.updated_at)
}.
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).

View File

@@ -0,0 +1,138 @@
-module(handler_events).
-include("records.hrl").
-export([init/2]).
init(Req, Opts) ->
handle(Req, Opts).
handle(Req, _Opts) ->
case cowboy_req:method(Req) of
<<"POST">> -> create_event(Req);
<<"GET">> -> list_events(Req);
_ -> send_error(Req, 405, <<"Method not allowed">>)
end.
%% POST /v1/calendars/:calendar_id/events - создание события
create_event(Req) ->
case handler_auth:authenticate(Req) of
{ok, UserId, Req1} ->
CalendarId = cowboy_req:binding(calendar_id, Req1),
{ok, Body, Req2} = cowboy_req:read_body(Req1),
try jsx:decode(Body, [return_maps]) of
Decoded when is_map(Decoded) ->
case Decoded of
#{<<"title">> := Title,
<<"start_time">> := StartTimeStr,
<<"duration">> := Duration} ->
case parse_datetime(StartTimeStr) of
{ok, StartTime} ->
case logic_event:create_event(UserId, CalendarId, Title, StartTime, Duration) of
{ok, Event} ->
Response = event_to_json(Event),
send_json(Req2, 201, Response);
{error, access_denied} ->
send_error(Req2, 403, <<"Access denied">>);
{error, not_found} ->
send_error(Req2, 404, <<"Calendar not found">>);
{error, event_in_past} ->
send_error(Req2, 400, <<"Event cannot be in the past">>);
{error, _} ->
send_error(Req2, 500, <<"Internal server error">>)
end;
{error, _} ->
send_error(Req2, 400, <<"Invalid start_time format. Use ISO 8601">>)
end;
_ ->
send_error(Req2, 400, <<"Missing required fields: title, start_time, duration">>)
end;
_ ->
send_error(Req2, 400, <<"Invalid JSON">>)
catch
_:_ ->
send_error(Req2, 400, <<"Invalid JSON format">>)
end;
{error, Code, Message, Req1} ->
send_error(Req1, Code, Message)
end.
%% GET /v1/calendars/:calendar_id/events - список событий
list_events(Req) ->
case handler_auth:authenticate(Req) of
{ok, UserId, Req1} ->
CalendarId = cowboy_req:binding(calendar_id, Req1),
case logic_event:list_events(UserId, CalendarId) of
{ok, Events} ->
Response = [event_to_json(E) || E <- Events],
send_json(Req1, 200, Response);
{error, access_denied} ->
send_error(Req1, 403, <<"Access denied">>);
{error, not_found} ->
send_error(Req1, 404, <<"Calendar not found">>);
{error, _} ->
send_error(Req1, 500, <<"Internal server error">>)
end;
{error, Code, Message, Req1} ->
send_error(Req1, Code, Message)
end.
%% Вспомогательные функции
parse_datetime(Str) ->
try
% Ожидаем формат ISO 8601: "2026-04-20T10:00:00Z"
[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.
event_to_json(Event) ->
LocationJson = case Event#event.location of
undefined -> null;
#location{address = Addr, lat = Lat, lon = Lon} ->
#{address => Addr, lat => Lat, lon => Lon}
end,
#{
id => Event#event.id,
calendar_id => Event#event.calendar_id,
title => Event#event.title,
description => Event#event.description,
event_type => Event#event.event_type,
start_time => datetime_to_iso8601(Event#event.start_time),
duration => Event#event.duration,
specialist_id => Event#event.specialist_id,
location => LocationJson,
tags => Event#event.tags,
capacity => Event#event.capacity,
online_link => Event#event.online_link,
status => Event#event.status,
rating_avg => Event#event.rating_avg,
rating_count => Event#event.rating_count,
created_at => datetime_to_iso8601(Event#event.created_at),
updated_at => datetime_to_iso8601(Event#event.updated_at)
}.
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).

View File

@@ -66,10 +66,12 @@ delete_calendar(UserId, CalendarId) ->
Error
end.
%% Проверка прав доступа
can_access(_UserId, #calendar{status = active}) -> true;
%% Проверка прав доступа (просмотр)
can_access(UserId, #calendar{owner_id = UserId, status = active}) -> true;
can_access(_UserId, #calendar{type = commercial, status = active}) -> true;
can_access(_UserId, _) -> false.
%% Проверка прав редактирования
can_edit(UserId, #calendar{owner_id = UserId, status = active}) -> true;
can_edit(_, _) -> false.

121
src/logic/logic_event.erl Normal file
View File

@@ -0,0 +1,121 @@
-module(logic_event).
-include("records.hrl").
-export([create_event/5, get_event/2, list_events/2,
update_event/3, delete_event/2]).
-export([validate_event_time/1]).
%% Создание события
create_event(UserId, CalendarId, Title, StartTime, Duration) ->
% Проверяем, что календарь существует и пользователь имеет права
case logic_calendar:get_calendar(UserId, CalendarId) of
{ok, Calendar} ->
case logic_calendar:can_edit(UserId, Calendar) of
true ->
% Валидация времени
case validate_event_time(StartTime) of
ok ->
core_event:create(CalendarId, Title, StartTime, Duration);
{error, _} = Error ->
Error
end;
false ->
{error, access_denied}
end;
Error ->
Error
end.
%% Получение события с проверкой доступа
get_event(UserId, EventId) ->
case core_event:get_by_id(EventId) of
{ok, Event} ->
% Проверяем доступ к календарю
case logic_calendar:get_calendar(UserId, Event#event.calendar_id) of
{ok, _} -> {ok, Event};
Error -> Error
end;
Error ->
Error
end.
%% Список событий календаря
list_events(UserId, CalendarId) ->
case logic_calendar:get_calendar(UserId, CalendarId) of
{ok, _} ->
core_event:list_by_calendar(CalendarId);
Error ->
Error
end.
%% Обновление события
update_event(UserId, EventId, Updates) ->
case core_event:get_by_id(EventId) of
{ok, Event} ->
% Проверяем права на календарь
case logic_calendar:get_calendar(UserId, Event#event.calendar_id) of
{ok, Calendar} ->
case logic_calendar:can_edit(UserId, Calendar) of
true ->
% Валидация обновлений
ValidUpdates = validate_updates(Updates),
core_event:update(EventId, ValidUpdates);
false ->
{error, access_denied}
end;
Error ->
Error
end;
Error ->
Error
end.
%% Удаление события
delete_event(UserId, EventId) ->
case core_event:get_by_id(EventId) of
{ok, Event} ->
case logic_calendar:get_calendar(UserId, Event#event.calendar_id) of
{ok, Calendar} ->
case logic_calendar:can_edit(UserId, Calendar) of
true ->
core_event:delete(EventId);
false ->
{error, access_denied}
end;
Error ->
Error
end;
Error ->
Error
end.
%% Валидация времени события (не в прошлом)
validate_event_time(StartTime) ->
Now = calendar:universal_time(),
case StartTime > Now of
true -> ok;
false -> {error, event_in_past}
end.
%% Валидация полей обновления
validate_updates(Updates) ->
lists:filter(fun validate_update/1, Updates).
validate_update({title, Value}) when is_binary(Value) -> true;
validate_update({description, Value}) when is_binary(Value) -> true;
validate_update({start_time, Value}) ->
case validate_event_time(Value) of
ok -> true;
_ -> false
end;
validate_update({duration, Value}) when is_integer(Value), Value > 0 -> true;
validate_update({specialist_id, Value}) when is_binary(Value) -> true;
validate_update({location, Value}) ->
case Value of
#location{} -> true;
_ -> false
end;
validate_update({tags, Value}) when is_list(Value) -> true;
validate_update({capacity, Value}) when is_integer(Value), Value > 0 -> true;
validate_update({online_link, Value}) when is_binary(Value) -> true;
validate_update(_) -> false.