Расширена работа с бан словами #22

This commit is contained in:
2026-05-25 21:30:33 +03:00
parent c2d2e934d9
commit 01dbc6d6cb
4 changed files with 309 additions and 83 deletions
+44 -21
View File
@@ -24,28 +24,51 @@ add_banned_word(Word, AddedBy) ->
{aborted, Reason} -> {error, Reason}
end.
remove_banned_word(Word) ->
case mnesia:transaction(fun() ->
case mnesia:match_object(#banned_word{word = Word, _ = '_'}) of
[Rec] -> mnesia:delete_object(Rec), {ok, deleted};
[] -> mnesia:abort(not_found)
end
end) of
{atomic, {ok, deleted}} -> {ok, deleted};
{aborted, not_found} -> {error, not_found}
%% Поиск запрещённого слова по его тексту
-spec get_by_word(binary()) -> {ok, #banned_word{}} | {error, not_found}.
get_by_word(Word) ->
case mnesia:dirty_match_object(#banned_word{word = Word, _ = '_'}) of
[Record] -> {ok, Record};
[] -> {error, not_found}
end.
%% Проверка существования слова (по тексту)
word_exists(Word) ->
case get_by_word(Word) of
{ok, _} -> true;
_ -> false
end.
%% Обновление запрещённого слова
update_banned_word(OldWord, NewWord) ->
case mnesia:transaction(fun() ->
case mnesia:match_object(#banned_word{word = OldWord, _ = '_'}) of
[Rec] ->
Updated = Rec#banned_word{word = NewWord},
mnesia:write(Updated),
{ok, Updated};
[] ->
mnesia:abort(not_found)
end
end) of
{atomic, {ok, UpdatedRec}} -> {ok, UpdatedRec};
{aborted, not_found} -> {error, not_found}
case get_by_word(OldWord) of
{ok, Record} ->
case NewWord =:= OldWord orelse not word_exists(NewWord) of
true ->
NewRecord = Record#banned_word{word = NewWord},
F = fun() ->
%% Если первичный ключ — id, просто обновляем запись
mnesia:write(NewRecord),
ok
end,
case mnesia:transaction(F) of
{atomic, ok} -> {ok, NewRecord};
{aborted, Reason} -> {error, Reason}
end;
false ->
{error, already_exists}
end;
{error, _} = Error -> Error
end.
%% Удаление слова (используем get_by_word для надёжности)
remove_banned_word(Word) ->
case get_by_word(Word) of
{ok, Record} ->
F = fun() -> mnesia:delete({banned_word, Record#banned_word.id}), ok end,
case mnesia:transaction(F) of
{atomic, ok} -> {ok, Record};
{aborted, Reason} -> {error, Reason}
end;
{error, _} = Error -> Error
end.