defmodule FrenzyWeb.FeedController do use FrenzyWeb, :controller alias Frenzy.{Repo, 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], order_by: [desc: :date]) render(conn, "show.html", %{ feed: feed, items: items }) end def new(conn, _params) do changeset = Feed.changeset(%Feed{}, %{}) render(conn, "new.html", changeset: changeset) end def create(conn, %{"feed" => feed}) do changeset = Feed.changeset( %Feed{ filter: %Filter{ mode: "accept", score: 0, rules: [] } }, feed ) {:ok, feed} = Repo.insert(changeset) redirect(conn, to: Routes.feed_path(Endpoint, :index)) end 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 feed = Repo.get(Feed, id) changeset = Feed.changeset(feed, %{filter_enabled: false}) Repo.update(changeset) 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) feed = Frenzy.UpdateFeeds.refresh(Frenzy.UpdateFeeds, feed) redirect(conn, to: Routes.feed_path(Endpoint, :show, feed.id)) end end