diff --git a/lib/bdfr_browser/comment.ex b/lib/bdfr_browser/comment.ex index 74e2122..b2c568b 100644 --- a/lib/bdfr_browser/comment.ex +++ b/lib/bdfr_browser/comment.ex @@ -41,4 +41,26 @@ defmodule BdfrBrowser.Comment do group_by: [c.id, p.id, s.name] ) end + + def search(str) 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: 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 dfd5be4..f6109d7 100644 --- a/lib/bdfr_browser/http/plug.ex +++ b/lib/bdfr_browser/http/plug.ex @@ -106,6 +106,32 @@ defmodule BdfrBrowser.HTTP.Plug do |> send_resp(200, content) end + get "/search" do + conn = Plug.Conn.fetch_query_params(conn) + params = conn.query_params + + {tpl, tpl_args} = + if not is_nil(params["comment"]) and String.length(params["comment"]) > 0 do + search = params["comment"] + + {"search_comments", [search: search, comments: search |> Comment.search() |> Repo.all()]} + else + search = params["post"] + + {"search_posts", + [ + search: search, + posts: search |> Post.search() |> Repo.all() + ]} + end + + content = render_template(tpl, tpl_args) + + conn + |> put_resp_header("content-type", "text/html; charset=utf-8") + |> send_resp(200, content) + end + get "/static/*path" do file_path = Application.app_dir(:bdfr_browser, Path.join("priv/static", path)) diff --git a/lib/bdfr_browser/post.ex b/lib/bdfr_browser/post.ex index 6ccc1e1..032ee64 100644 --- a/lib/bdfr_browser/post.ex +++ b/lib/bdfr_browser/post.ex @@ -78,4 +78,25 @@ defmodule BdfrBrowser.Post do group_by: [p.id, s.name] ) end + + def search(str) 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: 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/index.eex b/priv/templates/http/index.eex index 7d4a8f2..ae94ff1 100644 --- a/priv/templates/http/index.eex +++ b/priv/templates/http/index.eex @@ -1,9 +1,33 @@ -

Archived Subreddits

+

BDFR Browser

Chats +
+
+
+ +
+
+ +
+ +
+ +
+ +
+ +
+
+ +
+ +

Archived Subreddits

+ +
+
<%= for subreddit <- subreddits, not String.starts_with?(subreddit, "u_") do %> <%= subreddit %> <% end %> @@ -23,3 +47,5 @@ <% end %>
+ +
diff --git a/priv/templates/http/search_comments.eex b/priv/templates/http/search_comments.eex new file mode 100644 index 0000000..864f2d2 --- /dev/null +++ b/priv/templates/http/search_comments.eex @@ -0,0 +1,22 @@ +

Search: <%= search %>

+ +
+
+ <%= for comment <- comments do %> +
+
+
+ <%= BdfrBrowser.RenderUtils.comment(comment.body) %> + + +
+
+
+ <% end %> +
+
diff --git a/priv/templates/http/search_posts.eex b/priv/templates/http/search_posts.eex new file mode 100644 index 0000000..764e6d5 --- /dev/null +++ b/priv/templates/http/search_posts.eex @@ -0,0 +1,20 @@ +

Search: <%= search %>

+ +
+
+ <%= for post <- posts do %> +
+
+
<%= post.title %>
+
+ + <%= post.subreddit %> - + <%= post.num_comments %> comment(s) - + <%= Calendar.strftime(post.posted_at, "%Y-%m-%d") %> + +
+
+
+ <% end %> +
+
diff --git a/priv/templates/http/subreddit_posts.eex b/priv/templates/http/subreddit_posts.eex index 6c8b5b7..05ad54a 100644 --- a/priv/templates/http/subreddit_posts.eex +++ b/priv/templates/http/subreddit_posts.eex @@ -7,7 +7,7 @@
<%= post.title %>
- <%= post.num_comments %> comment(s) - <%= DateTime.to_iso8601(post.posted_at) %> + <%= post.num_comments %> comment(s) - <%= DateTime.to_iso8601(post.posted_at) %>
diff --git a/priv/templates/http/user.eex b/priv/templates/http/user.eex index d4422ce..a833692 100644 --- a/priv/templates/http/user.eex +++ b/priv/templates/http/user.eex @@ -29,9 +29,11 @@
<%= post.title %>
- <%= post.subreddit %> - - <%= post.num_comments %> comment(s) - - <%= Calendar.strftime(post.posted_at, "%Y-%m-%d") %> + + <%= post.subreddit %> - + <%= post.num_comments %> comment(s) - + <%= Calendar.strftime(post.posted_at, "%Y-%m-%d") %> +