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 defmodule DiskuyWeb.ThreadPagesController do
use DiskuyWeb, :controller use DiskuyWeb, :controller
alias Diskuy.Forum
alias Diskuy.ForumThreadPage alias Diskuy.ForumThreadPage
alias Diskuy.Pagination alias Diskuy.Repo
action_fallback DiskuyWeb.FallbackController action_fallback DiskuyWeb.FallbackController
# Pages Top Pattern Matching # Pages Top Pattern Matching
def pages_top(conn, %{"after" => cursor}) do def pages_top(conn, %{"page" => page_num}) do
page = ForumThreadPage.page_top() |> Pagination.paginate_after(cursor) page = ForumThreadPage.page_top() |> Repo.paginate(page: page_num)
with %{entries: entries, metadata: metadata} = page do with %{entries: _entries} = page do
render(conn, "pages.json", %{entries: entries, metadata: metadata}) render(conn, "pages.json", %{page: page})
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
end end
def pages_top(conn, _params) do def pages_top(conn, _params) do
page = ForumThreadPage.page_top() |> Pagination.paginate_start page = ForumThreadPage.page_top() |> Repo.paginate()
with %{entries: entries, metadata: metadata} <- page do with %{entries: _entries} = page do
render(conn, "pages.json", %{entries: entries, metadata: metadata}) render(conn, "pages.json", %{page: page})
end end
end end
# Pages Recent Pattern Matching # Pages Recent Pattern Matching
def pages_recent(conn, %{"after" => cursor}) do def pages_recent(conn, %{"page" => page_num}) do
page = ForumThreadPage.page_recent() |> Pagination.paginate_after(cursor) page = ForumThreadPage.page_recent() |> Repo.paginate(page: page_num)
with %{entries: entries, metadata: metadata} = page do with %{entries: _entries} = page do
render(conn, "pages.json", %{entries: entries, metadata: metadata}) render(conn, "pages.json", %{page: page})
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
end end
def pages_recent(conn, _params) do def pages_recent(conn, _params) do
page = ForumThreadPage.page_recent() |> Pagination.paginate_start page = ForumThreadPage.page_recent() |> Repo.paginate()
with %{entries: entries, metadata: metadata} <- page do with %{entries: _entries} = page do
render(conn, "pages.json", %{entries: entries, metadata: metadata}) render(conn, "pages.json", %{page: page})
end end
end end
# Pages Search Pattern Matching # Pages Search Pattern Matching
def pages_search(conn, %{"title" => title,"after" => cursor}) do def pages_search(conn, %{"title" => title, "page" => page_num}) do
page = ForumThreadPage.page_search(title) |> Pagination.paginate_after(cursor) page = ForumThreadPage.page_search(title) |> Repo.paginate(page: page_num)
with %{entries: entries, metadata: metadata} = page do with %{entries: _entries} = page do
render(conn, "pages.json", %{entries: entries, metadata: metadata}) render(conn, "pages.json", %{page: page})
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
end end
def pages_search(conn, %{"title" => title}) do def pages_search(conn, %{"title" => title}) do
page = ForumThreadPage.page_search(title) |> Pagination.paginate_start page = ForumThreadPage.page_search(title) |> Repo.paginate()
with %{entries: entries, metadata: metadata} <- page do with %{entries: _entries} <- page do
render(conn, "pages.json", %{entries: entries, metadata: metadata}) render(conn, "pages.json", %{page: page})
end end
end end
# Pages Per topic # Pages Per topic
def pages_topic(conn, %{"topic_id" => topic_id,"after" => cursor}) do def pages_topic(conn, %{"topic_id" => topic_id, "page" => page_num}) do
page = ForumThreadPage.page_per_topic(topic_id) |> Pagination.paginate_after(cursor) page = ForumThreadPage.page_per_topic(topic_id) |> Repo.paginate(page: page_num)
with %{entries: entries, metadata: metadata} = page do with %{entries: _entries} = page do
render(conn, "pages.json", %{entries: entries, metadata: metadata}) render(conn, "pages.json", %{page: page})
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
end end
def pages_topic(conn, %{"topic_id" => topic_id}) do def pages_topic(conn, %{"topic_id" => topic_id}) do
page = ForumThreadPage.page_per_topic(topic_id) |> Pagination.paginate_start page = ForumThreadPage.page_per_topic(topic_id) |> Repo.paginate()
with %{entries: entries, metadata: metadata} <- page do with %{entries: _entries} <- page do
render(conn, "pages.json", %{entries: entries, metadata: metadata}) render(conn, "pages.json", %{page: page})
end end
end end
......
...@@ -2,9 +2,16 @@ defmodule DiskuyWeb.ThreadPagesView do ...@@ -2,9 +2,16 @@ defmodule DiskuyWeb.ThreadPagesView do
use DiskuyWeb, :view use DiskuyWeb, :view
# alias DiskuyWeb.ThreadPagesView # alias DiskuyWeb.ThreadPagesView
def render("pages.json", %{entries: entries, metadata: metadata}) do def render("pages.json", %{page: page}) do
%{entries: entries, %{
metadata: Map.from_struct(metadata) 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
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