Stage 6
This commit is contained in:
86
src/handlers/handler_banned_words.erl
Normal file
86
src/handlers/handler_banned_words.erl
Normal file
@@ -0,0 +1,86 @@
|
||||
-module(handler_banned_words).
|
||||
-include("records.hrl").
|
||||
|
||||
-export([init/2]).
|
||||
|
||||
init(Req, Opts) ->
|
||||
handle(Req, Opts).
|
||||
|
||||
handle(Req, _Opts) ->
|
||||
case cowboy_req:method(Req) of
|
||||
<<"GET">> -> list_banned_words(Req);
|
||||
<<"POST">> -> add_banned_word(Req);
|
||||
<<"DELETE">> -> remove_banned_word(Req);
|
||||
_ -> send_error(Req, 405, <<"Method not allowed">>)
|
||||
end.
|
||||
|
||||
%% GET /v1/admin/banned-words - список запрещённых слов
|
||||
list_banned_words(Req) ->
|
||||
case handler_auth:authenticate(Req) of
|
||||
{ok, AdminId, Req1} ->
|
||||
case logic_moderation:list_banned_words(AdminId) of
|
||||
{ok, Words} ->
|
||||
send_json(Req1, 200, Words);
|
||||
{error, access_denied} ->
|
||||
send_error(Req1, 403, <<"Admin access required">>);
|
||||
{error, _} ->
|
||||
send_error(Req1, 500, <<"Internal server error">>)
|
||||
end;
|
||||
{error, Code, Message, Req1} ->
|
||||
send_error(Req1, Code, Message)
|
||||
end.
|
||||
|
||||
%% POST /v1/admin/banned-words - добавить запрещённое слово
|
||||
add_banned_word(Req) ->
|
||||
case handler_auth:authenticate(Req) of
|
||||
{ok, AdminId, Req1} ->
|
||||
{ok, Body, Req2} = cowboy_req:read_body(Req1),
|
||||
try jsx:decode(Body, [return_maps]) of
|
||||
#{<<"word">> := Word} ->
|
||||
case logic_moderation:add_banned_word(AdminId, Word) of
|
||||
{ok, _} ->
|
||||
send_json(Req2, 201, #{word => Word, status => <<"added">>});
|
||||
{error, already_exists} ->
|
||||
send_error(Req2, 409, <<"Word already exists">>);
|
||||
{error, access_denied} ->
|
||||
send_error(Req2, 403, <<"Admin access required">>);
|
||||
{error, _} ->
|
||||
send_error(Req2, 500, <<"Internal server error">>)
|
||||
end;
|
||||
_ ->
|
||||
send_error(Req2, 400, <<"Missing 'word' field">>)
|
||||
catch
|
||||
_:_ ->
|
||||
send_error(Req2, 400, <<"Invalid JSON format">>)
|
||||
end;
|
||||
{error, Code, Message, Req1} ->
|
||||
send_error(Req1, Code, Message)
|
||||
end.
|
||||
|
||||
%% DELETE /v1/admin/banned-words - удалить запрещённое слово
|
||||
remove_banned_word(Req) ->
|
||||
case handler_auth:authenticate(Req) of
|
||||
{ok, AdminId, Req1} ->
|
||||
Word = cowboy_req:binding(word, Req1),
|
||||
case logic_moderation:remove_banned_word(AdminId, Word) of
|
||||
{ok, removed} ->
|
||||
send_json(Req1, 200, #{word => Word, status => <<"removed">>});
|
||||
{error, not_found} ->
|
||||
send_error(Req1, 404, <<"Word not found">>);
|
||||
{error, access_denied} ->
|
||||
send_error(Req1, 403, <<"Admin access required">>);
|
||||
{error, _} ->
|
||||
send_error(Req1, 500, <<"Internal server error">>)
|
||||
end;
|
||||
{error, Code, Message, Req1} ->
|
||||
send_error(Req1, Code, Message)
|
||||
end.
|
||||
|
||||
%% Вспомогательные функции
|
||||
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).
|
||||
Reference in New Issue
Block a user