105 lines
3.9 KiB
Erlang
105 lines
3.9 KiB
Erlang
%%%-------------------------------------------------------------------
|
|
%%% @doc Обработчик бронирований текущего пользователя (клиентский API).
|
|
%%% GET – возвращает список всех бронирований, сделанных пользователем.
|
|
%%% @end
|
|
%%%-------------------------------------------------------------------
|
|
-module(handler_user_bookings).
|
|
-behaviour(cowboy_handler).
|
|
|
|
-export([init/2]).
|
|
-export([trails/0]).
|
|
|
|
-include("records.hrl").
|
|
|
|
%%% cowboy_handler callback
|
|
-spec init(cowboy_req:req(), any()) -> {ok, cowboy_req:req(), any()}.
|
|
init(Req, Opts) ->
|
|
handle(Req, Opts).
|
|
|
|
%%% Swagger metadata
|
|
-spec trails() -> [map()].
|
|
trails() ->
|
|
[
|
|
#{
|
|
path => <<"/v1/user/bookings">>,
|
|
method => <<"GET">>,
|
|
description => <<"List bookings of the current user">>,
|
|
tags => [<<"Bookings">>],
|
|
responses => #{
|
|
200 => #{
|
|
description => <<"Array of bookings">>,
|
|
content => #{<<"application/json">> => #{schema => #{
|
|
type => array,
|
|
items => booking_schema()
|
|
}}}
|
|
},
|
|
401 => #{description => <<"Unauthorized">>}
|
|
}
|
|
}
|
|
].
|
|
|
|
booking_schema() ->
|
|
#{
|
|
type => object,
|
|
properties => #{
|
|
id => #{type => string},
|
|
event_id => #{type => string},
|
|
user_id => #{type => string},
|
|
status => #{type => string, enum => [<<"pending">>, <<"confirmed">>, <<"cancelled">>]},
|
|
notes => #{type => string, nullable => true},
|
|
reminder_sent => #{type => boolean},
|
|
confirmed_at => #{type => string, format => <<"date-time">>, nullable => true},
|
|
created_at => #{type => string, format => <<"date-time">>},
|
|
updated_at => #{type => string, format => <<"date-time">>}
|
|
}
|
|
}.
|
|
|
|
%%%===================================================================
|
|
%%% HTTP-методы
|
|
%%%===================================================================
|
|
|
|
%% @private
|
|
-spec handle(cowboy_req:req(), any()) -> {ok, cowboy_req:req(), any()}.
|
|
handle(Req, _Opts) ->
|
|
case cowboy_req:method(Req) of
|
|
<<"GET">> -> list_user_bookings(Req);
|
|
_ -> handler_utils:send_error(Req, 405, <<"Method not allowed">>)
|
|
end.
|
|
|
|
%% @doc GET /v1/user/bookings — список бронирований текущего пользователя.
|
|
-spec list_user_bookings(cowboy_req:req()) -> {ok, binary(), cowboy_req:req()}.
|
|
list_user_bookings(Req) ->
|
|
case handler_utils:auth_user(Req) of
|
|
{ok, UserId, Req1} ->
|
|
case logic_booking:list_user_bookings(UserId) of
|
|
{ok, Bookings} ->
|
|
Response = [booking_to_json(B) || B <- Bookings],
|
|
handler_utils:send_json(Req1, 200, Response);
|
|
{error, _} ->
|
|
handler_utils:send_error(Req1, 500, <<"Internal server error">>)
|
|
end;
|
|
{error, Code, Message, Req1} ->
|
|
handler_utils:send_error(Req1, Code, Message)
|
|
end.
|
|
|
|
%%%===================================================================
|
|
%%% Внутренние функции
|
|
%%%===================================================================
|
|
|
|
%% @private Формирует JSON-представление записи #booking{}.
|
|
-spec booking_to_json(#booking{}) -> map().
|
|
booking_to_json(Booking) ->
|
|
#{
|
|
id => Booking#booking.id,
|
|
event_id => Booking#booking.event_id,
|
|
user_id => Booking#booking.user_id,
|
|
status => Booking#booking.status,
|
|
notes => Booking#booking.notes,
|
|
reminder_sent => Booking#booking.reminder_sent,
|
|
confirmed_at => case Booking#booking.confirmed_at of
|
|
undefined -> null;
|
|
Dt -> handler_utils:datetime_to_iso8601(Dt)
|
|
end,
|
|
created_at => handler_utils:datetime_to_iso8601(Booking#booking.created_at),
|
|
updated_at => handler_utils:datetime_to_iso8601(Booking#booking.updated_at)
|
|
}. |