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

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
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{
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)

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

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