This repository has been archived on 2023-11-26. You can view files and clone it, but cannot push or open issues or pull requests.
bdfr-browser/lib/bdfr_browser/subreddit.ex

38 lines
931 B
Elixir
Raw Normal View History

2023-08-13 23:18:55 +00:00
defmodule BdfrBrowser.Subreddit do
use Ecto.Schema
import Ecto.Query, only: [from: 2]
alias BdfrBrowser.Post
schema "subreddits" do
2023-10-02 10:59:52 +00:00
field :name, :string
2023-08-13 23:18:55 +00:00
2023-10-02 10:59:52 +00:00
has_many :posts, Post
end
def multiple_names(names) do
from(s in __MODULE__, where: s.name in ^names)
2023-08-13 23:18:55 +00:00
end
def names do
2023-08-14 00:06:10 +00:00
from(s in __MODULE__, select: s.name, order_by: [asc: fragment("lower(?)", s.name)])
2023-08-13 23:18:55 +00:00
end
def names_without(hidden) do
from(s in __MODULE__, select: s.name, where: s.name not in ^hidden, order_by: [asc: fragment("lower(?)", s.name)])
end
def statistics(subreddit) when is_binary(subreddit), do: statistics([subreddit])
def statistics(subreddits) when is_list(subreddits) do
from(s in __MODULE__,
join: p in assoc(s, :posts),
left_join: c in assoc(p, :comments),
select: {s.name, count(p.id), count(c.id)},
where: s.name in ^subreddits,
group_by: [s.name]
)
end
2023-08-13 23:18:55 +00:00
end