defmodule FrenzyWeb.FeedView do use FrenzyWeb, :view alias Frenzy.{Repo, Item} import Ecto.Query @spec next_page_path(Plug.Conn.t(), [Frenzy.Item.t()]) :: String.t() | nil def next_page_path(_conn, []), do: nil def next_page_path(conn, items) do %Item{id: id, date: date} = List.last(items) has_older = Repo.exists?(from i in Item, where: i.date < ^date) if has_older do Phoenix.Controller.current_path(conn, %{before: id}) else nil end end @spec prev_page_path(Plug.Conn.t(), [Frenzy.Item.t()]) :: String.t() | nil def prev_page_path(_conn, []), do: nil def prev_page_path(conn, [%Item{id: id, date: date} | _]) do has_newer = Repo.exists?(from i in Item, where: i.date > ^date) if has_newer do Phoenix.Controller.current_path(conn, %{after: id}) else nil end end end