Stage 3.2

This commit is contained in:
2026-04-20 11:42:03 +03:00
parent 4224da1a22
commit 491b4ae179
8 changed files with 403 additions and 4 deletions

View File

@@ -0,0 +1,90 @@
-module(handler_calendars).
-include("records.hrl").
-export([init/2]).
init(Req, Opts) ->
handle(Req, Opts).
handle(Req, _Opts) ->
case cowboy_req:method(Req) of
<<"POST">> -> create_calendar(Req);
<<"GET">> -> list_calendars(Req);
_ -> send_error(Req, 405, <<"Method not allowed">>)
end.
%% POST /v1/calendars - создание календаря
create_calendar(Req) ->
case handler_auth:authenticate(Req) of
{ok, UserId, Req1} ->
{ok, Body, Req2} = cowboy_req:read_body(Req1),
try jsx:decode(Body, [return_maps]) of
Decoded when is_map(Decoded) ->
case Decoded of
#{<<"title">> := Title} ->
Description = maps:get(<<"description">>, Decoded, <<"">>),
case logic_calendar:create_calendar(UserId, Title, Description) of
{ok, Calendar} ->
Response = calendar_to_json(Calendar),
send_json(Req2, 201, Response);
{error, user_inactive} ->
send_error(Req2, 403, <<"User account is not active">>);
{error, _} ->
send_error(Req2, 500, <<"Internal server error">>)
end;
_ ->
send_error(Req2, 400, <<"Missing required field: title">>)
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.
%% GET /v1/calendars - список календарей
list_calendars(Req) ->
case handler_auth:authenticate(Req) of
{ok, UserId, Req1} ->
case logic_calendar:list_calendars(UserId) of
{ok, Calendars} ->
Response = [calendar_to_json(C) || C <- Calendars],
send_json(Req1, 200, Response);
{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).
send_error(Req, Status, Message) ->
Body = jsx:encode(#{error => Message}),
cowboy_req:reply(Status, #{<<"content-type">> => <<"application/json">>}, Body, Req).