diff --git a/lib/bdfr_browser/comment.ex b/lib/bdfr_browser/comment.ex index b2c568b..4531606 100644 --- a/lib/bdfr_browser/comment.ex +++ b/lib/bdfr_browser/comment.ex @@ -42,7 +42,9 @@ defmodule BdfrBrowser.Comment do ) end - def search(str) do + def search(str), do: search(str, nil) + + def search(str, subreddits) when is_nil(subreddits) do search_str = "%#{str}%" from(c in __MODULE__, @@ -63,4 +65,26 @@ defmodule BdfrBrowser.Comment do group_by: [c.id, p.id, s.name] ) end + + def search(str, subreddits) when is_list(subreddits) do + search_str = "%#{str}%" + + from(c in __MODULE__, + join: p in assoc(c, :post), + join: s in assoc(p, :subreddit), + select: %{ + author: c.author, + body: c.body, + children: [], + posted_at: c.posted_at, + subreddit: s.name, + post_id: p.id, + post_title: p.title, + post_date: fragment("to_char(?, 'YYYY-MM')", p.posted_at) + }, + where: s.name in ^subreddits and ilike(c.body, ^search_str), + order_by: [desc: c.posted_at], + group_by: [c.id, p.id, s.name] + ) + end end diff --git a/lib/bdfr_browser/http/plug.ex b/lib/bdfr_browser/http/plug.ex index b7eeaed..1ece185 100644 --- a/lib/bdfr_browser/http/plug.ex +++ b/lib/bdfr_browser/http/plug.ex @@ -164,11 +164,12 @@ defmodule BdfrBrowser.HTTP.Plug do conn = Plug.Conn.fetch_query_params(conn) params = conn.query_params search = params["search"] + subreddits = params["subreddit"] tpl_args = [ search: search, - posts: search |> Post.search() |> Repo.all(), - comments: search |> Comment.search() |> Repo.all() + posts: search |> Post.search(subreddits) |> Repo.all(), + comments: search |> Comment.search(subreddits) |> Repo.all() ] content = render_template("search", tpl_args) diff --git a/lib/bdfr_browser/post.ex b/lib/bdfr_browser/post.ex index 1b13b28..9501618 100644 --- a/lib/bdfr_browser/post.ex +++ b/lib/bdfr_browser/post.ex @@ -120,7 +120,9 @@ defmodule BdfrBrowser.Post do ) end - def search(str) do + def search(str), do: search(str, nil) + + def search(str, subreddits) when is_nil(subreddits) do search_str = "%#{str}%" from(p in __MODULE__, @@ -140,4 +142,25 @@ defmodule BdfrBrowser.Post do group_by: [p.id, s.name] ) end + + def search(str, subreddits) when is_list(subreddits) do + search_str = "%#{str}%" + + from(p in __MODULE__, + left_join: c in assoc(p, :comments), + join: s in assoc(p, :subreddit), + select: %{ + id: p.id, + title: p.title, + author: p.author, + posted_at: p.posted_at, + num_comments: count(c.id), + subreddit: s.name, + date: fragment("to_char(?, 'YYYY-MM')", p.posted_at) + }, + where: s.name in ^subreddits and (ilike(p.title, ^search_str) or ilike(p.selftext, ^search_str)), + order_by: [desc: p.posted_at], + group_by: [p.id, s.name] + ) + end end diff --git a/priv/templates/http/subreddit.eex b/priv/templates/http/subreddit.eex index ba2d0ed..b192895 100644 --- a/priv/templates/http/subreddit.eex +++ b/priv/templates/http/subreddit.eex @@ -13,12 +13,32 @@

<%= if is_list(subreddit) do %> - <%= Enum.join(subreddit, ", ") %> + Multi-Reddit <% else %> <%= subreddit %> <% end %>

+
+
+ +
+ + <%= if is_list(subreddit) do %> + <%= for s <- subreddit do %> + + <% end %> + <% else %> + + <% end %> + +
+ +
+
+ +
+
<%= for date <- dates do %> diff --git a/priv/templates/http/subreddit_posts.eex b/priv/templates/http/subreddit_posts.eex index b858eb0..f93807d 100644 --- a/priv/templates/http/subreddit_posts.eex +++ b/priv/templates/http/subreddit_posts.eex @@ -14,7 +14,7 @@

<%= if is_list(subreddit) do %> - <%= Enum.join(subreddit, ", ") %> + Multi-Reddit <% else %> <%= subreddit %> <% end %>