philomena/lib/philomena_web/controllers/profile/user_link_controller.ex

85 lines
2.4 KiB
Elixir
Raw Normal View History

2019-12-10 02:21:49 +01:00
defmodule PhilomenaWeb.Profile.UserLinkController do
use PhilomenaWeb, :controller
alias Philomena.UserLinks.UserLink
alias Philomena.UserLinks
alias Philomena.Users.User
alias Philomena.Repo
import Ecto.Query
plug PhilomenaWeb.FilterBannedUsersPlug when action in [:new, :create]
2020-01-11 05:20:19 +01:00
plug :load_and_authorize_resource,
model: UserLink,
only: [:show, :edit, :update],
preload: [:user, :tag, :contacted_by_user]
2019-12-10 02:21:49 +01:00
plug PhilomenaWeb.CanaryMapPlug,
index: :create_links,
new: :create_links,
create: :create_links,
show: :create_links,
edit: :edit_links,
update: :edit_links
2020-01-11 05:20:19 +01:00
plug :load_and_authorize_resource,
model: User,
id_field: "slug",
id_name: "profile_id",
persisted: true
2019-12-10 02:21:49 +01:00
def index(conn, _params) do
user = conn.assigns.current_user
2020-01-11 05:20:19 +01:00
2019-12-10 02:21:49 +01:00
user_links =
UserLink
|> where(user_id: ^user.id)
|> Repo.all()
render(conn, "index.html", title: "Artist Links", user_links: user_links)
2019-12-10 02:21:49 +01:00
end
def new(conn, _params) do
changeset = UserLinks.change_user_link(%UserLink{})
render(conn, "new.html", title: "New Artist Link", changeset: changeset)
2019-12-10 02:21:49 +01:00
end
def create(conn, %{"user_link" => user_link_params}) do
case UserLinks.create_user_link(conn.assigns.user, user_link_params) do
{:ok, user_link} ->
conn
2020-01-11 05:20:19 +01:00
|> put_flash(
:info,
"Link submitted! Please put '#{user_link.verification_code}' on your linked webpage now."
)
|> redirect(to: Routes.profile_user_link_path(conn, :show, conn.assigns.user, user_link))
2019-12-10 02:21:49 +01:00
{:error, %Ecto.Changeset{} = changeset} ->
render(conn, "new.html", changeset: changeset)
end
end
def show(conn, _params) do
user_link = conn.assigns.user_link
render(conn, "show.html", title: "Showing Artist Link", user_link: user_link)
2019-12-10 02:21:49 +01:00
end
def edit(conn, _params) do
changeset = UserLinks.change_user_link(conn.assigns.user_link)
render(conn, "edit.html", title: "Editing Artist Link", changeset: changeset)
2019-12-10 02:21:49 +01:00
end
def update(conn, %{"user_link" => user_link_params}) do
case UserLinks.update_user_link(conn.assigns.user_link, user_link_params) do
{:ok, user_link} ->
conn
|> put_flash(:info, "Link successfully updated.")
|> redirect(to: Routes.profile_user_link_path(conn, :show, conn.assigns.user, user_link))
{:error, changeset} ->
render(conn, "edit.html", changeset: changeset)
end
end
end