Fakultas Ilmu Komputer UI

Commit 62d86e90 authored by Muhammad Rafif Elfazri's avatar Muhammad Rafif Elfazri
Browse files

Create module for query building thread schema

parent 49ce2dc9
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
Markdown is supported
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