Fakultas Ilmu Komputer UI

Commit 755324fb authored by Muhammad Rafif Elfazri's avatar Muhammad Rafif Elfazri
Browse files

Refactor Repetitive code

parent d8c635b9
......@@ -5,64 +5,47 @@ defmodule DiskuyWeb.ThreadPagesController do
action_fallback DiskuyWeb.FallbackController
# Pages Top Pattern Matching
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
# Pages Top Pattern Matching
def pages_top(conn, _params) do
page = ForumThreadPage.page_top() |> Repo.paginate()
with %{entries: _entries} = page do
render(conn, "pages.json", %{page: page})
end
query = ForumThreadPage.page_top()
paginate_and_render(conn, query)
end
# Pages Recent Pattern Matching
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() |> Repo.paginate()
with %{entries: _entries} = page do
render(conn, "pages.json", %{page: page})
end
query = ForumThreadPage.page_recent()
paginate_and_render(conn, query)
end
# Pages Search Pattern Matching
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
def pages_search(conn, %{"title" => title}) do
query = ForumThreadPage.page_search(title)
paginate_and_render(conn, query)
end
def pages_search(conn, %{"title" => title}) do
page = ForumThreadPage.page_search(title) |> Repo.paginate()
with %{entries: _entries} <- page do
render(conn, "pages.json", %{page: page})
end
# Pages Per topic
def pages_topic(conn, %{"topic_id" => topic_id}) do
query = ForumThreadPage.page_per_topic(topic_id)
paginate_and_render(conn, query)
end
# Pages Per topic
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})
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_topic(conn, %{"topic_id" => topic_id}) do
page = ForumThreadPage.page_per_topic(topic_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