diff --git a/lib/bdfr_browser/http/plug.ex b/lib/bdfr_browser/http/plug.ex index 1ece185..6c4221b 100644 --- a/lib/bdfr_browser/http/plug.ex +++ b/lib/bdfr_browser/http/plug.ex @@ -44,7 +44,8 @@ defmodule BdfrBrowser.HTTP.Plug do tpl_args = [ subreddit: subreddit, - dates: subreddit_record |> Post.date_listing() |> Repo.all() + dates: subreddit_record |> Post.date_listing() |> Repo.all(), + statistics: subreddit |> Subreddit.statistics() |> Repo.all() ] content = render_template("subreddit", tpl_args) @@ -94,7 +95,8 @@ defmodule BdfrBrowser.HTTP.Plug do tpl_args = [ subreddit: subreddit_names, - dates: subreddit_records |> Post.date_listing() |> Repo.all() + dates: subreddit_records |> Post.date_listing() |> Repo.all(), + statistics: subreddit_names |> Subreddit.statistics() |> Repo.all() ] content = render_template("subreddit", tpl_args) diff --git a/lib/bdfr_browser/subreddit.ex b/lib/bdfr_browser/subreddit.ex index eb9423e..773f422 100644 --- a/lib/bdfr_browser/subreddit.ex +++ b/lib/bdfr_browser/subreddit.ex @@ -22,4 +22,16 @@ defmodule BdfrBrowser.Subreddit do 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 end diff --git a/priv/templates/http/subreddit.eex b/priv/templates/http/subreddit.eex index b192895..4de6423 100644 --- a/priv/templates/http/subreddit.eex +++ b/priv/templates/http/subreddit.eex @@ -49,4 +49,12 @@ <% end %> <% end %> + +
+

+ <%= for {name, post_count, comment_count} <- statistics do %> + <%= name %>: <%= post_count %> Posts; <%= comment_count %> Comments
+ <% end %> +

+