2019-11-29 20:29:01 +01:00
|
|
|
defmodule PhilomenaWeb.Forum.SubscriptionController do
|
|
|
|
use PhilomenaWeb, :controller
|
|
|
|
|
|
|
|
alias Philomena.Forums.Forum
|
|
|
|
alias Philomena.Forums
|
|
|
|
|
|
|
|
plug PhilomenaWeb.CanaryMapPlug, create: :show, delete: :show
|
2020-01-11 05:20:19 +01:00
|
|
|
|
|
|
|
plug :load_and_authorize_resource,
|
|
|
|
model: Forum,
|
|
|
|
id_name: "forum_id",
|
|
|
|
id_field: "short_name",
|
|
|
|
persisted: true
|
2019-11-29 20:29:01 +01:00
|
|
|
|
|
|
|
def create(conn, _params) do
|
|
|
|
forum = conn.assigns.forum
|
|
|
|
user = conn.assigns.current_user
|
|
|
|
|
|
|
|
case Forums.create_subscription(forum, user) do
|
|
|
|
{:ok, _subscription} ->
|
|
|
|
render(conn, "_subscription.html", forum: forum, watching: true, layout: false)
|
|
|
|
|
|
|
|
{:error, _changeset} ->
|
|
|
|
render(conn, "_error.html", layout: false)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def delete(conn, _params) do
|
|
|
|
forum = conn.assigns.forum
|
|
|
|
user = conn.assigns.current_user
|
|
|
|
|
|
|
|
case Forums.delete_subscription(forum, user) do
|
|
|
|
{:ok, _subscription} ->
|
|
|
|
render(conn, "_subscription.html", forum: forum, watching: false, layout: false)
|
|
|
|
|
|
|
|
{:error, _changeset} ->
|
|
|
|
render(conn, "_error.html", layout: false)
|
|
|
|
end
|
|
|
|
end
|
2020-01-11 05:20:19 +01:00
|
|
|
end
|