Initial groups implementation

This commit is contained in:
Shadowfacts 2019-03-21 19:21:56 -04:00
parent 7a39e649d1
commit eb0ebeba91
Signed by: shadowfacts
GPG Key ID: 94A5AB95422746E5
13 changed files with 167 additions and 61 deletions

View File

@ -21,6 +21,8 @@ defmodule Frenzy.Feed do
field :title, :string field :title, :string
field :filter_enabled, :boolean field :filter_enabled, :boolean
belongs_to :group, Frenzy.Group
has_many :items, Frenzy.Item, on_delete: :delete_all has_many :items, Frenzy.Item, on_delete: :delete_all
has_one :filter, Frenzy.Filter, on_delete: :delete_all has_one :filter, Frenzy.Filter, on_delete: :delete_all

19
lib/frenzy/group.ex Normal file
View File

@ -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

View File

@ -1,14 +1,10 @@
defmodule FrenzyWeb.FeedController do defmodule FrenzyWeb.FeedController do
use FrenzyWeb, :controller use FrenzyWeb, :controller
alias Frenzy.{Repo, Feed, Filter, Item} alias Frenzy.{Repo, Group, Feed, Filter, Item}
alias FrenzyWeb.Router.Helpers, as: Routes alias FrenzyWeb.Router.Helpers, as: Routes
alias FrenzyWeb.Endpoint alias FrenzyWeb.Endpoint
import Ecto.Query import Ecto.Query
def index(conn, _params) do
render(conn, "index.html", feeds: Repo.all(Feed))
end
def show(conn, %{"id" => id}) do def show(conn, %{"id" => id}) do
feed = Repo.get(Feed, id) |> Repo.preload(:filter) feed = Repo.get(Feed, id) |> Repo.preload(:filter)
items = Repo.all(from Item, where: [feed_id: ^id, tombstone: false], order_by: [desc: :date]) items = Repo.all(from Item, where: [feed_id: ^id, tombstone: false], order_by: [desc: :date])
@ -19,36 +15,27 @@ defmodule FrenzyWeb.FeedController do
}) })
end end
def new(conn, _params) do def create(conn, %{"feed" => %{"group_id" => group_id, "feed_url" => feed_url}}) do
changeset = Feed.changeset(%Feed{}, %{}) group = Repo.get(Group, group_id)
render(conn, "new.html", changeset: changeset)
end
def create(conn, %{"feed" => %{"feed_url" => feed_url}}) do
changeset = changeset =
Feed.changeset( Ecto.build_assoc(group, :feeds, %{
%Feed{
feed_url: feed_url, feed_url: feed_url,
filter: %Filter{ filter: %Filter{
mode: "reject", mode: "reject",
score: 0 score: 0
} }
}, })
%{}
)
{:ok, feed} = Repo.insert(changeset) {:ok, feed} = Repo.insert(changeset)
# changeset = redirect(conn, to: Routes.group_path(Endpoint, :show, group_id))
# Ecto.build_assoc(feed, :filter, %{ end
# mode: "reject",
# score: 0
# })
# {:ok, _} = Repo.insert(changeset) def delete(conn, %{"id" => id}) do
feed = Repo.get(Feed, id)
redirect(conn, to: Routes.feed_path(Endpoint, :index)) {:ok, _} = Repo.delete(feed)
redirect(conn, to: Routes.group_path(Endpoint, :show, feed.group_id))
end end
def enable_filter(conn, %{"id" => id}) do def enable_filter(conn, %{"id" => id}) do
@ -65,12 +52,6 @@ defmodule FrenzyWeb.FeedController do
redirect(conn, to: Routes.feed_path(Endpoint, :show, id)) redirect(conn, to: Routes.feed_path(Endpoint, :show, id))
end 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 def refresh(conn, %{"id" => id}) do
feed = Repo.get(Feed, id) |> Repo.preload(:filter) feed = Repo.get(Feed, id) |> Repo.preload(:filter)
feed = Frenzy.UpdateFeeds.refresh(Frenzy.UpdateFeeds, feed) feed = Frenzy.UpdateFeeds.refresh(Frenzy.UpdateFeeds, feed)

View File

@ -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

View File

@ -17,10 +17,10 @@ defmodule FrenzyWeb.Router do
scope "/", FrenzyWeb do scope "/", FrenzyWeb do
pipe_through :browser pipe_through :browser
# get "/", PageController, :index get "/", GroupController, :index
resources "/groups", GroupController, except: [:edit, :update]
get "/", FeedController, :index resources "/feeds", FeedController, only: [:show, :create, :delete]
resources "/feeds", FeedController, except: [:edit, :update]
post "/feeds/:id/refresh", FeedController, :refresh post "/feeds/:id/refresh", FeedController, :refresh
post "/feeds/:id/enable_filter", FeedController, :enable_filter post "/feeds/:id/enable_filter", FeedController, :enable_filter
post "/feeds/:id/disable_filter", FeedController, :disable_filter post "/feeds/:id/disable_filter", FeedController, :disable_filter

View File

@ -1,14 +0,0 @@
<a href="/feeds/new" class="button">Add Feed</a>
<table>
<%= for feed <- @feeds do %>
<tr>
<td>
<a href="/feeds/<%= feed.id %>"><%= feed.feed_url %></a>
</td>
<td>
<a href="<%= feed.site_url %>"><%= feed.title %></a>
</td>
</tr>
<% end %>
</table>

View File

@ -1,9 +0,0 @@
<%= form_for @changeset, Routes.feed_path(@conn, :create), fn form -> %>
<div class="form-group">
<label for="feed_url">Feed URL</label>
<%= text_input form, :feed_url %>
</div>
<div class="form-group">
<%= submit "Create Feed" %>
</div>
<% end %>

View File

@ -0,0 +1,17 @@
<a href="<%= Routes.group_path(@conn, :new) %>" class="button">Add Group</a>
<table>
<%= for group <- @groups do %>
<tr>
<td>
<a href="/groups/<%= group.id %>"><%= group.title %></a>
</td>
<td>
<%= case Enum.count(group.feeds) do
1 -> "1 feed"
count -> "#{count} feeds"
end %>
</td>
</tr>
<% end %>
</table>

View File

@ -0,0 +1,9 @@
<%= form_for @changeset, Routes.group_path(@conn, :create), fn form -> %>
<div class="form-group">
<label for="title">Title</label>
<%= text_input form, :title %>
</div>
<div class="form-group">
<%= submit "Create Group" %>
</div>
<% end %>

View File

@ -0,0 +1,29 @@
<h2><%= @group.title %></h2>
<%= 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 %>
<div class="form-group">
<%= label form, :feed_url, "Feed URL" %>
<%= text_input form, :feed_url %>
</div>
<div class="form-group">
<%= submit "Create Feed" %>
</div>
<% end %>
<table>
<%= for feed <- @group.feeds do %>
<tr>
<td>
<a href="<%= Routes.feed_path(@conn, :show, feed.id) %>"><%= feed.feed_url %></a>
</td>
<td>
<a href="<%= feed.site_url %>"><%= feed.title %></a>
</td>
</tr>
<% end %>
</table>

View File

@ -0,0 +1,3 @@
defmodule FrenzyWeb.GroupView do
use FrenzyWeb, :view
end

View File

@ -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

View File

@ -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