Stage 3.4

This commit is contained in:
2026-04-20 16:40:44 +03:00
parent 42a047a938
commit b24cbc97f3
25 changed files with 2520 additions and 123 deletions
+102
View File
@@ -0,0 +1,102 @@
-module(core_booking).
-include("records.hrl").
-export([create/2, get_by_id/1, get_by_event_and_user/2, list_by_event/1, list_by_user/1]).
-export([update_status/2, delete/1]).
-export([generate_id/0]).
%% Создание бронирования
create(EventId, UserId) ->
Id = generate_id(),
Booking = #booking{
id = Id,
event_id = EventId,
user_id = UserId,
status = pending,
confirmed_at = undefined,
created_at = calendar:universal_time(),
updated_at = calendar:universal_time()
},
F = fun() ->
mnesia:write(Booking),
{ok, Booking}
end,
case mnesia:transaction(F) of
{atomic, Result} -> Result;
{aborted, Reason} -> {error, Reason}
end.
%% Получение бронирования по ID
get_by_id(Id) ->
case mnesia:dirty_read(booking, Id) of
[] -> {error, not_found};
[Booking] -> {ok, Booking}
end.
%% Получение бронирования по событию и пользователю
get_by_event_and_user(EventId, UserId) ->
Match = #booking{event_id = EventId, user_id = UserId, _ = '_'},
case mnesia:dirty_match_object(Match) of
[] -> {error, not_found};
[Booking] -> {ok, Booking}
end.
%% Список бронирований события
list_by_event(EventId) ->
Match = #booking{event_id = EventId, _ = '_'},
Bookings = mnesia:dirty_match_object(Match),
{ok, Bookings}.
%% Список бронирований пользователя
list_by_user(UserId) ->
Match = #booking{user_id = UserId, _ = '_'},
Bookings = mnesia:dirty_match_object(Match),
{ok, Bookings}.
%% Обновление статуса бронирования
update_status(Id, Status) when Status =:= pending; Status =:= confirmed; Status =:= cancelled ->
F = fun() ->
case mnesia:read(booking, Id) of
[] ->
{error, not_found};
[Booking] ->
Updated = Booking#booking{
status = Status,
confirmed_at = case Status of
confirmed -> calendar:universal_time();
_ -> Booking#booking.confirmed_at
end,
updated_at = calendar:universal_time()
},
mnesia:write(Updated),
{ok, Updated}
end
end,
case mnesia:transaction(F) of
{atomic, Result} -> Result;
{aborted, Reason} -> {error, Reason}
end.
%% Удаление бронирования (hard delete)
delete(Id) ->
F = fun() ->
case mnesia:read(booking, Id) of
[] ->
{error, not_found};
[Booking] ->
mnesia:delete_object(Booking),
{ok, deleted}
end
end,
case mnesia:transaction(F) of
{atomic, Result} -> Result;
{aborted, Reason} -> {error, Reason}
end.
%% Внутренние функции
generate_id() ->
base64:encode(crypto:strong_rand_bytes(16), #{mode => urlsafe, padding => false}).