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

85 lines
2.5 KiB
Elixir
Raw Permalink Normal View History

defmodule PhilomenaWeb.Profile.ArtistLinkController do
2019-12-10 02:21:49 +01:00
use PhilomenaWeb, :controller
alias Philomena.ArtistLinks.ArtistLink
alias Philomena.ArtistLinks
2019-12-10 02:21:49 +01:00
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: ArtistLink,
2020-01-11 05:20:19 +01:00
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
artist_links =
ArtistLink
2019-12-10 02:21:49 +01:00
|> where(user_id: ^user.id)
|> Repo.all()
render(conn, "index.html", title: "Artist Links", artist_links: artist_links)
2019-12-10 02:21:49 +01:00
end
def new(conn, _params) do
changeset = ArtistLinks.change_artist_link(%ArtistLink{})
render(conn, "new.html", title: "New Artist Link", changeset: changeset)
2019-12-10 02:21:49 +01:00
end
def create(conn, %{"artist_link" => artist_link_params}) do
case ArtistLinks.create_artist_link(conn.assigns.user, artist_link_params) do
{:ok, artist_link} ->
2019-12-10 02:21:49 +01:00
conn
2020-01-11 05:20:19 +01:00
|> put_flash(
:info,
"Link submitted! Please put '#{artist_link.verification_code}' on your linked webpage now."
2020-01-11 05:20:19 +01:00
)
|> redirect(to: ~p"/profiles/#{conn.assigns.user}/artist_links/#{artist_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
artist_link = conn.assigns.artist_link
render(conn, "show.html", title: "Showing Artist Link", artist_link: artist_link)
2019-12-10 02:21:49 +01:00
end
def edit(conn, _params) do
changeset = ArtistLinks.change_artist_link(conn.assigns.artist_link)
2019-12-10 02:21:49 +01:00
render(conn, "edit.html", title: "Editing Artist Link", changeset: changeset)
2019-12-10 02:21:49 +01:00
end
def update(conn, %{"artist_link" => artist_link_params}) do
case ArtistLinks.update_artist_link(conn.assigns.artist_link, artist_link_params) do
{:ok, artist_link} ->
2019-12-10 02:21:49 +01:00
conn
|> put_flash(:info, "Link successfully updated.")
|> redirect(to: ~p"/profiles/#{conn.assigns.user}/artist_links/#{artist_link}")
2019-12-10 02:21:49 +01:00
{:error, changeset} ->
render(conn, "edit.html", changeset: changeset)
end
end
end