Fakultas Ilmu Komputer UI

Commit 752a9d80 authored by Muhammad Rafif Elfazri's avatar Muhammad Rafif Elfazri
Browse files

Adapt the code with Scrivener Pagination

parent 0f607339
defmodule DiskuyWeb.ThreadPagesController do
use DiskuyWeb, :controller
alias Diskuy.Forum
alias Diskuy.ForumThreadPage
alias Diskuy.Pagination
alias Diskuy.Repo
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})
def pages_top(conn, %{"page" => page_num}) do
page = ForumThreadPage.page_top() |> Repo.paginate(page: page_num)
with %{entries: _entries} = page do
render(conn, "pages.json", %{page: page})
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})
page = ForumThreadPage.page_top() |> Repo.paginate()
with %{entries: _entries} = page do
render(conn, "pages.json", %{page: page})
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})
def pages_recent(conn, %{"page" => page_num}) do
page = ForumThreadPage.page_recent() |> Repo.paginate(page: page_num)
with %{entries: _entries} = page do
render(conn, "pages.json", %{page: page})
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})
page = ForumThreadPage.page_recent() |> Repo.paginate()
with %{entries: _entries} = page do
render(conn, "pages.json", %{page: page})
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})
def pages_search(conn, %{"title" => title, "page" => page_num}) do
page = ForumThreadPage.page_search(title) |> Repo.paginate(page: page_num)
with %{entries: _entries} = page do
render(conn, "pages.json", %{page: page})
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})
page = ForumThreadPage.page_search(title) |> Repo.paginate()
with %{entries: _entries} <- page do
render(conn, "pages.json", %{page: page})
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})
def pages_topic(conn, %{"topic_id" => topic_id, "page" => page_num}) do
page = ForumThreadPage.page_per_topic(topic_id) |> Repo.paginate(page: page_num)
with %{entries: _entries} = page do
render(conn, "pages.json", %{page: page})
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})
page = ForumThreadPage.page_per_topic(topic_id) |> Repo.paginate()
with %{entries: _entries} <- page do
render(conn, "pages.json", %{page: page})
end
end
......
......@@ -2,9 +2,16 @@ defmodule DiskuyWeb.ThreadPagesView do
use DiskuyWeb, :view
# alias DiskuyWeb.ThreadPagesView
def render("pages.json", %{entries: entries, metadata: metadata}) do
%{entries: entries,
metadata: Map.from_struct(metadata)
def render("pages.json", %{page: page}) do
%{
data: Enum.map(page.entries, fn x -> x.thread end),
metadata: %{
page_number: page.page_number,
page_size: page.page_size,
total_data: page.total_entries,
total_pages: page.total_pages
}
}
end
end
Supports Markdown
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