Stage 3.4
This commit is contained in:
@@ -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}).
|
||||
@@ -1,11 +1,11 @@
|
||||
-module(core_calendar).
|
||||
-include("records.hrl").
|
||||
|
||||
-export([create/3, get_by_id/1, list_by_owner/1, update/2, delete/1]).
|
||||
-export([create/4, get_by_id/1, list_by_owner/1, update/2, delete/1]).
|
||||
-export([generate_id/0]).
|
||||
|
||||
%% Создание календаря
|
||||
create(OwnerId, Title, Description) ->
|
||||
create(OwnerId, Title, Description, Confirmation) ->
|
||||
Id = generate_id(),
|
||||
Calendar = #calendar{
|
||||
id = Id,
|
||||
@@ -14,7 +14,7 @@ create(OwnerId, Title, Description) ->
|
||||
description = Description,
|
||||
tags = [],
|
||||
type = personal,
|
||||
confirmation = manual,
|
||||
confirmation = Confirmation,
|
||||
rating_avg = 0.0,
|
||||
rating_count = 0,
|
||||
status = active,
|
||||
|
||||
Reference in New Issue
Block a user