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
62d86e90
Commit
62d86e90
authored
Feb 08, 2021
by
Muhammad Rafif Elfazri
Browse files
Create module for query building thread schema
parent
49ce2dc9
Changes
1
Hide whitespace changes
Inline
Side-by-side
lib/diskuy/forum_thread_page.ex
0 → 100644
View file @
62d86e90
defmodule
Diskuy
.
ForumThreadPage
do
@moduledoc
"""
The Query Builder For Paging Thread Schema.
"""
import
Ecto
.
Query
,
warn:
false
alias
Diskuy
.
Account
.
User
alias
Diskuy
.
Forum
.
Topic
alias
Diskuy
.
Forum
.
Thread
def
page_recent
do
query
=
query_start
()
|>
order_by
([
tr
],
desc:
tr
.
inserted_at
,
asc:
tr
.
title
)
query
end
def
page_top
do
query_start
()
|>
order_by
([
tr
],
desc:
tr
.
points
,
desc:
tr
.
inserted_at
,
asc:
tr
.
title
)
end
def
page_search
(
title
)
do
query_start
()
|>
where
([
p
],
ilike
(
p
.
title
,
^
"%
#{
String
.
replace
(
title
,
~r/[%_(\\)]/
,
"
\\\\\\
0"
)
}
%"
))
end
def
page_per_topic
(
topic_id
)
do
query_start
()
|>
where
([
tr
],
tr
.
topic_id
==
^
topic_id
)
|>
order_by
([
tr
],
desc:
tr
.
inserted_at
,
asc:
tr
.
title
)
end
defp
query_start
do
Thread
|>
join
(
:inner
,
[
tr
],
to
in
Topic
,
as:
:topics
,
on:
tr
.
topic_id
==
to
.
id
)
|>
join
(
:inner
,
[
tr
],
u
in
User
,
as:
:users
,
on:
tr
.
user_id
==
u
.
id
)
|>
select
([
tr
,
to
,
u
],
%{
thread:
%{
id:
tr
.
id
,
title:
tr
.
title
,
content:
tr
.
content
,
points:
tr
.
points
,
user_id:
tr
.
user_id
,
topic_id:
tr
.
topic_id
,
topic_name:
to
.
name
,
username:
u
.
username
,
inserted_at:
tr
.
inserted_at
,
updated_at:
tr
.
updated_at
}}
)
end
end
Write
Preview
Markdown
is supported
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