Рефакторинг обработчиков. Часть 2 #21
This commit is contained in:
@@ -1,89 +1,175 @@
|
||||
%%%-------------------------------------------------------------------
|
||||
%%% @doc Обработчик маршрута `/v1/events/:id/bookings`.
|
||||
%%%
|
||||
%%% POST – Создание бронирования (запись на событие).
|
||||
%%% GET – Получение списка бронирований события (для владельца календаря).
|
||||
%%% @end
|
||||
%%%-------------------------------------------------------------------
|
||||
-module(handler_bookings).
|
||||
-include("records.hrl").
|
||||
-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).
|
||||
|
||||
-spec handle(cowboy_req:req(), any()) -> {ok, cowboy_req:req(), any()}.
|
||||
handle(Req, _Opts) ->
|
||||
case cowboy_req:method(Req) of
|
||||
<<"POST">> -> create_booking(Req);
|
||||
<<"GET">> -> list_bookings(Req);
|
||||
_ -> send_error(Req, 405, <<"Method not allowed">>)
|
||||
<<"GET">> -> list_bookings(Req);
|
||||
_ -> handler_utils:send_error(Req, 405, <<"Method not allowed">>)
|
||||
end.
|
||||
|
||||
%% POST /v1/events/:id/bookings - создание бронирования (запись на событие)
|
||||
%%% Swagger metadata
|
||||
-spec trails() -> [map()].
|
||||
trails() ->
|
||||
BookingSchema = booking_schema(),
|
||||
[
|
||||
#{ % POST
|
||||
path => <<"/v1/events/:id/bookings">>,
|
||||
method => <<"POST">>,
|
||||
description => <<"Create a booking for an event">>,
|
||||
tags => [<<"Bookings">>],
|
||||
parameters => [
|
||||
#{
|
||||
name => <<"id">>,
|
||||
in => <<"path">>,
|
||||
description => <<"Event ID">>,
|
||||
required => true,
|
||||
schema => #{type => string}
|
||||
}
|
||||
],
|
||||
responses => #{
|
||||
201 => #{
|
||||
description => <<"Booking created">>,
|
||||
content => #{<<"application/json">> => #{schema => BookingSchema}}
|
||||
},
|
||||
400 => #{description => <<"Event is full or not active">>},
|
||||
403 => #{description => <<"Access denied">>},
|
||||
404 => #{description => <<"Event not found">>},
|
||||
409 => #{description => <<"Already booked">>}
|
||||
}
|
||||
},
|
||||
#{ % GET list
|
||||
path => <<"/v1/events/:id/bookings">>,
|
||||
method => <<"GET">>,
|
||||
description => <<"List bookings for an event (owner only)">>,
|
||||
tags => [<<"Bookings">>],
|
||||
parameters => [
|
||||
#{
|
||||
name => <<"id">>,
|
||||
in => <<"path">>,
|
||||
description => <<"Event ID">>,
|
||||
required => true,
|
||||
schema => #{type => string}
|
||||
}
|
||||
],
|
||||
responses => #{
|
||||
200 => #{
|
||||
description => <<"Array of bookings">>,
|
||||
content => #{<<"application/json">> => #{schema => #{
|
||||
type => array,
|
||||
items => BookingSchema
|
||||
}}}
|
||||
},
|
||||
403 => #{description => <<"Access denied">>},
|
||||
404 => #{description => <<"Event not found">>}
|
||||
}
|
||||
}
|
||||
].
|
||||
|
||||
-spec booking_schema() -> map().
|
||||
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-методы
|
||||
%%%===================================================================
|
||||
|
||||
%% @doc POST /v1/events/:id/bookings — Создание бронирования.
|
||||
-spec create_booking(cowboy_req:req()) -> {ok, binary(), cowboy_req:req()}.
|
||||
create_booking(Req) ->
|
||||
case handler_auth:authenticate(Req) of
|
||||
case handler_utils:auth_user(Req) of
|
||||
{ok, UserId, Req1} ->
|
||||
EventId = cowboy_req:binding(id, Req1),
|
||||
case logic_booking:create_booking(UserId, EventId) of
|
||||
{ok, Booking} ->
|
||||
Response = booking_to_json(Booking),
|
||||
send_json(Req1, 201, Response);
|
||||
handler_utils:send_json(Req1, 201, booking_to_json(Booking));
|
||||
{error, already_booked} ->
|
||||
send_error(Req1, 409, <<"Already booked">>);
|
||||
handler_utils:send_error(Req1, 409, <<"Already booked">>);
|
||||
{error, event_full} ->
|
||||
send_error(Req1, 400, <<"Event is full">>);
|
||||
handler_utils:send_error(Req1, 400, <<"Event is full">>);
|
||||
{error, event_not_active} ->
|
||||
send_error(Req1, 400, <<"Event is not active">>);
|
||||
handler_utils:send_error(Req1, 400, <<"Event is not active">>);
|
||||
{error, access_denied} ->
|
||||
send_error(Req1, 403, <<"Access denied">>);
|
||||
handler_utils:send_error(Req1, 403, <<"Access denied">>);
|
||||
{error, not_found} ->
|
||||
send_error(Req1, 404, <<"Event not found">>);
|
||||
handler_utils:send_error(Req1, 404, <<"Event not found">>);
|
||||
{error, _} ->
|
||||
send_error(Req1, 500, <<"Internal server error">>)
|
||||
handler_utils:send_error(Req1, 500, <<"Internal server error">>)
|
||||
end;
|
||||
{error, Code, Message, Req1} ->
|
||||
send_error(Req1, Code, Message)
|
||||
handler_utils:send_error(Req1, Code, Message)
|
||||
end.
|
||||
|
||||
%% GET /v1/events/:id/bookings - список бронирований события (для владельца)
|
||||
%% @doc GET /v1/events/:id/bookings — Список бронирований события (для владельца).
|
||||
-spec list_bookings(cowboy_req:req()) -> {ok, binary(), cowboy_req:req()}.
|
||||
list_bookings(Req) ->
|
||||
case handler_auth:authenticate(Req) of
|
||||
case handler_utils:auth_user(Req) of
|
||||
{ok, UserId, Req1} ->
|
||||
EventId = cowboy_req:binding(id, Req1),
|
||||
case logic_booking:list_event_bookings(UserId, EventId) of
|
||||
{ok, Bookings} ->
|
||||
Response = [booking_to_json(B) || B <- Bookings],
|
||||
send_json(Req1, 200, Response);
|
||||
handler_utils:send_json(Req1, 200, Response);
|
||||
{error, access_denied} ->
|
||||
send_error(Req1, 403, <<"Access denied">>);
|
||||
handler_utils:send_error(Req1, 403, <<"Access denied">>);
|
||||
{error, not_found} ->
|
||||
send_error(Req1, 404, <<"Event not found">>);
|
||||
handler_utils:send_error(Req1, 404, <<"Event not found">>);
|
||||
{error, _} ->
|
||||
send_error(Req1, 500, <<"Internal server error">>)
|
||||
handler_utils:send_error(Req1, 500, <<"Internal server error">>)
|
||||
end;
|
||||
{error, Code, Message, Req1} ->
|
||||
send_error(Req1, Code, Message)
|
||||
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,
|
||||
confirmed_at => case Booking#booking.confirmed_at of
|
||||
undefined -> null;
|
||||
Dt -> datetime_to_iso8601(Dt)
|
||||
end,
|
||||
created_at => datetime_to_iso8601(Booking#booking.created_at),
|
||||
updated_at => datetime_to_iso8601(Booking#booking.updated_at)
|
||||
}.
|
||||
|
||||
datetime_to_iso8601({{Year, Month, Day}, {Hour, Minute, Second}}) ->
|
||||
iolist_to_binary(io_lib:format("~4..0B-~2..0B-~2..0BT~2..0B:~2..0B:~2..0BZ",
|
||||
[Year, Month, Day, Hour, Minute, Second])).
|
||||
|
||||
send_json(Req, Status, Data) ->
|
||||
Body = jsx:encode(Data),
|
||||
cowboy_req:reply(Status, #{<<"content-type">> => <<"application/json">>}, Body, Req),
|
||||
{ok, Body, []}.
|
||||
|
||||
send_error(Req, Status, Message) ->
|
||||
Body = jsx:encode(#{error => Message}),
|
||||
cowboy_req:reply(Status, #{<<"content-type">> => <<"application/json">>}, Body, Req),
|
||||
{ok, Body, []}.
|
||||
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)
|
||||
}.
|
||||
Reference in New Issue
Block a user