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
+4 -2
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
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.