From 38e04277224059900e85f8184786a35088a104a4 Mon Sep 17 00:00:00 2001 From: daniel_sp Date: Sat, 16 Dec 2023 12:20:09 +0000 Subject: [PATCH] refactor ninja view --- .../controllers/ninja/ninja_controller.ex | 22 +++-- .../controllers/ninja/ninja_json.ex | 21 ++--- lib/bokken_web/views/auth_view.ex | 2 +- lib/bokken_web/views/enrollment_view.ex | 2 +- lib/bokken_web/views/lecture_view.ex | 2 +- .../controllers/ninja_json_test.exs | 91 +++++++++++++------ 6 files changed, 87 insertions(+), 53 deletions(-) diff --git a/lib/bokken_web/controllers/ninja/ninja_controller.ex b/lib/bokken_web/controllers/ninja/ninja_controller.ex index e6e3bd78..23b86550 100644 --- a/lib/bokken_web/controllers/ninja/ninja_controller.ex +++ b/lib/bokken_web/controllers/ninja/ninja_controller.ex @@ -14,20 +14,24 @@ 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, + 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 @@ -39,7 +43,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 @@ -52,18 +56,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 @@ -72,14 +76,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 diff --git a/lib/bokken_web/controllers/ninja/ninja_json.ex b/lib/bokken_web/controllers/ninja/ninja_json.ex index 5a55911b..933181a1 100644 --- a/lib/bokken_web/controllers/ninja/ninja_json.ex +++ b/lib/bokken_web/controllers/ninja/ninja_json.ex @@ -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), @@ -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 } diff --git a/lib/bokken_web/views/auth_view.ex b/lib/bokken_web/views/auth_view.ex index 56b7d32e..5a3ee124 100644 --- a/lib/bokken_web/views/auth_view.ex +++ b/lib/bokken_web/views/auth_view.ex @@ -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 diff --git a/lib/bokken_web/views/enrollment_view.ex b/lib/bokken_web/views/enrollment_view.ex index 919492ed..a328b447 100644 --- a/lib/bokken_web/views/enrollment_view.ex +++ b/lib/bokken_web/views/enrollment_view.ex @@ -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 diff --git a/lib/bokken_web/views/lecture_view.ex b/lib/bokken_web/views/lecture_view.ex index a2324756..3522b071 100644 --- a/lib/bokken_web/views/lecture_view.ex +++ b/lib/bokken_web/views/lecture_view.ex @@ -35,7 +35,7 @@ defmodule BokkenWeb.LectureView do defp ninja(lecture, current_user) do if Ecto.assoc_loaded?(lecture.ninja) do - %{ninja: NinjaJSON.ninja(%{ninja: lecture.ninja, current_user: current_user})} + %{ninja: NinjaJSON.show(%{ninja: lecture.ninja, current_user: current_user})} else %{ninja_id: lecture.ninja_id} end diff --git a/test/bokken_web/controllers/ninja_json_test.exs b/test/bokken_web/controllers/ninja_json_test.exs index fed9449a..9bc51376 100644 --- a/test/bokken_web/controllers/ninja_json_test.exs +++ b/test/bokken_web/controllers/ninja_json_test.exs @@ -5,38 +5,73 @@ 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 + 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 == + %{ + 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 + } + } + end - test "show" do - ninja = build(:ninja) - rendered_ninja = NinjaJSON.show(%{ninja: ninja}) + 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 == + %{ + 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 + } + } + 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