diff --git a/lib/safira/accounts/accounts.ex b/lib/safira/accounts/accounts.ex index 1604e3ae..120d19f7 100644 --- a/lib/safira/accounts/accounts.ex +++ b/lib/safira/accounts/accounts.ex @@ -88,8 +88,8 @@ defmodule Safira.Accounts do |> Repo.preload(:prizes) end - def get_attendee_with_badge_count!(id) do - case get_attendee(id) do + def get_attendee_with_badge_count_by_id!(id) do + case get_attendee!(id) do nil -> nil @@ -102,12 +102,25 @@ defmodule Safira.Accounts do end end - def get_attendee(id) do - Repo.get(Attendee, id) + def get_attendee_with_badge_count_by_username!(username) do + case get_attendee_by_username!(username) do + nil -> + nil + + %Attendee{} = attendee -> + badges = + attendee.badges + |> Enum.filter(&(&1.type != 0)) + + Map.put(attendee, :badge_count, length(badges)) + end + end + + def get_attendee_by_username!(username) do + Repo.get_by!(Attendee, nickname: username) |> Repo.preload(:badges) |> Repo.preload(:user) |> Repo.preload(:prizes) - |> Repo.preload(:course) end def get_attendee_by_discord_association_code(discord_association_code) do diff --git a/lib/safira/admin/accounts.ex b/lib/safira/admin/accounts.ex index 2f89a5c8..9e2c2547 100644 --- a/lib/safira/admin/accounts.ex +++ b/lib/safira/admin/accounts.ex @@ -104,6 +104,12 @@ defmodule Safira.Admin.Accounts do |> Repo.preload(:badges) end + def get_attendee_by_username!(username) do + Repo.get_by!(Attendee, nickname: username) + |> Repo.preload(:user) + |> Repo.preload(:badges) + end + @doc """ Creates a attendee. diff --git a/lib/safira_web/controllers/attendee_controller.ex b/lib/safira_web/controllers/attendee_controller.ex index dc372b2a..5fb7f735 100644 --- a/lib/safira_web/controllers/attendee_controller.ex +++ b/lib/safira_web/controllers/attendee_controller.ex @@ -10,6 +10,7 @@ defmodule SafiraWeb.AttendeeController do alias Safira.Store action_fallback SafiraWeb.FallbackController + @uuid_regex ~r/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i def index(conn, _params) do attendees = Accounts.list_active_attendees() @@ -17,7 +18,14 @@ defmodule SafiraWeb.AttendeeController do end def show(conn, %{"id" => id}) do - attendee = Accounts.get_attendee_with_badge_count!(id) + + attendee = + case String.match?(id, @uuid_regex) do + true -> + Accounts.get_attendee_with_badge_count_by_id!(id) + _ -> + Accounts.get_attendee_by_username!(id) + end cond do is_nil(attendee) ->