frenzy/lib/frenzy_web/controllers/group_controller.ex

50 lines
1.2 KiB
Elixir

defmodule FrenzyWeb.GroupController do
use FrenzyWeb, :controller
alias Frenzy.{Repo, Group, Feed}
alias FrenzyWeb.Router.Helpers, as: Routes
alias FrenzyWeb.Endpoint
import Ecto.Query
def index(conn, _params) do
groups = Repo.all(from group in Group, preload: [:feeds])
render(conn, "index.html", groups: groups)
end
def show(conn, %{"id" => id}) do
group = Repo.get(Group, id) |> Repo.preload(:feeds)
create_feed_changeset =
Feed.changeset(
%Feed{
group_id: id
},
%{}
)
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
changeset = Group.changeset(%Group{title: title}, %{})
{:ok, group} = Repo.insert(changeset)
redirect(conn, to: Routes.group_path(Endpoint, :index))
end
def delete(conn, %{"id" => id}) do
group = Repo.get(Group, id)
{:ok, _} = Repo.delete(group)
redirect(conn, to: Routes.group_path(Endpoint, :index))
end
end