-module(handler_calendar_by_id). -include("records.hrl"). -export([init/2]). init(Req, Opts) -> handle(Req, Opts). handle(Req, _Opts) -> case cowboy_req:method(Req) of <<"GET">> -> get_calendar(Req); <<"PUT">> -> update_calendar(Req); <<"DELETE">> -> delete_calendar(Req); _ -> send_error(Req, 405, <<"Method not allowed">>) end. %% GET /v1/calendars/:id - получение календаря get_calendar(Req) -> case handler_auth:authenticate(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); {error, access_denied} -> send_error(Req1, 403, <<"Access denied">>); {error, not_found} -> send_error(Req1, 404, <<"Calendar not found">>); {error, _} -> send_error(Req1, 500, <<"Internal server error">>) end; {error, Code, Message, Req1} -> send_error(Req1, Code, Message) end. %% PUT /v1/calendars/:id - обновление календаря update_calendar(Req) -> case handler_auth:authenticate(Req) of {ok, UserId, Req1} -> CalendarId = cowboy_req:binding(id, Req1), {ok, Body, Req2} = cowboy_req:read_body(Req1), try jsx:decode(Body, [return_maps]) of UpdatesMap when is_map(UpdatesMap) -> 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); {error, access_denied} -> send_error(Req2, 403, <<"Access denied">>); {error, not_found} -> send_error(Req2, 404, <<"Calendar not found">>); {error, _} -> send_error(Req2, 500, <<"Internal server error">>) end; _ -> send_error(Req2, 400, <<"Invalid JSON">>) catch _:_ -> send_error(Req2, 400, <<"Invalid JSON format">>) end; {error, Code, Message, Req1} -> send_error(Req1, Code, Message) end. %% DELETE /v1/calendars/:id - удаление календаря delete_calendar(Req) -> case handler_auth:authenticate(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">>}); {error, access_denied} -> send_error(Req1, 403, <<"Access denied">>); {error, not_found} -> send_error(Req1, 404, <<"Calendar not found">>); {error, _} -> 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, []}.