Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: upgrade organizer controllers to 1.7 #196

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
defmodule BokkenWeb.OrganizerController do
use BokkenWeb, controller: "1.6"
use BokkenWeb, :controller

alias Bokken.Accounts
alias Bokken.Accounts.Organizer
Expand All @@ -8,29 +8,29 @@ defmodule BokkenWeb.OrganizerController do

def index(conn, _params) do
organizers = Accounts.list_organizers()
render(conn, "index.json", organizers: organizers)
render(conn, :index, organizers: organizers)
end

def create(conn, %{"organizer" => organizer_params}) when is_organizer(conn) do
with {:ok, %Organizer{} = organizer} <- Accounts.create_organizer(organizer_params) do
conn
|> put_status(:created)
|> put_resp_header("location", Routes.organizer_path(conn, :show, organizer))
|> render("show.json", organizer: organizer)
|> put_resp_header("location", ~p"/api/organizers/#{organizer}")
|> render(:show, organizer: organizer)
end
end

def show(conn, %{"id" => id}) do
organizer = Accounts.get_organizer!(id)
render(conn, "show.json", organizer: organizer)
render(conn, :show, organizer: organizer)
end

def update(conn, %{"id" => id, "organizer" => organizer_params})
when is_organizer(conn) do
organizer = Accounts.get_organizer!(id)

with {:ok, %Organizer{} = organizer} <- Accounts.update_organizer(organizer, organizer_params) do
render(conn, "show.json", organizer: organizer)
render(conn, :show, organizer: organizer)
end
end

Expand Down
18 changes: 18 additions & 0 deletions lib/bokken_web/controllers/organizer/organizer_json.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
defmodule BokkenWeb.OrganizerJSON do
def index(%{organizers: organizers}) do
%{data: for(organizer <- organizers, do: data(organizer))}
end

def show(%{organizer: organizer}) do
%{data: data(organizer)}
end

def data(organizer) do
%{
id: organizer.id,
champion: organizer.champion,
first_name: organizer.first_name,
last_name: organizer.last_name
}
end
end
4 changes: 2 additions & 2 deletions lib/bokken_web/views/auth_view.ex
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ defmodule BokkenWeb.AuthView do
alias BokkenWeb.GuardianView
alias BokkenWeb.MentorView
alias BokkenWeb.NinjaView
alias BokkenWeb.OrganizerView
alias BokkenWeb.OrganizerJSON

def render("me.json", %{user: %{registered: false} = user}) do
render_one(user, AuthView, "user.json")
Expand All @@ -30,7 +30,7 @@ defmodule BokkenWeb.AuthView do
end

def render("me.json", %{user: %{role: :organizer, organizer: organizer} = user}) do
render_one(organizer, OrganizerView, "organizer.json")
render_one(organizer, OrganizerJSON, "organizer.json")
|> Map.merge(render_one(user, AuthView, "user.json", current_user: user))
|> Map.put(:organizer_id, organizer.id)
end
Expand Down
22 changes: 0 additions & 22 deletions lib/bokken_web/views/organizer_view.ex

This file was deleted.

36 changes: 36 additions & 0 deletions test/bokken_web/controllers/organizer_json_test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
defmodule Bokken.OrganizerJSONTest do
use Bokken.DataCase

import Bokken.Factory
alias BokkenWeb.OrganizerJSON

test "data" do
organizer = build(:organizer)
rendered_organizer = OrganizerJSON.data(organizer)

assert rendered_organizer == %{
id: organizer.id,
champion: organizer.champion,
first_name: organizer.first_name,
last_name: organizer.last_name
}
end

test "show" do
organizer = build(:organizer)
rendered_organizer = OrganizerJSON.show(%{organizer: organizer})

assert rendered_organizer == %{
data: OrganizerJSON.data(organizer)
}
end

test "index" do
organizers = build_list(5, :organizer)
rendered_organizers = OrganizerJSON.index(%{organizers: organizers})

assert rendered_organizers == %{
data: Enum.map(organizers, &OrganizerJSON.data(&1))
}
end
end