From eb0ebeba9166ad0cef1ed6a4e3eae07b95358d9c Mon Sep 17 00:00:00 2001 From: Shadowfacts Date: Thu, 21 Mar 2019 19:21:56 -0400 Subject: [PATCH] Initial groups implementation --- lib/frenzy/feed.ex | 2 + lib/frenzy/group.ex | 19 +++++++ lib/frenzy_web/controllers/feed_controller.ex | 51 ++++++------------- .../controllers/group_controller.ex | 49 ++++++++++++++++++ lib/frenzy_web/router.ex | 6 +-- lib/frenzy_web/templates/feed/index.html.eex | 14 ----- lib/frenzy_web/templates/feed/new.html.eex | 9 ---- lib/frenzy_web/templates/group/index.html.eex | 17 +++++++ lib/frenzy_web/templates/group/new.html.eex | 9 ++++ lib/frenzy_web/templates/group/show.html.eex | 29 +++++++++++ lib/frenzy_web/views/group_view.ex | 3 ++ .../20190321221841_create_groups.exs | 11 ++++ .../20190321223329_feeds_add_group.exs | 9 ++++ 13 files changed, 167 insertions(+), 61 deletions(-) create mode 100644 lib/frenzy/group.ex create mode 100644 lib/frenzy_web/controllers/group_controller.ex delete mode 100644 lib/frenzy_web/templates/feed/index.html.eex delete mode 100644 lib/frenzy_web/templates/feed/new.html.eex create mode 100644 lib/frenzy_web/templates/group/index.html.eex create mode 100644 lib/frenzy_web/templates/group/new.html.eex create mode 100644 lib/frenzy_web/templates/group/show.html.eex create mode 100644 lib/frenzy_web/views/group_view.ex create mode 100644 priv/repo/migrations/20190321221841_create_groups.exs create mode 100644 priv/repo/migrations/20190321223329_feeds_add_group.exs diff --git a/lib/frenzy/feed.ex b/lib/frenzy/feed.ex index 82bb9f2..3838d2f 100644 --- a/lib/frenzy/feed.ex +++ b/lib/frenzy/feed.ex @@ -21,6 +21,8 @@ defmodule Frenzy.Feed do field :title, :string field :filter_enabled, :boolean + belongs_to :group, Frenzy.Group + has_many :items, Frenzy.Item, on_delete: :delete_all has_one :filter, Frenzy.Filter, on_delete: :delete_all diff --git a/lib/frenzy/group.ex b/lib/frenzy/group.ex new file mode 100644 index 0000000..95d5018 --- /dev/null +++ b/lib/frenzy/group.ex @@ -0,0 +1,19 @@ +defmodule Frenzy.Group do + use Ecto.Schema + import Ecto.Changeset + + schema "groups" do + field :title, :string + + has_many :feeds, Frenzy.Feed, on_delete: :delete_all + + timestamps() + end + + @doc false + def changeset(group, attrs) do + group + |> cast(attrs, [:title]) + |> validate_required([:title]) + end +end diff --git a/lib/frenzy_web/controllers/feed_controller.ex b/lib/frenzy_web/controllers/feed_controller.ex index c1a9120..9e9cdfa 100644 --- a/lib/frenzy_web/controllers/feed_controller.ex +++ b/lib/frenzy_web/controllers/feed_controller.ex @@ -1,14 +1,10 @@ defmodule FrenzyWeb.FeedController do use FrenzyWeb, :controller - alias Frenzy.{Repo, Feed, Filter, Item} + alias Frenzy.{Repo, Group, Feed, Filter, Item} alias FrenzyWeb.Router.Helpers, as: Routes alias FrenzyWeb.Endpoint import Ecto.Query - def index(conn, _params) do - render(conn, "index.html", feeds: Repo.all(Feed)) - end - def show(conn, %{"id" => id}) do feed = Repo.get(Feed, id) |> Repo.preload(:filter) items = Repo.all(from Item, where: [feed_id: ^id, tombstone: false], order_by: [desc: :date]) @@ -19,36 +15,27 @@ defmodule FrenzyWeb.FeedController do }) end - def new(conn, _params) do - changeset = Feed.changeset(%Feed{}, %{}) + def create(conn, %{"feed" => %{"group_id" => group_id, "feed_url" => feed_url}}) do + group = Repo.get(Group, group_id) - render(conn, "new.html", changeset: changeset) - end - - def create(conn, %{"feed" => %{"feed_url" => feed_url}}) do changeset = - Feed.changeset( - %Feed{ - feed_url: feed_url, - filter: %Filter{ - mode: "reject", - score: 0 - } - }, - %{} - ) + Ecto.build_assoc(group, :feeds, %{ + feed_url: feed_url, + filter: %Filter{ + mode: "reject", + score: 0 + } + }) {:ok, feed} = Repo.insert(changeset) - # changeset = - # Ecto.build_assoc(feed, :filter, %{ - # mode: "reject", - # score: 0 - # }) + redirect(conn, to: Routes.group_path(Endpoint, :show, group_id)) + end - # {:ok, _} = Repo.insert(changeset) - - redirect(conn, to: Routes.feed_path(Endpoint, :index)) + def delete(conn, %{"id" => id}) do + feed = Repo.get(Feed, id) + {:ok, _} = Repo.delete(feed) + redirect(conn, to: Routes.group_path(Endpoint, :show, feed.group_id)) end def enable_filter(conn, %{"id" => id}) do @@ -65,12 +52,6 @@ defmodule FrenzyWeb.FeedController do redirect(conn, to: Routes.feed_path(Endpoint, :show, id)) end - def delete(conn, %{"id" => id}) do - feed = Repo.get(Feed, id) - {:ok, _} = Repo.delete(feed) - redirect(conn, to: Routes.feed_path(Endpoint, :index)) - end - def refresh(conn, %{"id" => id}) do feed = Repo.get(Feed, id) |> Repo.preload(:filter) feed = Frenzy.UpdateFeeds.refresh(Frenzy.UpdateFeeds, feed) diff --git a/lib/frenzy_web/controllers/group_controller.ex b/lib/frenzy_web/controllers/group_controller.ex new file mode 100644 index 0000000..2d3603d --- /dev/null +++ b/lib/frenzy_web/controllers/group_controller.ex @@ -0,0 +1,49 @@ +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 diff --git a/lib/frenzy_web/router.ex b/lib/frenzy_web/router.ex index 5b3a71c..6b91517 100644 --- a/lib/frenzy_web/router.ex +++ b/lib/frenzy_web/router.ex @@ -17,10 +17,10 @@ defmodule FrenzyWeb.Router do scope "/", FrenzyWeb do pipe_through :browser - # get "/", PageController, :index + get "/", GroupController, :index + resources "/groups", GroupController, except: [:edit, :update] - get "/", FeedController, :index - resources "/feeds", FeedController, except: [:edit, :update] + resources "/feeds", FeedController, only: [:show, :create, :delete] post "/feeds/:id/refresh", FeedController, :refresh post "/feeds/:id/enable_filter", FeedController, :enable_filter post "/feeds/:id/disable_filter", FeedController, :disable_filter diff --git a/lib/frenzy_web/templates/feed/index.html.eex b/lib/frenzy_web/templates/feed/index.html.eex deleted file mode 100644 index f3372e5..0000000 --- a/lib/frenzy_web/templates/feed/index.html.eex +++ /dev/null @@ -1,14 +0,0 @@ -Add Feed - - - <%= for feed <- @feeds do %> - - - - - <% end %> -
- <%= feed.feed_url %> - - <%= feed.title %> -
\ No newline at end of file diff --git a/lib/frenzy_web/templates/feed/new.html.eex b/lib/frenzy_web/templates/feed/new.html.eex deleted file mode 100644 index 49b8a2f..0000000 --- a/lib/frenzy_web/templates/feed/new.html.eex +++ /dev/null @@ -1,9 +0,0 @@ -<%= form_for @changeset, Routes.feed_path(@conn, :create), fn form -> %> -
- - <%= text_input form, :feed_url %> -
-
- <%= submit "Create Feed" %> -
-<% end %> \ No newline at end of file diff --git a/lib/frenzy_web/templates/group/index.html.eex b/lib/frenzy_web/templates/group/index.html.eex new file mode 100644 index 0000000..20da229 --- /dev/null +++ b/lib/frenzy_web/templates/group/index.html.eex @@ -0,0 +1,17 @@ +Add Group + + + <%= for group <- @groups do %> + + + + + <% end %> +
+ <%= group.title %> + + <%= case Enum.count(group.feeds) do + 1 -> "1 feed" + count -> "#{count} feeds" + end %> +
\ No newline at end of file diff --git a/lib/frenzy_web/templates/group/new.html.eex b/lib/frenzy_web/templates/group/new.html.eex new file mode 100644 index 0000000..3b24ccb --- /dev/null +++ b/lib/frenzy_web/templates/group/new.html.eex @@ -0,0 +1,9 @@ +<%= form_for @changeset, Routes.group_path(@conn, :create), fn form -> %> +
+ + <%= text_input form, :title %> +
+
+ <%= submit "Create Group" %> +
+<% end %> \ No newline at end of file diff --git a/lib/frenzy_web/templates/group/show.html.eex b/lib/frenzy_web/templates/group/show.html.eex new file mode 100644 index 0000000..24813d4 --- /dev/null +++ b/lib/frenzy_web/templates/group/show.html.eex @@ -0,0 +1,29 @@ +

