Stage 3.2 + tests
This commit is contained in:
@@ -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.
|
||||
Reference in New Issue
Block a user