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

build: Migrate views to Phoenix 1.7 #343

Closed
wants to merge 19 commits into from
Closed
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
2 changes: 1 addition & 1 deletion config/config.exs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ config :safira,
config :safira, SafiraWeb.Endpoint,
url: [host: "localhost"],
secret_key_base: "3KpMz5Dsmzm2+40c8Urp8UC0N95fFWvsHudtIUHjTv2yGsikjN3wIHPNPi3e+4xi",
render_errors: [view: SafiraWeb.ErrorView, accepts: ~w(json)],
render_errors: [view: SafiraWeb.ErrorJSON, accepts: ~w(json)],
pubsub_server: Safira.PubSub

config :safira, SafiraWeb.CORS,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
defmodule SafiraWeb.AttendeeController do
use SafiraWeb, controller: "1.6"
use SafiraWeb, :controller

alias Safira.Accounts
alias Safira.Accounts.Attendee
Expand Down
98 changes: 98 additions & 0 deletions lib/safira_web/controllers/attendee/attendee_json.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
defmodule SafiraWeb.AttendeeJSON do
@moduledoc false

alias Safira.Avatar

alias Safira.CV

alias SafiraWeb.BadgeJSON
alias SafiraWeb.PrizeJSON
alias SafiraWeb.RedeemableJSON

def index(%{attendees: attendees}) do
%{data: for(at <- attendees, do: multiple_attendees(%{attendee: at}))}
end

def show(%{attendee: at}) do
%{data: attendee(%{attendee: at})}
end

def show_simple(%{attendee: at}) do
%{data: attendee_simple(%{attendee: at})}
end

def manager_show(%{attendee: at}) do
%{data: manager_attendee(%{attendee: at})}
end

def attendee(%{attendee: at}) do
%{
id: at.id,
nickname: at.nickname,
name: at.name,
avatar: Avatar.url({at.avatar, at}, :original),
course: at.course_id,
cv: CV.url({at.cv, at}, :original),
badges: for(b <- at.badges, do: BadgeJSON.badge(%{badge: b})),
badge_count: at.badge_count,
token_balance: at.token_balance,
prizes: for(p <- at.prizes, do: PrizeJSON.prize_attendee(%{prize: p})),
entries: at.entries,
redeemables: for(r <- at.redeemables, do: RedeemableJSON.my_redeemables(%{redeemable: r}))
}
end

def multiple_attendees(%{attendee: at}) do
%{
id: at.id,
nickname: at.nickname,
name: at.name,
avatar: Avatar.url({at.avatar, at}, :original),
course: at.course_id,
cv: CV.url({at.cv, at}, :original),
badges: for(b <- at.badges, do: BadgeJSON.badge(%{badge: b})),
badge_count: at.badge_count,
token_balance: at.token_balance,
entries: at.entries
}
end

def manager_attendee(%{attendee: at}) do
%{
id: at.id,
nickname: at.nickname,
name: at.name,
email: at.user.email,
avatar: Avatar.url({at.avatar, at}, :original),
cv: CV.url({at.cv, at}, :original),
badges: for(b <- at.badges, do: BadgeJSON.badge(%{badge: b})),
badge_count: at.badge_count
}
end

def attendee_simple(%{attendee: at}) do
%{
id: at.id,
nickname: at.nickname,
name: at.name,
avatar: Avatar.url({at.avatar, at}, :original),
course: at.course_id,
cv: CV.url({at.cv, at}, :original),
token_balance: at.token_balance,
entries: at.entries
}
end

def attendee_no_cv(%{attendee: at}) do
%{
id: at.id,
nickname: at.nickname,
name: at.name,
avatar: Avatar.url({at.avatar, at}, :original),
course: at.course_id,
cv: nil,
token_balance: at.token_balance,
entries: at.entries
}
end
end
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
defmodule SafiraWeb.BadgeController do
use SafiraWeb, controller: "1.6"
use SafiraWeb, :controller

alias Safira.Accounts
alias Safira.Contest
Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,18 @@
defmodule SafiraWeb.BadgeView do
use SafiraWeb, :view
defmodule SafiraWeb.BadgeJSON do
@moduledoc false

