2019-02-11 22:22:35 +00:00
|
|
|
defmodule FrenzyWeb.FeedView do
|
|
|
|
use FrenzyWeb, :view
|
2020-06-02 02:24:18 +00:00
|
|
|
alias Frenzy.{Repo, Item}
|
|
|
|
import Ecto.Query
|
|
|
|
|
2020-06-02 02:38:27 +00:00
|
|
|
@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
|
2020-06-02 02:24:18 +00:00
|
|
|
%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
|
|
|
|
|
2020-06-02 02:38:27 +00:00
|
|
|
@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
|
2020-06-02 02:24:18 +00:00
|
|
|
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
|