From 388ecee31feee341badd26645455d6da21c5fcb7 Mon Sep 17 00:00:00 2001 From: Daniel Kempkens Date: Sat, 19 Aug 2023 12:43:26 +0200 Subject: [PATCH] feat: Unified search results --- lib/bdfr_browser/http/plug.ex | 22 +++------ priv/templates/http/index.eex | 10 ++-- priv/templates/http/search.eex | 61 +++++++++++++++++++++++++ priv/templates/http/search_comments.eex | 22 --------- priv/templates/http/search_posts.eex | 20 -------- 5 files changed, 71 insertions(+), 64 deletions(-) create mode 100644 priv/templates/http/search.eex delete mode 100644 priv/templates/http/search_comments.eex delete mode 100644 priv/templates/http/search_posts.eex diff --git a/lib/bdfr_browser/http/plug.ex b/lib/bdfr_browser/http/plug.ex index f6109d7..33dc87b 100644 --- a/lib/bdfr_browser/http/plug.ex +++ b/lib/bdfr_browser/http/plug.ex @@ -109,23 +109,15 @@ defmodule BdfrBrowser.HTTP.Plug do get "/search" do conn = Plug.Conn.fetch_query_params(conn) params = conn.query_params + search = params["search"] - {tpl, tpl_args} = - if not is_nil(params["comment"]) and String.length(params["comment"]) > 0 do - search = params["comment"] + tpl_args = [ + search: search, + posts: search |> Post.search() |> Repo.all(), + comments: search |> Comment.search() |> Repo.all() + ] - {"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) + content = render_template("search", tpl_args) conn |> put_resp_header("content-type", "text/html; charset=utf-8") diff --git a/priv/templates/http/index.eex b/priv/templates/http/index.eex index ae94ff1..d09dc63 100644 --- a/priv/templates/http/index.eex +++ b/priv/templates/http/index.eex @@ -9,15 +9,11 @@
-
- +
+
-
- -
- -
+
diff --git a/priv/templates/http/search.eex b/priv/templates/http/search.eex new file mode 100644 index 0000000..9a1daad --- /dev/null +++ b/priv/templates/http/search.eex @@ -0,0 +1,61 @@ +

Search: <%= search %>

+ + + +
+
+
+
+ <%= for post <- posts do %> +
+
+
<%= post.title %>
+
+ + <%= post.subreddit %> - + <%= post.num_comments %> comment(s) - + <%= Calendar.strftime(post.posted_at, "%Y-%m-%d") %> + +
+
+
+ <% end %> +
+
+
+ +
+
+
+ <%= for comment <- comments do %> +
+
+
+ <%= BdfrBrowser.RenderUtils.comment(comment.body) %> + + +
+
+
+ <% end %> +
+
+
+
diff --git a/priv/templates/http/search_comments.eex b/priv/templates/http/search_comments.eex deleted file mode 100644 index 864f2d2..0000000 --- a/priv/templates/http/search_comments.eex +++ /dev/null @@ -1,22 +0,0 @@ -

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 deleted file mode 100644 index 764e6d5..0000000 --- a/priv/templates/http/search_posts.eex +++ /dev/null @@ -1,20 +0,0 @@ -

Search: <%= search %>

- -
-
- <%= for post <- posts do %> -
-
-
<%= post.title %>
-
- - <%= post.subreddit %> - - <%= post.num_comments %> comment(s) - - <%= Calendar.strftime(post.posted_at, "%Y-%m-%d") %> - -
-
-
- <% end %> -
-