Fakultas Ilmu Komputer UI
Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
Menu
Open sidebar
Functional Programming
Diskuy-Backend
Commits
f632f4e0
Commit
f632f4e0
authored
Feb 09, 2021
by
Muhammad Rafif Elfazri
Browse files
Refactor Repetitive code on PostPageController
parent
755324fb
Changes
1
Hide whitespace changes
Inline
Side-by-side
lib/diskuy_web/controllers/post_pages_controller.ex
View file @
f632f4e0
...
...
@@ -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
...
...
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment