feat: Search in certain subreddits only

This commit is contained in:
Daniel Kempkens 2023-10-04 11:01:10 +02:00
parent 38df258ce5
commit d68b03d133
Signed by: daniel
SSH key fingerprint: SHA256:Ks/MyhQYcPRQiwMKLAKquWCdCPe3JXlb1WttgnAoSeM
5 changed files with 74 additions and 6 deletions

View file

@ -42,7 +42,9 @@ defmodule BdfrBrowser.Comment do
) )
end end
def search(str) do def search(str), do: search(str, nil)
def search(str, subreddits) when is_nil(subreddits) do
search_str = "%#{str}%" search_str = "%#{str}%"
from(c in __MODULE__, from(c in __MODULE__,
@ -63,4 +65,26 @@ defmodule BdfrBrowser.Comment do
group_by: [c.id, p.id, s.name] group_by: [c.id, p.id, s.name]
) )
end 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 end

View file

@ -164,11 +164,12 @@ defmodule BdfrBrowser.HTTP.Plug do
conn = Plug.Conn.fetch_query_params(conn) conn = Plug.Conn.fetch_query_params(conn)
params = conn.query_params params = conn.query_params
search = params["search"] search = params["search"]
subreddits = params["subreddit"]
tpl_args = [ tpl_args = [
search: search, search: search,
posts: search |> Post.search() |> Repo.all(), posts: search |> Post.search(subreddits) |> Repo.all(),
comments: search |> Comment.search() |> Repo.all() comments: search |> Comment.search(subreddits) |> Repo.all()
] ]
content = render_template("search", tpl_args) content = render_template("search", tpl_args)

View file

@ -120,7 +120,9 @@ defmodule BdfrBrowser.Post do
) )
end end
def search(str) do def search(str), do: search(str, nil)
def search(str, subreddits) when is_nil(subreddits) do
search_str = "%#{str}%" search_str = "%#{str}%"
from(p in __MODULE__, from(p in __MODULE__,
@ -140,4 +142,25 @@ defmodule BdfrBrowser.Post do
group_by: [p.id, s.name] group_by: [p.id, s.name]
) )
end 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 end

View file

@ -13,12 +13,32 @@
<h2> <h2>
<%= if is_list(subreddit) do %> <%= if is_list(subreddit) do %>
<%= Enum.join(subreddit, ", ") %> Multi-Reddit
<% else %> <% else %>
<%= subreddit %> <%= subreddit %>
<% end %> <% end %>
</h2> </h2>
<form method="get" action="/search" class="row">
<div class="col-8">
<input type="text" class="form-control" placeholder="Search term" name="search" />
</div>
<%= if is_list(subreddit) do %>
<%= for s <- subreddit do %>
<input type="hidden" name="subreddit[]" value="<%= s %>" />
<% end %>
<% else %>
<input type="hidden" name="subreddit[]" value="<%= subreddit %>" />
<% end %>
<div class="col-4 text-end">
<button type="submit" class="btn btn-primary">Search</button>
</div>
</form>
<br>
<div class="row text-center"> <div class="row text-center">
<div class="d-grid gap-2 col-12 mx-auto"> <div class="d-grid gap-2 col-12 mx-auto">
<%= for date <- dates do %> <%= for date <- dates do %>

View file

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