Fakultas Ilmu Komputer UI

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

Refactor Repetitive code on PostPageController

parent 755324fb
......@@ -5,15 +5,23 @@ defmodule DiskuyWeb.PostPagesController do
# Pages Per threads
def pages_thread(conn, %{"thread_id" => thread_id, "page" => page_num}) do
page = ForumPostPage.page_per_thread(thread_id) |> Repo.paginate(page: page_num)
with %{entries: _entries} <- page do
render(conn, "pages.json", %{page: page})
def pages_thread(conn, %{"thread_id" => thread_id}) do
query = ForumPostPage.page_per_thread(thread_id) |> Repo.paginate()
paginate_and_render(conn, query)
end
defp paginate_and_render(conn, query) do
case conn.params do
%{"page" => page_num} ->
query
|> Repo.paginate(page: page_num) |> render_helper(conn)
_ ->
query
|> Repo.paginate(page: 1) |> render_helper(conn)
end
end
def pages_thread(conn, %{"thread_id" => thread_id}) do
page = ForumPostPage.page_per_thread(thread_id) |> Repo.paginate()
defp render_helper(page, conn) do
with %{entries: _entries} <- page do
render(conn, "pages.json", %{page: page})
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