Рефакторинг обработчиков. Часть 2 #21
This commit is contained in:
@@ -1,42 +1,159 @@
|
||||
%%%-------------------------------------------------------------------
|
||||
%%% @doc Обработчик конкретного календаря (клиентский API).
|
||||
%%%
|
||||
%%% GET – получить информацию о календаре.
|
||||
%%% PUT – обновить календарь (владельцем).
|
||||
%%% DELETE – удалить календарь (владельцем).
|
||||
%%% @end
|
||||
%%%-------------------------------------------------------------------
|
||||
-module(handler_calendar_by_id).
|
||||
-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
|
||||
<<"GET">> -> get_calendar(Req);
|
||||
<<"PUT">> -> update_calendar(Req);
|
||||
<<"GET">> -> get_calendar(Req);
|
||||
<<"PUT">> -> update_calendar(Req);
|
||||
<<"DELETE">> -> delete_calendar(Req);
|
||||
_ -> send_error(Req, 405, <<"Method not allowed">>)
|
||||
_ -> handler_utils:send_error(Req, 405, <<"Method not allowed">>)
|
||||
end.
|
||||
|
||||
%% GET /v1/calendars/:id - получение календаря
|
||||
%%% Swagger metadata
|
||||
-spec trails() -> [map()].
|
||||
trails() ->
|
||||
BaseParams = [
|
||||
#{
|
||||
name => <<"id">>,
|
||||
in => <<"path">>,
|
||||
description => <<"Calendar ID">>,
|
||||
required => true,
|
||||
schema => #{type => string}
|
||||
}
|
||||
],
|
||||
[
|
||||
#{ % GET
|
||||
path => <<"/v1/calendars/:id">>,
|
||||
method => <<"GET">>,
|
||||
description => <<"Get calendar by ID">>,
|
||||
tags => [<<"Calendars">>],
|
||||
parameters => BaseParams,
|
||||
responses => #{
|
||||
200 => #{
|
||||
description => <<"Calendar details">>,
|
||||
content => #{<<"application/json">> => #{schema => calendar_schema()}}
|
||||
},
|
||||
403 => #{description => <<"Access denied">>},
|
||||
404 => #{description => <<"Calendar not found">>}
|
||||
}
|
||||
},
|
||||
#{ % PUT
|
||||
path => <<"/v1/calendars/:id">>,
|
||||
method => <<"PUT">>,
|
||||
description => <<"Update calendar">>,
|
||||
tags => [<<"Calendars">>],
|
||||
parameters => BaseParams,
|
||||
requestBody => #{
|
||||
required => true,
|
||||
content => #{<<"application/json">> => #{schema => calendar_update_schema()}}
|
||||
},
|
||||
responses => #{
|
||||
200 => #{description => <<"Calendar updated">>},
|
||||
400 => #{description => <<"Invalid request">>},
|
||||
403 => #{description => <<"Access denied">>},
|
||||
404 => #{description => <<"Calendar not found">>}
|
||||
}
|
||||
},
|
||||
#{ % DELETE
|
||||
path => <<"/v1/calendars/:id">>,
|
||||
method => <<"DELETE">>,
|
||||
description => <<"Delete calendar">>,
|
||||
tags => [<<"Calendars">>],
|
||||
parameters => BaseParams,
|
||||
responses => #{
|
||||
200 => #{description => <<"Calendar deleted">>},
|
||||
403 => #{description => <<"Access denied">>},
|
||||
404 => #{description => <<"Calendar not found">>}
|
||||
}
|
||||
}
|
||||
].
|
||||
|
||||
-spec calendar_schema() -> map().
|
||||
calendar_schema() ->
|
||||
#{
|
||||
type => object,
|
||||
properties => #{
|
||||
id => #{type => string},
|
||||
owner_id => #{type => string},
|
||||
title => #{type => string},
|
||||
description => #{type => string},
|
||||
short_name => #{type => string, nullable => true},
|
||||
category => #{type => string, nullable => true},
|
||||
color => #{type => string, nullable => true},
|
||||
image_url => #{type => string, nullable => true},
|
||||
settings => #{type => object, nullable => true},
|
||||
tags => #{type => array, items => #{type => string}},
|
||||
type => #{type => string, enum => [<<"personal">>, <<"commercial">>]},
|
||||
confirmation => #{type => string, description => <<"auto, manual, or {timeout, N}">>},
|
||||
rating_avg => #{type => number, format => float},
|
||||
rating_count => #{type => integer},
|
||||
status => #{type => string, enum => [<<"active">>, <<"frozen">>, <<"deleted">>]},
|
||||
reason => #{type => string, nullable => true},
|
||||
created_at => #{type => string, format => <<"date-time">>},
|
||||
updated_at => #{type => string, format => <<"date-time">>}
|
||||
}
|
||||
}.
|
||||
|
||||
-spec calendar_update_schema() -> map().
|
||||
calendar_update_schema() ->
|
||||
#{
|
||||
type => object,
|
||||
properties => #{
|
||||
title => #{type => string},
|
||||
description => #{type => string},
|
||||
type => #{type => string, enum => [<<"personal">>, <<"commercial">>]},
|
||||
confirmation => #{type => string, description => <<"auto, manual, or {timeout, N}">>},
|
||||
tags => #{type => array, items => #{type => string}}
|
||||
}
|
||||
}.
|
||||
|
||||
%%%===================================================================
|
||||
%%% HTTP-методы
|
||||
%%%===================================================================
|
||||
|
||||
%% @doc GET /v1/calendars/:id — получение календаря.
|
||||
-spec get_calendar(cowboy_req:req()) -> {ok, binary(), cowboy_req:req()}.
|
||||
get_calendar(Req) ->
|
||||
case handler_auth:authenticate(Req) of
|
||||
case handler_utils:auth_user(Req) of
|
||||
{ok, UserId, Req1} ->
|
||||
CalendarId = cowboy_req:binding(id, Req1),
|
||||
case logic_calendar:get_calendar(UserId, CalendarId) of
|
||||
{ok, Calendar} ->
|
||||
Response = calendar_to_json(Calendar),
|
||||
send_json(Req1, 200, Response);
|
||||
handler_utils:send_json(Req1, 200, handler_utils:calendar_to_json(Calendar));
|
||||
{error, access_denied} ->
|
||||
send_error(Req1, 403, <<"Access denied">>);
|
||||
handler_utils:send_error(Req1, 403, <<"Access denied">>);
|
||||
{error, not_found} ->
|
||||
send_error(Req1, 404, <<"Calendar not found">>);
|
||||
handler_utils:send_error(Req1, 404, <<"Calendar 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.
|
||||
|
||||
%% PUT /v1/calendars/:id - обновление календаря
|
||||
%% @doc PUT /v1/calendars/:id — обновление календаря.
|
||||
-spec update_calendar(cowboy_req:req()) -> {ok, binary(), cowboy_req:req()}.
|
||||
update_calendar(Req) ->
|
||||
case handler_auth:authenticate(Req) of
|
||||
case handler_utils:auth_user(Req) of
|
||||
{ok, UserId, Req1} ->
|
||||
CalendarId = cowboy_req:binding(id, Req1),
|
||||
{ok, Body, Req2} = cowboy_req:read_body(Req1),
|
||||
@@ -45,71 +162,39 @@ update_calendar(Req) ->
|
||||
Updates = maps:to_list(UpdatesMap),
|
||||
case logic_calendar:update_calendar(UserId, CalendarId, Updates) of
|
||||
{ok, Calendar} ->
|
||||
Response = calendar_to_json(Calendar),
|
||||
send_json(Req2, 200, Response);
|
||||
handler_utils:send_json(Req2, 200, handler_utils:calendar_to_json(Calendar));
|
||||
{error, access_denied} ->
|
||||
send_error(Req2, 403, <<"Access denied">>);
|
||||
handler_utils:send_error(Req2, 403, <<"Access denied">>);
|
||||
{error, not_found} ->
|
||||
send_error(Req2, 404, <<"Calendar not found">>);
|
||||
handler_utils:send_error(Req2, 404, <<"Calendar not found">>);
|
||||
{error, _} ->
|
||||
send_error(Req2, 500, <<"Internal server error">>)
|
||||
handler_utils:send_error(Req2, 500, <<"Internal server error">>)
|
||||
end;
|
||||
_ ->
|
||||
send_error(Req2, 400, <<"Invalid JSON">>)
|
||||
handler_utils:send_error(Req2, 400, <<"Invalid JSON">>)
|
||||
catch
|
||||
_:_ ->
|
||||
send_error(Req2, 400, <<"Invalid JSON format">>)
|
||||
_:_ -> handler_utils:send_error(Req2, 400, <<"Invalid JSON format">>)
|
||||
end;
|
||||
{error, Code, Message, Req1} ->
|
||||
send_error(Req1, Code, Message)
|
||||
handler_utils:send_error(Req1, Code, Message)
|
||||
end.
|
||||
|
||||
%% DELETE /v1/calendars/:id - удаление календаря
|
||||
%% @doc DELETE /v1/calendars/:id — удаление календаря.
|
||||
-spec delete_calendar(cowboy_req:req()) -> {ok, binary(), cowboy_req:req()}.
|
||||
delete_calendar(Req) ->
|
||||
case handler_auth:authenticate(Req) of
|
||||
case handler_utils:auth_user(Req) of
|
||||
{ok, UserId, Req1} ->
|
||||
CalendarId = cowboy_req:binding(id, Req1),
|
||||
case logic_calendar:delete_calendar(UserId, CalendarId) of
|
||||
{ok, _} ->
|
||||
send_json(Req1, 200, #{status => <<"deleted">>});
|
||||
handler_utils:send_json(Req1, 200, #{status => <<"deleted">>});
|
||||
{error, access_denied} ->
|
||||
send_error(Req1, 403, <<"Access denied">>);
|
||||
handler_utils:send_error(Req1, 403, <<"Access denied">>);
|
||||
{error, not_found} ->
|
||||
send_error(Req1, 404, <<"Calendar not found">>);
|
||||
handler_utils:send_error(Req1, 404, <<"Calendar 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)
|
||||
end.
|
||||
|
||||
%% Вспомогательные функции
|
||||
calendar_to_json(Calendar) ->
|
||||
#{
|
||||
id => Calendar#calendar.id,
|
||||
owner_id => Calendar#calendar.owner_id,
|
||||
title => Calendar#calendar.title,
|
||||
description => Calendar#calendar.description,
|
||||
tags => Calendar#calendar.tags,
|
||||
type => Calendar#calendar.type,
|
||||
confirmation => confirmation_to_json(Calendar#calendar.confirmation),
|
||||
rating_avg => Calendar#calendar.rating_avg,
|
||||
rating_count => Calendar#calendar.rating_count,
|
||||
status => Calendar#calendar.status,
|
||||
created_at => Calendar#calendar.created_at,
|
||||
updated_at => Calendar#calendar.updated_at
|
||||
}.
|
||||
|
||||
confirmation_to_json(auto) -> <<"auto">>;
|
||||
confirmation_to_json(manual) -> <<"manual">>;
|
||||
confirmation_to_json({timeout, N}) -> #{<<"timeout">> => N}.
|
||||
|
||||
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, []}.
|
||||
handler_utils:send_error(Req1, Code, Message)
|
||||
end.
|
||||
Reference in New Issue
Block a user