Избавление от undefined, всем необязательным полям присваиваются дефолтные значения #22
This commit is contained in:
+120
-73
@@ -1,41 +1,127 @@
|
||||
-module(core_booking).
|
||||
-include("records.hrl").
|
||||
-export([create/3, get_by_id/1, list_by_event/1, list_by_user/1, update/2, delete/1]).
|
||||
-export([count_bookings/0, get_by_event_and_user/2]).
|
||||
-export([list_all/0]). % ← добавлено
|
||||
|
||||
-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([count_bookings/0]).
|
||||
%% ─────────────────────────────────────────────────────────────────
|
||||
%% Значения по умолчанию для необязательных полей
|
||||
%% ─────────────────────────────────────────────────────────────────
|
||||
-define(DEFAULT_NOTES, <<>>).
|
||||
-define(DEFAULT_REMINDER_SENT, false).
|
||||
-define(DEFAULT_CONFIRMED_AT, {{1970,1,1},{0,0,0}}).
|
||||
|
||||
%% Создание бронирования
|
||||
create(EventId, UserId) ->
|
||||
%%%-------------------------------------------------------------------
|
||||
%%% @doc Создание бронирования.
|
||||
%%% @end
|
||||
%%%-------------------------------------------------------------------
|
||||
-spec create(EventId :: binary(), UserId :: binary(), Status :: atom()) ->
|
||||
{ok, #booking{}} | {error, term()}.
|
||||
create(EventId, UserId, Status) ->
|
||||
Id = infra_utils:generate_id(16),
|
||||
Now = calendar:universal_time(),
|
||||
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()
|
||||
id = Id,
|
||||
event_id = EventId,
|
||||
user_id = UserId,
|
||||
status = Status,
|
||||
notes = ?DEFAULT_NOTES,
|
||||
reminder_sent = ?DEFAULT_REMINDER_SENT,
|
||||
confirmed_at = ?DEFAULT_CONFIRMED_AT,
|
||||
created_at = Now,
|
||||
updated_at = Now
|
||||
},
|
||||
|
||||
F = fun() ->
|
||||
mnesia:write(Booking),
|
||||
{ok, Booking}
|
||||
end,
|
||||
|
||||
F = fun() -> mnesia:write(Booking), {ok, Booking} end,
|
||||
case mnesia:transaction(F) of
|
||||
{atomic, Result} -> Result;
|
||||
{aborted, Reason} -> {error, Reason}
|
||||
end.
|
||||
|
||||
%% Получение бронирования по ID
|
||||
%%%-------------------------------------------------------------------
|
||||
%%% @doc Получить бронирование по идентификатору.
|
||||
%%% @end
|
||||
%%%-------------------------------------------------------------------
|
||||
-spec get_by_id(Id :: binary()) -> {ok, #booking{}} | {error, not_found}.
|
||||
get_by_id(Id) ->
|
||||
case mnesia:dirty_read(booking, Id) of
|
||||
[] -> {error, not_found};
|
||||
[Booking] -> {ok, Booking}
|
||||
end.
|
||||
|
||||
%% Получение бронирования по событию и пользователю
|
||||
%%%-------------------------------------------------------------------
|
||||
%%% @doc Список бронирований для события.
|
||||
%%% @end
|
||||
%%%-------------------------------------------------------------------
|
||||
-spec list_by_event(EventId :: binary()) -> {ok, [#booking{}]}.
|
||||
list_by_event(EventId) ->
|
||||
Match = #booking{event_id = EventId, _ = '_'},
|
||||
Bookings = mnesia:dirty_match_object(Match),
|
||||
{ok, Bookings}.
|
||||
|
||||
%%%-------------------------------------------------------------------
|
||||
%%% @doc Список бронирований пользователя.
|
||||
%%% @end
|
||||
%%%-------------------------------------------------------------------
|
||||
-spec list_by_user(UserId :: binary()) -> {ok, [#booking{}]}.
|
||||
list_by_user(UserId) ->
|
||||
Match = #booking{user_id = UserId, _ = '_'},
|
||||
Bookings = mnesia:dirty_match_object(Match),
|
||||
{ok, Bookings}.
|
||||
|
||||
%%%-------------------------------------------------------------------
|
||||
%%% @doc Получить все бронирования (административный режим).
|
||||
%%% @end
|
||||
%%%-------------------------------------------------------------------
|
||||
-spec list_all() -> [#booking{}].
|
||||
list_all() ->
|
||||
mnesia:dirty_match_object(#booking{_ = '_'}).
|
||||
|
||||
%%%-------------------------------------------------------------------
|
||||
%%% @doc Обновить поля бронирования.
|
||||
%%% @end
|
||||
%%%-------------------------------------------------------------------
|
||||
-spec update(Id :: binary(), Updates :: [{atom(), term()}]) ->
|
||||
{ok, #booking{}} | {error, not_found | term()}.
|
||||
update(Id, Updates) ->
|
||||
F = fun() ->
|
||||
case mnesia:read(booking, Id) of
|
||||
[] -> {error, not_found};
|
||||
[Booking] ->
|
||||
UpdatedBooking = apply_updates(Booking, Updates),
|
||||
mnesia:write(UpdatedBooking),
|
||||
{ok, UpdatedBooking}
|
||||
end
|
||||
end,
|
||||
case mnesia:transaction(F) of
|
||||
{atomic, Result} -> Result;
|
||||
{aborted, Reason} -> {error, Reason}
|
||||
end.
|
||||
|
||||
%%%-------------------------------------------------------------------
|
||||
%%% @doc Удаление бронирования (физическое).
|
||||
%%% @end
|
||||
%%%-------------------------------------------------------------------
|
||||
-spec delete(Id :: binary()) -> ok | {error, not_found}.
|
||||
delete(Id) ->
|
||||
case mnesia:dirty_read(booking, Id) of
|
||||
[] -> {error, not_found};
|
||||
[_] -> mnesia:dirty_delete(booking, Id), ok
|
||||
end.
|
||||
|
||||
%%%-------------------------------------------------------------------
|
||||
%%% @doc Количество бронирований.
|
||||
%%% @end
|
||||
%%%-------------------------------------------------------------------
|
||||
-spec count_bookings() -> non_neg_integer().
|
||||
count_bookings() ->
|
||||
mnesia:table_info(booking, size).
|
||||
|
||||
%%%-------------------------------------------------------------------
|
||||
%%% @doc Получить бронирование по событию и пользователю.
|
||||
%%% @end
|
||||
%%%-------------------------------------------------------------------
|
||||
-spec get_by_event_and_user(EventId :: binary(), UserId :: binary()) ->
|
||||
{ok, #booking{}} | {error, not_found}.
|
||||
get_by_event_and_user(EventId, UserId) ->
|
||||
Match = #booking{event_id = EventId, user_id = UserId, _ = '_'},
|
||||
case mnesia:dirty_match_object(Match) of
|
||||
@@ -43,58 +129,19 @@ get_by_event_and_user(EventId, UserId) ->
|
||||
[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}.
|
||||
apply_updates(Booking, Updates) ->
|
||||
Updated = lists:foldl(fun({Field, Value}, B) -> set_field(Field, Value, B) end,
|
||||
Booking, Updates),
|
||||
Updated#booking{updated_at = calendar:universal_time()}.
|
||||
|
||||
%% Обновление статуса бронирования
|
||||
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.
|
||||
|
||||
count_bookings() -> mnesia:table_info(booking, size).
|
||||
set_field(status, Value, B) -> B#booking{status = Value};
|
||||
set_field(notes, Value, B) -> B#booking{notes = Value};
|
||||
set_field(reminder_sent, Value, B) -> B#booking{reminder_sent = Value};
|
||||
set_field(confirmed_at, Value, B) -> B#booking{confirmed_at = Value};
|
||||
set_field(event_id, Value, B) -> B#booking{event_id = Value};
|
||||
set_field(user_id, Value, B) -> B#booking{user_id = Value};
|
||||
set_field(_, _, B) -> B.
|
||||
Reference in New Issue
Block a user