Stage 3.3

This commit is contained in:
2026-04-20 14:04:30 +03:00
parent 5291f70337
commit 42a047a938
9 changed files with 988 additions and 29 deletions
+95 -3
View File
@@ -1,10 +1,11 @@
-module(core_event).
-include("records.hrl").
-export([create/4, get_by_id/1, list_by_calendar/1, update/2, delete/1]).
-export([create/4, create_recurring/5, get_by_id/1, list_by_calendar/1,
update/2, delete/1, materialize_occurrence/3]).
-export([generate_id/0]).
%% Создание события (одиночного)
%% Создание одиночного события
create(CalendarId, Title, StartTime, Duration) ->
Id = generate_id(),
Event = #event{
@@ -40,6 +41,97 @@ create(CalendarId, Title, StartTime, Duration) ->
{aborted, Reason} -> {error, Reason}
end.
%% Создание повторяющегося события (мастер-запись)
create_recurring(CalendarId, Title, StartTime, Duration, RRule) ->
Id = generate_id(),
Event = #event{
id = Id,
calendar_id = CalendarId,
title = Title,
description = <<"">>,
event_type = recurring,
start_time = StartTime,
duration = Duration,
recurrence_rule = jsx:encode(RRule),
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.
%% Материализация вхождения повторяющегося события
materialize_occurrence(MasterId, OccurrenceStart, SpecialistId) ->
case mnesia:dirty_read(event, MasterId) of
[] ->
{error, master_not_found};
[Master] when Master#event.event_type =:= recurring ->
% Проверяем, не существует ли уже материализованное вхождение
Existing = mnesia:dirty_match_object(
#event{master_id = MasterId, start_time = OccurrenceStart, is_instance = true, _ = '_'}
),
case Existing of
[] ->
% Создаём новый экземпляр
InstanceId = generate_id(),
Instance = #event{
id = InstanceId,
calendar_id = Master#event.calendar_id,
title = Master#event.title,
description = Master#event.description,
event_type = single,
start_time = OccurrenceStart,
duration = Master#event.duration,
recurrence_rule = undefined,
master_id = MasterId,
is_instance = true,
specialist_id = SpecialistId,
location = Master#event.location,
tags = Master#event.tags,
capacity = Master#event.capacity,
online_link = Master#event.online_link,
status = active,
rating_avg = 0.0,
rating_count = 0,
created_at = calendar:universal_time(),
updated_at = calendar:universal_time()
},
F = fun() ->
mnesia:write(Instance),
{ok, Instance}
end,
case mnesia:transaction(F) of
{atomic, Result} -> Result;
{aborted, Reason} -> {error, Reason}
end;
[Instance] ->
% Уже существует, возвращаем его
{ok, Instance}
end;
[_] ->
{error, not_recurring}
end.
%% Получение события по ID
get_by_id(Id) ->
case mnesia:dirty_read(event, Id) of
@@ -47,7 +139,7 @@ get_by_id(Id) ->
[Event] -> {ok, Event}
end.
%% Список событий календаря
%% Список событий календаря (только мастер-записи и одиночные)
list_by_calendar(CalendarId) ->
Match = #event{calendar_id = CalendarId, status = active, is_instance = false, _ = '_'},
Events = mnesia:dirty_match_object(Match),