defmodule OpenpodWeb.FeedController do
  use OpenpodWeb, :controller
  alias Openpod.Boundary.ArchiveServer

  def by_identifier(conn, %{"identifier" => identifier, "reload" => _}) do
    do_by_identifier(conn, identifier, fn -> ArchiveServer.reload(identifier) end)
  end

  def by_identifier(conn, %{"identifier" => identifier}) do
    do_by_identifier(conn, identifier, fn -> ArchiveServer.get_feed(identifier) end)
  end

  defp do_by_identifier(conn, identifier, fetcher) do
    %{podcast: podcast, items: items} = fetcher.()
    conn
    |> put_resp_content_type("text/xml")
    |> render("feed.xml", identifier: identifier, podcast: podcast, items: items)
  end
end