alias Safira.Avatar
alias SafiraWeb.AttendeeJSON

alias SafiraWeb.AttendeeView
alias SafiraWeb.BadgeView

def render("index.json", %{badges: badges}) do
%{data: render_many(badges, BadgeView, "badge.json")}
def index(%{badges: badges}) do
%{data: for(b <- badges, do: badge(%{badge: b}))}
end

def render("show.json", %{badge: badge}) do
%{data: render_one(badge, BadgeView, "badge_show.json")}
def show(%{badge: badge}) do
%{data: badge_show(%{badge: badge})}
end

def render("badge.json", %{badge: badge}) do
def badge(%{badge: badge}) do
%{
id: badge.id,
name: badge.name,
Expand All @@ -27,7 +25,7 @@ defmodule SafiraWeb.BadgeView do
}
end

def render("badge_show.json", %{badge: badge}) do
def badge_show(%{badge: badge}) do
%{
id: badge.id,
name: badge.name,
Expand All @@ -37,7 +35,7 @@ defmodule SafiraWeb.BadgeView do
end: badge.end,
type: badge.type,
tokens: badge.tokens,
attendees: render_many(badge.attendees, AttendeeView, "attendee_simple.json")
attendees: for(at <- badge.attendees, do: AttendeeJSON.attendee_simple(%{attendee: at}))
}
end
end
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
defmodule SafiraWeb.BonusController do
use SafiraWeb, controller: "1.6"
use SafiraWeb, :controller

alias Safira.Accounts
alias Safira.Interaction
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
defmodule SafiraWeb.BonusView do
use SafiraWeb, :view
defmodule SafiraWeb.BonusJSON do
@moduledoc false

@token_bonus Application.compile_env!(:safira, :token_bonus)

def render("bonus.json", changes) do
def bonus(changes) do
attendee = Map.get(changes, :update_attendee)
bonus = Map.get(changes, :upsert_bonus)

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
defmodule SafiraWeb.BuyController do
use SafiraWeb, controller: "1.6"
use SafiraWeb, :controller

alias Safira.Accounts

Expand Down Expand Up @@ -48,4 +48,8 @@ defmodule SafiraWeb.BuyController do
end
end
end

def render(template, _assigns) do
%{errors: %{detail: Phoenix.Controller.status_message_from_template(template)}}
end
end
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
defmodule SafiraWeb.CompanyController do
use SafiraWeb, controller: "1.6"
use SafiraWeb, :controller

alias Safira.Accounts

Expand Down
32 changes: 32 additions & 0 deletions lib/safira_web/controllers/company/company_json.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
defmodule SafiraWeb.CompanyJSON do
@moduledoc false

alias SafiraWeb.AttendeeJSON

def index(%{companies: companies}) do
%{data: for(c <- companies, do: company_show(%{company: c}))}
end

def show(%{company: company}) do
%{data: company_show(%{company: company})}
end

def company_show(%{company: company}) do
%{
id: company.id,
name: company.name,
sponsorship: company.sponsorship,
channel_id: company.channel_id,
badge_id: company.badge_id,
has_cv_access: company.has_cv_access
}
end

def index_attendees(%{attendees: attendees, show_cv: show_cv}) do
if show_cv do
%{data: for(at <- attendees, do: AttendeeJSON.attendee_simple(%{attendee: at}))}
else
%{data: for(at <- attendees, do: AttendeeJSON.attendee_no_cv(%{attendee: at}))}
end
end
end
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
defmodule SafiraWeb.CVController do
use SafiraWeb, controller: "1.6"
use SafiraWeb, :controller

alias Safira.Accounts

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
defmodule SafiraWeb.DeliverPrizeController do
use SafiraWeb, controller: "1.6"
use SafiraWeb, :controller

alias Safira.Accounts

Expand Down
18 changes: 18 additions & 0 deletions lib/safira_web/controllers/deliver_prize/deliver_prize_json.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
defmodule SafiraWeb.DeliverPrizeJSON do
@moduledoc false

