Skip to content

Commit

Permalink
refactor ninja view
Browse files Browse the repository at this point in the history
  • Loading branch information
danielsp45 committed Dec 16, 2023
1 parent 0444df5 commit d1bd729
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 52 deletions.
18 changes: 9 additions & 9 deletions lib/bokken_web/controllers/ninja/ninja_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,20 @@ defmodule BokkenWeb.NinjaController do
when is_map_key(params, "badge_id")
when is_map_key(params, "event_id") do
ninjas = Accounts.list_ninjas()
render(conn, :index, ninjas: ninjas)
render(conn, :index, %{ninjas: ninjas, current_user: conn.assigns.current_user})
end

def index(conn, _params) when is_guardian(conn) or is_organizer(conn) do
guardian_id = conn.assigns.current_user.guardian.id
guardian = Accounts.get_guardian!(guardian_id, [:ninjas])
render(conn, :index, ninjas: guardian.ninjas)
render(conn, :index, %{ninjas: guardian.ninjas. ninjas, current_user: conn.assigns.current_user})
end

def create(conn, %{"event_id" => event_id, "ninja_id" => ninja_id})
when is_guardian(conn) or is_organizer(conn) do
with {:ok, %Event{} = event, %Lecture{} = _lecture} <-
Accounts.register_ninja_in_event(event_id, ninja_id) do
render(conn, :index, ninjas: event.ninjas)
render(conn, :index, %{ninjas: event.ninjas, current_user: conn.assigns.current_user})
end
end

Expand All @@ -39,7 +39,7 @@ defmodule BokkenWeb.NinjaController do
conn
|> put_status(:created)
|> put_resp_header("location", ~p"/api/teams/#{ninja}")
|> render(:show, ninja: ninja)
|> render(:show, %{ninja: ninja, current_user: conn.assigns.current_user})
end
end

Expand All @@ -52,18 +52,18 @@ defmodule BokkenWeb.NinjaController do
conn
|> put_status(:created)
|> put_resp_header("location", ~p"/api/ninjas/#{ninja}")
|> render(:show, ninja: ninja)
|> render(:show, %{ninja: ninja, current_user: conn.assigns.current_user})
end
end

def show(conn, %{"id" => id}) do
ninja = Accounts.get_ninja!(id, [:skills])
render(conn, :show, ninja: ninja)
render(conn, :show, %{ninja: ninja, current_user: conn.assigns.current_user})
end

def show(conn, %{"discord_id" => discord_id}) do
with {:ok, %Ninja{} = ninja} <- Accounts.get_ninja_by_discord(discord_id) do
render(conn, :show, ninja: ninja)
render(conn, :show, %{ninja: ninja, current_user: conn.assigns.current_user})
end
end

Expand All @@ -72,14 +72,14 @@ defmodule BokkenWeb.NinjaController do
ninja = Accounts.get_ninja!(id)

with {:ok, %Ninja{} = ninja} <- Accounts.update_ninja(ninja, ninja_params) do
render(conn, :show, ninja: ninja)
render(conn, :show, %{ninja: ninja, current_user: conn.assigns.current_user})
end
end

def update(conn, %{"discord_id" => discord_id, "ninja" => ninja_params}) do
with {:ok, %Ninja{} = ninja} <- Accounts.get_ninja_by_discord(discord_id),
{:ok, %Ninja{} = ninja} <- Accounts.update_ninja(ninja, ninja_params) do
render(conn, :show, ninja: ninja)
render(conn, :show, %{ninja: ninja, current_user: conn.assigns.current_user})
end
end

Expand Down
21 changes: 8 additions & 13 deletions lib/bokken_web/controllers/ninja/ninja_json.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,15 @@ defmodule BokkenWeb.NinjaJSON do
alias Bokken.Uploaders.Avatar
alias BokkenWeb.SkillJSON

def index(%{ninjas: ninjas}) do
%{data: for(ninja <- ninjas, do: data(ninja))}
def index(%{ninjas: ninjas, current_user: current_user}) do
%{data: for(ninja <- ninjas, do: data(ninja, current_user))}
end

def show(%{ninja: ninja}) do
%{data: data(ninja)}
def show(%{ninja: ninja, current_user: current_user}) do
%{data: data(ninja, current_user)}
end

def ninja(%{ninja: ninja, current_user: current_user}) do
data(ninja)
|> Map.merge(personal(ninja, current_user))
|> Map.merge(sensitive(ninja, current_user))
end

def data(ninja) do
def data(ninja, current_user) do
%{
id: ninja.id,
photo: Avatar.url({ninja.photo, ninja}, :thumb),
Expand All @@ -25,14 +19,15 @@ defmodule BokkenWeb.NinjaJSON do
belt: ninja.belt,
socials: ninja.socials,
since: ninja.inserted_at,
birthday: ninja.birthday,
guardian_id: ninja.guardian_id
}
|> Map.merge(personal(ninja, current_user))
|> Map.merge(sensitive(ninja, current_user))
|> Map.merge(skills(ninja))
end

defp personal(ninja, current_user)
when current_user.role == :organizer or current_user.id == ninja.id do
when current_user.role in [:organizer, :guardian] or current_user.id == ninja.id do
%{
birthday: ninja.birthday
}
Expand Down
2 changes: 1 addition & 1 deletion lib/bokken_web/views/auth_view.ex
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ defmodule BokkenWeb.AuthView do
end

