frenzy/lib/frenzy_web/controllers/group_controller.ex

148 lines
3.7 KiB
Elixir
Raw Normal View History

2019-03-21 23:21:56 +00:00
defmodule FrenzyWeb.GroupController do
use FrenzyWeb, :controller
2020-06-02 02:38:27 +00:00
alias Frenzy.{Repo, Group, Feed, Item, Paginator}
2019-03-21 23:21:56 +00:00
alias FrenzyWeb.Router.Helpers, as: Routes
alias FrenzyWeb.Endpoint
2020-06-02 02:38:27 +00:00
import Ecto.Query
2019-03-23 23:42:38 +00:00
plug :user_owns_group
defp user_owns_group(%Plug.Conn{path_params: %{"id" => id}} = conn, _opts) do
user = conn.assigns[:user]
group = Repo.get(Group, id)
if Enum.any?(user.groups, fn g -> g.id == group.id end) do
conn
|> assign(:group, group)
else
conn
|> put_flash(:error, "You do not have permission to acess that resource.")
|> redirect(to: Routes.group_path(Endpoint, :index))
|> halt()
end
end
defp user_owns_group(conn, _opts), do: conn
2019-03-21 23:21:56 +00:00
def index(conn, _params) do
2019-03-23 23:42:38 +00:00
groups = conn.assigns[:user].groups
2019-03-21 23:21:56 +00:00
render(conn, "index.html", groups: groups)
end
2019-03-23 23:42:38 +00:00
def show(conn, _params) do
group = conn.assigns[:group] |> Repo.preload(:feeds)
2019-03-21 23:21:56 +00:00
create_feed_changeset =
Feed.changeset(
%Feed{
2019-03-23 23:42:38 +00:00
group_id: group.id
2019-03-21 23:21:56 +00:00
},
%{}
)
render(conn, "show.html", %{
group: group,
create_feed_changeset: create_feed_changeset
})
end
def new(conn, _params) do
changeset = Group.changeset(%Group{}, %{})
render(conn, "new.html", changeset: changeset)
end
def create(conn, %{"group" => %{"title" => title}}) do
2019-03-23 23:42:38 +00:00
user = conn.assigns[:user]
2019-08-31 19:13:29 +00:00
title = title |> String.trim()
2019-03-23 23:42:38 +00:00
2019-08-31 19:13:29 +00:00
if String.length(title) > 0 do
changeset =
Ecto.build_assoc(user, :groups, %{
title: title
})
2019-03-21 23:21:56 +00:00
2019-08-31 19:13:29 +00:00
{:ok, group} = Repo.insert(changeset)
2019-03-21 23:21:56 +00:00
2019-08-31 19:13:29 +00:00
redirect(conn, to: Routes.group_path(Endpoint, :show, group.id))
else
conn
|> put_flash(:error, "Group title must not be empty")
|> redirect(to: Routes.group_path(Endpoint, :new))
end
2019-03-21 23:21:56 +00:00
end
2020-06-01 22:51:45 +00:00
def edit(conn, _params) do
group = conn.assigns[:group]
edit_changeset = Group.changeset(group, %{})
render(conn, "edit.html", %{
group: group,
changeset: edit_changeset
})
end
def update(conn, %{"group" => group_params}) do
group = conn.assigns[:group]
changeset = Group.changeset(group, group_params)
{:ok, group} = Repo.update(changeset)
redirect(conn, to: Routes.group_path(Endpoint, :show, group.id))
end
2019-03-23 23:42:38 +00:00
def delete(conn, _params) do
group = conn.assigns[:group]
2019-03-21 23:21:56 +00:00
{:ok, _} = Repo.delete(group)
redirect(conn, to: Routes.group_path(Endpoint, :index))
end
2020-06-02 02:38:27 +00:00
def read(conn, params) do
group = conn.assigns[:group] |> Repo.preload(:feeds)
feed_ids = Enum.map(group.feeds, fn feed -> feed.id end)
items =
Item
|> where([i], not i.tombstone and i.feed_id in ^feed_ids)
2020-06-02 02:38:27 +00:00
|> Paginator.paginate(params)
|> limit(50)
|> preload(:feed)
|> Repo.all()
|> Enum.sort_by(fn item -> item.date end, fn a, b ->
DateTime.compare(a, b) == :gt
end)
first_item = List.first(items)
last_item = List.last(items)
prev_page_path =
if !is_nil(first_item) &&
Repo.exists?(
from i in Item,
where: not i.tombstone and i.date > ^first_item.date and i.feed_id in ^feed_ids
) do
current_path(conn, %{after: first_item.id})
else
nil
end
next_page_path =
if !is_nil(last_item) &&
Repo.exists?(
from i in Item,
where: not i.tombstone and i.date < ^last_item.date and i.feed_id in ^feed_ids
) do
current_path(conn, %{before: last_item.id})
else
nil
end
2020-06-02 02:38:27 +00:00
render(conn, "read.html", %{
group: group,
items: items,
prev_page_path: prev_page_path,
next_page_path: next_page_path
2020-06-02 02:38:27 +00:00
})
end
2019-03-21 23:21:56 +00:00
end