From 0cbe93f8dba31d0562f04095ae4a8d4df323f0ef Mon Sep 17 00:00:00 2001 From: Graham McIntire Date: Thu, 23 Apr 2026 14:49:57 -0500 Subject: [PATCH] refactor: MobileChannel callsign search pattern via function dispatch build_search_pattern/1 picks between SQL-wildcard (when the query contains `*`) and prefix-match (SSID-friendly) forms via two function heads instead of an inline `if`. The transform is now isolated and obviously boolean-dispatched at the call site. --- lib/aprsme_web/channels/mobile_channel.ex | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/lib/aprsme_web/channels/mobile_channel.ex b/lib/aprsme_web/channels/mobile_channel.ex index d6b026a..6f2cce5 100644 --- a/lib/aprsme_web/channels/mobile_channel.ex +++ b/lib/aprsme_web/channels/mobile_channel.ex @@ -353,6 +353,16 @@ defmodule AprsmeWeb.MobileChannel do defp ensure_float(_value), do: nil + # Dispatch on wildcard presence. `*` is the user-facing wildcard; SQL LIKE + # uses `%`. Without a wildcard, match callsigns that start with the query + # so SSID searches like "K5ABC" match "K5ABC-9". + defp build_search_pattern(query) do + build_search_pattern_for(String.contains?(query, "*"), query) + end + + defp build_search_pattern_for(true, query), do: String.replace(query, "*", "%") + defp build_search_pattern_for(false, query), do: "#{query}%" + defp ensure_integer(value, _default) when is_integer(value), do: value defp ensure_integer(value, default) when is_binary(value) do @@ -471,16 +481,7 @@ defmodule AprsmeWeb.MobileChannel do Logger.info("Searching for callsign: #{query}") - # Build search pattern - support wildcard with * - pattern = - if String.contains?(query, "*") do - # Convert * to SQL % wildcard - String.replace(query, "*", "%") - else - # If no wildcard, search for exact match or with SSID - "#{query}%" - end - + pattern = build_search_pattern(query) Logger.info("Search pattern: #{pattern}") # Query for matching callsigns - optimized version