def render("me.json", %{user: %{role: :ninja, ninja: ninja} = user}) do
NinjaJSON.ninja(%{ninja: ninja, current_user: user})
NinjaJSON.data(ninja, user)
|> Map.merge(render_one(user, AuthView, "user.json"))
|> Map.put(:ninja_id, ninja.id)
end
Expand Down
2 changes: 1 addition & 1 deletion lib/bokken_web/views/enrollment_view.ex
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ defmodule BokkenWeb.EnrollmentView do

defp ninja(enrollment, current_user) do
if Ecto.assoc_loaded?(enrollment.ninja) do
%{ninja: NinjaJSON.ninja(%{ninja: enrollment.ninja, current_user: current_user})}
%{ninja: NinjaJSON.show(%{ninja: enrollment.ninja, current_user: current_user})}
else
%{ninja_id: enrollment.ninja_id}
end
Expand Down
90 changes: 62 additions & 28 deletions test/bokken_web/controllers/ninja_json_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -5,38 +5,72 @@ defmodule Bokken.NinjaJSONTest do
alias Bokken.Uploaders.Avatar
alias BokkenWeb.NinjaJSON

test "data" do
ninja = build(:ninja)
rendered_ninja = NinjaJSON.data(ninja)

assert rendered_ninja == %{
id: ninja.id,
photo: Avatar.url({ninja.photo, ninja}),
first_name: ninja.first_name,
last_name: ninja.last_name,
belt: ninja.belt,
socials: ninja.socials,
since: ninja.inserted_at,
birthday: ninja.birthday,
guardian_id: ninja.guardian_id
}
end

test "show" do
ninja = build(:ninja)
rendered_ninja = NinjaJSON.show(%{ninja: ninja})
describe "login as guardian" do
setup do
user = build(:user, role: :guardian)
guardian = build(:guardian, user: user)
ninja = build(:ninja, guardian: guardian)
{:ok, ninja: ninja, current_user: user}
end

test "show", %{ninja: ninja, current_user: user} do
rendered_ninja = NinjaJSON.show(%{ninja: ninja, current_user: user})

assert rendered_ninja ==

Check warning on line 20 in test/bokken_web/controllers/ninja_json_test.exs

View workflow job for this annotation

GitHub Actions / OTP 25.x / Elixir 1.14.x

There should be no trailing white-space at the end of a line.
%{data: %{
id: ninja.id,
photo: Avatar.url({ninja.photo, ninja}),
first_name: ninja.first_name,
last_name: ninja.last_name,
belt: ninja.belt,
socials: ninja.socials,
since: ninja.inserted_at,
birthday: ninja.birthday,
guardian_id: ninja.guardian_id
}

Check warning on line 31 in test/bokken_web/controllers/ninja_json_test.exs

View workflow job for this annotation

GitHub Actions / OTP 25.x / Elixir 1.14.x

There should be no trailing white-space at the end of a line.
}
end

test "index", %{ninja: ninja, current_user: user} do
ninjas = build_list(5, :ninja, guardian: ninja.guardian)
rendered_ninjas = NinjaJSON.index(%{ninjas: ninjas, current_user: user})

assert rendered_ninja == %{
data: NinjaJSON.data(ninja)
}
assert 5 == Enum.count(rendered_ninjas[:data])
end
end

test "index" do
ninjas = build_list(5, :ninja)
rendered_ninjas = NinjaJSON.index(%{ninjas: ninjas})
describe "login as mentor" do
setup do
user = build(:user, role: :mentor)
ninja = build(:ninja)
{:ok, ninja: ninja, current_user: user}
end

test "show", %{ninja: ninja, current_user: user} do
rendered_ninja = NinjaJSON.show(%{ninja: ninja, current_user: user})

assert rendered_ninja ==

Check warning on line 53 in test/bokken_web/controllers/ninja_json_test.exs

View workflow job for this annotation

GitHub Actions / OTP 25.x / Elixir 1.14.x

There should be no trailing white-space at the end of a line.
%{data: %{
id: ninja.id,
photo: Avatar.url({ninja.photo, ninja}),
first_name: ninja.first_name,
last_name: ninja.last_name,
belt: ninja.belt,
socials: ninja.socials,
since: ninja.inserted_at,
birthday: ninja.birthday,
notes: nil,
guardian_id: ninja.guardian_id
}

Check warning on line 65 in test/bokken_web/controllers/ninja_json_test.exs

View workflow job for this annotation

GitHub Actions / OTP 25.x / Elixir 1.14.x

There should be no trailing white-space at the end of a line.
}
end

test "index", %{ninja: ninja, current_user: user} do
ninjas = build_list(5, :ninja, guardian: ninja.guardian)
rendered_ninjas = NinjaJSON.index(%{ninjas: ninjas, current_user: user})

assert rendered_ninjas == %{
data: Enum.map(ninjas, &NinjaJSON.data(&1))
}
assert 5 == Enum.count(rendered_ninjas[:data])
end
end
end

0 comments on commit d1bd729

Please sign in to comment.