Stage 3.2

This commit is contained in:
2026-04-20 11:42:03 +03:00
parent 4224da1a22
commit 491b4ae179
8 changed files with 403 additions and 4 deletions

View File

@@ -0,0 +1,91 @@
-module(logic_calendar).
-include("records.hrl").
-export([create_calendar/3, get_calendar/2, list_calendars/1,
update_calendar/3, delete_calendar/2]).
-export([can_access/2, can_edit/2]).
%% Создание календаря
create_calendar(UserId, Title, Description) ->
% Проверка, что пользователь может создавать календари
case core_user:get_by_id(UserId) of
{ok, User} ->
case User#user.status of
active ->
core_calendar:create(UserId, Title, Description);
_ ->
{error, user_inactive}
end;
{error, _} ->
{error, user_not_found}
end.
%% Получение календаря с проверкой доступа
get_calendar(UserId, CalendarId) ->
case core_calendar:get_by_id(CalendarId) of
{ok, Calendar} ->
case can_access(UserId, Calendar) of
true -> {ok, Calendar};
false -> {error, access_denied}
end;
Error ->
Error
end.
%% Список календарей пользователя
list_calendars(UserId) ->
core_calendar:list_by_owner(UserId).
%% Обновление календаря
update_calendar(UserId, CalendarId, Updates) ->
case core_calendar:get_by_id(CalendarId) of
{ok, Calendar} ->
case can_edit(UserId, Calendar) of
true ->
% Валидация обновлений
ValidUpdates = validate_updates(Updates),
core_calendar:update(CalendarId, ValidUpdates);
false ->
{error, access_denied}
end;
Error ->
Error
end.
%% Удаление календаря
delete_calendar(UserId, CalendarId) ->
case core_calendar:get_by_id(CalendarId) of
{ok, Calendar} ->
case can_edit(UserId, Calendar) of
true ->
core_calendar:delete(CalendarId);
false ->
{error, access_denied}
end;
Error ->
Error
end.
%% Проверка прав доступа
can_access(_UserId, #calendar{status = active}) -> true;
can_access(_UserId, _) -> false.
can_edit(UserId, #calendar{owner_id = UserId, status = active}) -> true;
can_edit(_, _) -> false.
%% Валидация полей обновления
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({tags, Value}) when is_list(Value) -> true;
validate_update({type, Value}) when Value =:= personal; Value =:= commercial -> true;
validate_update({confirmation, Value}) ->
case Value of
auto -> true;
manual -> true;
{timeout, N} when is_integer(N), N > 0 -> true;
_ -> false
end;
validate_update(_) -> false.