Refactoring

This commit is contained in:
2026-07-16 19:28:11 +03:00
parent 06e86abf33
commit 7cff348d0d
2 changed files with 38 additions and 18 deletions
+28 -5
View File
@@ -1,17 +1,40 @@
defmodule NeonVertigoService.MessageProcessor do
def process_message(["auctions", "create"], payload, _properties) do
alias NeonVertigoService.Repo
def process_message(mqtt_pid, {"auctions", "create"}, payload, properties) do
changeset = Auction.changeset(%Auction{}, payload)
reply = case NeonVertigoService.Repo.insert(changeset) do
reply = case Repo.insert(changeset) do
{:ok, auction} ->
%{ok: true, gid: auction.gid}
{:error, changeset} ->
%{ok: false, gid: changeset |> Ecto.Changeset.get_field(:gid)}
end
{:reply, reply}
reply(reply, mqtt_pid, properties)
end
def process_message(_, _, _) do
:noreply
def process_message(_, _, _, _) do
{:ok, :unknown_message}
end
defp reply(data, mqtt_pid, orig_properties, message_properties \\ []) do
response_topic = orig_properties[:"Response-Topic"]
if is_nil(response_topic) do
{:ok, :no_response_topic}
else
send_mqtt(data, mqtt_pid, response_topic, message_properties)
end
end
defp send_mqtt(data, mqtt_pid, response_topic, message_properties \\ [])
defp send_mqtt(data, mqtt_pid, response_topic, message_properties) when is_binary(response_topic) and byte_size(response_topic) > 0 do
if String.valid?(response_topic) && String.length(response_topic) > 0 do
:emqtt.publish(mqtt_pid, response_topic, JSON.encode!(data), message_properties)
else
{:error, :invalid_topic}
end
end
defp send_mqtt(_, _, _, _), do: {:error, :invalid_topic}
end