alias Safira.Avatar

def index(%{delivers: delivers}) do
%{data: for(d <- delivers, do: delivers(%{deliver_prize: d}))}
end

def delivers(%{deliver_prize: deliver}) do
%{
id: deliver.id,
name: deliver.name,
image: Avatar.url({deliver.avatar, deliver}, :original),
not_redeemed: deliver.not_redeemed
}
end
end
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
defmodule SafiraWeb.DeliverRedeemableController do
use SafiraWeb, controller: "1.6"
use SafiraWeb, :controller

alias Safira.Accounts

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
defmodule SafiraWeb.DeliverRedeemableJSON do
@moduledoc false
alias Safira.Avatar

def index(%{delivers: delivers}) do
%{data: for(d <- delivers, do: delivers_show(%{deliver_redeemable: d}))}
end

def delivers_show(%{deliver_redeemable: deliver}) do
%{
id: deliver.id,
name: deliver.name,
image: Avatar.url({deliver.img, deliver}, :original),
not_redeemed: deliver.not_redeemed
}
end
end
Original file line number Diff line number Diff line change
@@ -1,18 +1,5 @@
defmodule SafiraWeb.ErrorHelpers do
@moduledoc """
Conveniences for translating and building error messages.
"""

use Phoenix.HTML

@doc """
Generates tag for inlined form input errors.
"""
def error_tag(form, field) do
Enum.map(Keyword.get_values(form.errors, field), fn error ->
content_tag(:span, translate_error(error), class: "help-block")
end)
end
defmodule SafiraWeb.ErrorJSON do
@moduledoc false

@doc """
Translates an error message using gettext.
Expand Down Expand Up @@ -41,4 +28,28 @@ defmodule SafiraWeb.ErrorHelpers do
Gettext.dgettext(SafiraWeb.Gettext, "errors", msg, opts)
end
end

@doc """
Traverses and translates changeset errors.

See `Ecto.Changeset.traverse_errors/2` and
`SafiraWeb.ErrorHelpers.translate_error/1` for more details.
"""
def translate_errors(changeset) do
Ecto.Changeset.traverse_errors(changeset, &translate_error/1)
end

def error(%{changeset: changeset}) do
# When encoded, the changeset returns its errors
# as a JSON object. So we just pass it forward.
%{errors: translate_errors(changeset)}
end

def not_found(_) do
%{error: "Not found"}
end

def render(template, _assigns) do
%{errors: %{detail: Phoenix.Controller.status_message_from_template(template)}}
end
end
10 changes: 7 additions & 3 deletions lib/safira_web/controllers/fallback_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ defmodule SafiraWeb.FallbackController do

See `Phoenix.Controller.action_fallback/1` for more details.
"""
use SafiraWeb, controller: "1.6"
use SafiraWeb, :controller

alias SafiraWeb.ErrorJSON

def call(conn, {:error, :register_error}) do
conn
Expand Down Expand Up @@ -32,14 +34,16 @@ defmodule SafiraWeb.FallbackController do

def call(conn, {:error, %Ecto.Changeset{} = changeset}) do
conn
|> put_view(json: ErrorJSON)
|> put_status(:unprocessable_entity)
|> render(SafiraWeb.ChangesetView, "error.json", changeset: changeset)
|> render(:error, changeset: changeset)
end

def call(conn, {:error, :not_found}) do
conn
|> put_status(:not_found)
|> render(SafiraWeb.ErrorView, :"404")
|> put_view(json: ErrorJSON)
|> render(:not_found)
end

def call(conn, {:error, :no_permission}) do
Expand Down
2 changes: 1 addition & 1 deletion lib/safira_web/controllers/page_controller.ex
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
defmodule SafiraWeb.PageController do
use SafiraWeb, controller: "1.6"
use SafiraWeb, :controller

def index(conn, _params) do
conn
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
defmodule SafiraWeb.PasswordController do
use SafiraWeb, controller: "1.6"
use SafiraWeb, :controller

alias Safira.Accounts
alias Safira.Accounts.User
Expand Down
Loading
Loading