frenzy/lib/frenzy_web/controllers/feed_controller.ex

80 lines
2.1 KiB
Elixir
Raw Normal View History

2019-02-11 22:22:35 +00:00
defmodule FrenzyWeb.FeedController do
2019-03-10 23:47:01 +00:00
use FrenzyWeb, :controller
alias Frenzy.{Repo, Feed, Filter, Item}
2019-02-11 22:22:35 +00:00
alias FrenzyWeb.Router.Helpers, as: Routes
alias FrenzyWeb.Endpoint
import Ecto.Query
2019-03-10 23:47:01 +00:00
def index(conn, _params) do
render(conn, "index.html", feeds: Repo.all(Feed))
end
2019-02-11 22:22:35 +00:00
def show(conn, %{"id" => id}) do
2019-03-10 23:47:01 +00:00
feed = Repo.get(Feed, id) |> Repo.preload(:filter)
2019-03-14 23:48:46 +00:00
items = Repo.all(from Item, where: [feed_id: ^id, tombstone: false], order_by: [desc: :date])
2019-03-10 23:47:01 +00:00
2019-02-11 22:22:35 +00:00
render(conn, "show.html", %{
feed: feed,
items: items
2019-03-10 23:47:01 +00:00
})
2019-02-11 22:22:35 +00:00
end
def new(conn, _params) do
changeset = Feed.changeset(%Feed{}, %{})
2019-03-10 23:47:01 +00:00
2019-02-11 22:22:35 +00:00
render(conn, "new.html", changeset: changeset)
end
2019-03-11 02:23:28 +00:00
def create(conn, %{"feed" => %{"feed_url" => feed_url}}) do
2019-03-10 23:47:01 +00:00
changeset =
Feed.changeset(
%Feed{
2019-03-11 02:23:28 +00:00
feed_url: feed_url,
2019-03-10 23:47:01 +00:00
filter: %Filter{
2019-03-11 02:23:28 +00:00
mode: "reject",
score: 0
2019-03-10 23:47:01 +00:00
}
},
2019-03-11 02:23:28 +00:00
%{}
2019-03-10 23:47:01 +00:00
)
2019-02-11 22:22:35 +00:00
{:ok, feed} = Repo.insert(changeset)
2019-03-14 23:48:46 +00:00
# changeset =
# Ecto.build_assoc(feed, :filter, %{
# mode: "reject",
# score: 0
# })
# {:ok, _} = Repo.insert(changeset)
2019-02-11 22:22:35 +00:00
redirect(conn, to: Routes.feed_path(Endpoint, :index))
end
2019-03-10 23:47:01 +00:00
def enable_filter(conn, %{"id" => id}) do
feed = Repo.get(Feed, id) |> Repo.preload(:filter)
changeset = Feed.changeset(feed, %{filter_enabled: true})
Repo.update(changeset)
redirect(conn, to: Routes.feed_path(Endpoint, :show, id))
end
def disable_filter(conn, %{"id" => id}) do
2019-03-11 02:23:28 +00:00
feed = Repo.get(Feed, id) |> Repo.preload(:filter)
2019-03-10 23:47:01 +00:00
changeset = Feed.changeset(feed, %{filter_enabled: false})
Repo.update(changeset)
redirect(conn, to: Routes.feed_path(Endpoint, :show, id))
end
2019-02-11 22:22:35 +00:00
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
2019-03-11 02:23:28 +00:00
feed = Repo.get(Feed, id) |> Repo.preload(:filter)
2019-02-11 22:22:35 +00:00
feed = Frenzy.UpdateFeeds.refresh(Frenzy.UpdateFeeds, feed)
redirect(conn, to: Routes.feed_path(Endpoint, :show, feed.id))
end
2019-03-10 23:47:01 +00:00
end