<%= @group.title %>

+ +<%= form_tag Routes.group_path(@conn, :delete, @group.id), method: :delete do %> + <%= submit "Delete Group" %> +<% end %> + +<%= form_for @create_feed_changeset, Routes.feed_path(@conn, :create), fn form -> %> + <%= hidden_input form, :group_id %> +
+ <%= label form, :feed_url, "Feed URL" %> + <%= text_input form, :feed_url %> +
+
+ <%= submit "Create Feed" %> +
+<% end %> + + + <%= for feed <- @group.feeds do %> + + + + + <% end %> +
+ <%= feed.feed_url %> + + <%= feed.title %> +
\ No newline at end of file diff --git a/lib/frenzy_web/views/group_view.ex b/lib/frenzy_web/views/group_view.ex new file mode 100644 index 0000000..11d455f --- /dev/null +++ b/lib/frenzy_web/views/group_view.ex @@ -0,0 +1,3 @@ +defmodule FrenzyWeb.GroupView do + use FrenzyWeb, :view +end diff --git a/priv/repo/migrations/20190321221841_create_groups.exs b/priv/repo/migrations/20190321221841_create_groups.exs new file mode 100644 index 0000000..a15d70d --- /dev/null +++ b/priv/repo/migrations/20190321221841_create_groups.exs @@ -0,0 +1,11 @@ +defmodule Frenzy.Repo.Migrations.CreateGroups do + use Ecto.Migration + + def change do + create table(:groups) do + add :title, :string + + timestamps() + end + end +end diff --git a/priv/repo/migrations/20190321223329_feeds_add_group.exs b/priv/repo/migrations/20190321223329_feeds_add_group.exs new file mode 100644 index 0000000..856b818 --- /dev/null +++ b/priv/repo/migrations/20190321223329_feeds_add_group.exs @@ -0,0 +1,9 @@ +defmodule Frenzy.Repo.Migrations.FeedsAddGroup do + use Ecto.Migration + + def change do + alter table(:feeds) do + add :group_id, references(:groups, on_delete: :delete_all) + end + end +end