Fakultas Ilmu Komputer UI

Skip to content
Snippets Groups Projects
Select Git revision
  • 4540a0fb3c0f714abf2a6c67223b73427604c1cd
  • master default protected
  • feature/surveys2
  • deploy-runtime
  • deploy
5 results

forum_thread_page.ex

Blame
  • Code owners
    Assign users and groups as approvers for specific file changes. Learn more.
    forum_thread_page.ex 1.32 KiB
    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