Fakultas Ilmu Komputer UI

Commit cba4de95 authored by Muhammad Rafif Elfazri's avatar Muhammad Rafif Elfazri
Browse files

Create Controller for Thread pagination

parent 13c589b9
defmodule DiskuyWeb.ThreadPagesController do
use DiskuyWeb, :controller
alias Diskuy.Forum
alias Diskuy.ForumThreadPage
alias Diskuy.Pagination
action_fallback DiskuyWeb.FallbackController
# Pages Top Pattern Matching
def pages_top(conn, %{"after" => cursor}) do
page = ForumThreadPage.page_top() |> Pagination.paginate_after(cursor)
with %{entries: entries, metadata: metadata} = page do
render(conn, "pages.json", %{entries: entries, metadata: metadata})
end
end
def pages_top(conn, %{"before" => cursor}) do
page = ForumThreadPage.page_top() |> Pagination.paginate_before(cursor)
with %{entries: entries, metadata: metadata} = page do
render(conn, "pages.json", %{entries: entries, metadata: metadata})
end
end
def pages_top(conn, _params) do
page = ForumThreadPage.page_top() |> Pagination.paginate_start
with %{entries: entries, metadata: metadata} <- page do
render(conn, "pages.json", %{entries: entries, metadata: metadata})
end
end
# Pages Recent Pattern Matching
def pages_recent(conn, %{"after" => cursor}) do
page = ForumThreadPage.page_recent() |> Pagination.paginate_after(cursor)
with %{entries: entries, metadata: metadata} = page do
render(conn, "pages.json", %{entries: entries, metadata: metadata})
end
end
def pages_recent(conn, %{"before" => cursor}) do
page = ForumThreadPage.page_recent() |> Pagination.paginate_before(cursor)
with %{entries: entries, metadata: metadata} = page do
render(conn, "pages.json", %{entries: entries, metadata: metadata})
end
end
def pages_recent(conn, _params) do
page = ForumThreadPage.page_recent() |> Pagination.paginate_start
with %{entries: entries, metadata: metadata} <- page do
render(conn, "pages.json", %{entries: entries, metadata: metadata})
end
end
# Pages Search Pattern Matching
def pages_search(conn, %{"title" => title,"after" => cursor}) do
page = ForumThreadPage.page_search(title) |> Pagination.paginate_after(cursor)
with %{entries: entries, metadata: metadata} = page do
render(conn, "pages.json", %{entries: entries, metadata: metadata})
end
end
def pages_search(conn, %{"title" => title, "before" => cursor}) do
page = ForumThreadPage.page_search(title) |> Pagination.paginate_before(cursor)
with %{entries: entries, metadata: metadata} = page do
render(conn, "pages.json", %{entries: entries, metadata: metadata})
end
end
def pages_search(conn, %{"title" => title}) do
page = ForumThreadPage.page_search(title) |> Pagination.paginate_start
with %{entries: entries, metadata: metadata} <- page do
render(conn, "pages.json", %{entries: entries, metadata: metadata})
end
end
# Pages Per topic
def pages_topic(conn, %{"topic_id" => topic_id,"after" => cursor}) do
page = ForumThreadPage.page_per_topic(topic_id) |> Pagination.paginate_after(cursor)
with %{entries: entries, metadata: metadata} = page do
render(conn, "pages.json", %{entries: entries, metadata: metadata})
end
end
def pages_topic(conn, %{"topic_id" => topic_id, "before" => cursor}) do
page = ForumThreadPage.page_per_topic(topic_id) |> Pagination.paginate_before(cursor)
with %{entries: entries, metadata: metadata} = page do
render(conn, "pages.json", %{entries: entries, metadata: metadata})
end
end
def pages_topic(conn, %{"topic_id" => topic_id}) do
page = ForumThreadPage.page_per_topic(topic_id) |> Pagination.paginate_start
with %{entries: entries, metadata: metadata} <- page do
render(conn, "pages.json", %{entries: entries, metadata: metadata})
end
end
end
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment