%%%------------------------------------------------------------------- %%% @doc Список календарей, которые пользователь отслеживает (follow). %%% %%% GET /v1/user/following %%% @end %%%------------------------------------------------------------------- -module(handler_user_following). -behaviour(cowboy_handler). -export([init/2]). -export([trails/0]). -include("records.hrl"). -spec init(cowboy_req:req(), any()) -> {ok, cowboy_req:req(), any()}. init(Req0, _State) -> case cowboy_req:method(Req0) of <<"GET">> -> list_following(Req0); _ -> handler_utils:send_error(Req0, 405, <<"Method not allowed">>) end. -spec trails() -> [map()]. trails() -> [ #{ path => <<"/v1/user/following">>, method => <<"GET">>, description => <<"List calendars the current user follows">>, tags => [<<"Calendars">>], responses => #{ 200 => #{ description => <<"Array of followed calendars">>, content => #{<<"application/json">> => #{schema => #{ type => array, items => #{type => object} }}} }, 401 => #{description => <<"Unauthorized">>} } } ]. -spec list_following(cowboy_req:req()) -> {ok, cowboy_req:req(), any()}. list_following(Req) -> case handler_utils:auth_user(Req) of {ok, UserId, Req1} -> {ok, Calendars} = logic_calendar_follow:list_following_calendars(UserId), Response = [ maps:put(following, true, handler_utils:calendar_to_json(C)) || C <- Calendars ], handler_utils:send_json(Req1, 200, Response); {error, Code, Message, Req1} -> handler_utils:send_error(Req1, Code, Message) end.