Skip to content

Commit

Permalink
feat: normalize profile mentions
Browse files Browse the repository at this point in the history
  • Loading branch information
MarioRodrigues10 committed Dec 17, 2023
1 parent 8e3ed94 commit 1f93f8e
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 6 deletions.
23 changes: 18 additions & 5 deletions lib/safira/accounts/accounts.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
Expand Down
6 changes: 6 additions & 0 deletions lib/safira/admin/accounts.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
10 changes: 9 additions & 1 deletion lib/safira_web/controllers/attendee_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,22 @@ 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()
render(conn, "index.json", attendees: attendees)
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) ->
Expand Down

0 comments on commit 1f93f8e

Please sign in to comment.