fix: Use left_joins in some places

This commit is contained in:
Daniel Kempkens 2023-08-16 13:22:57 +02:00
parent 7931009e90
commit 86675725ff
Signed by: daniel
SSH key fingerprint: SHA256:Ks/MyhQYcPRQiwMKLAKquWCdCPe3JXlb1WttgnAoSeM
3 changed files with 10 additions and 8 deletions

View file

@ -15,7 +15,7 @@ defmodule BdfrBrowser.Chat do
def listing do def listing do
from(c in __MODULE__, from(c in __MODULE__,
join: m in assoc(c, :messages), left_join: m in assoc(c, :messages),
select: %{id: c.id, accounts: c.accounts, num_messages: count(m.id), latest_message: max(m.posted_at)}, select: %{id: c.id, accounts: c.accounts, num_messages: count(m.id), latest_message: max(m.posted_at)},
order_by: [desc: max(m.posted_at)], order_by: [desc: max(m.posted_at)],
group_by: c.id group_by: c.id

View file

@ -25,11 +25,13 @@ defmodule BdfrBrowser.Importer do
folders = list_folders(sort: :asc) folders = list_folders(sort: :asc)
for folder <- folders do for folder <- folders do
%Subreddit{name: folder} subreddit = Repo.get_by(Subreddit, name: folder)
|> Repo.insert(
on_conflict: :nothing, if is_nil(subreddit) do
conflict_target: :name Repo.insert(%Subreddit{name: folder})
) else
subreddit
end
end end
end end

View file

@ -37,7 +37,7 @@ defmodule BdfrBrowser.Post do
def during_range(subreddit, start_date, end_date) do def during_range(subreddit, start_date, end_date) do
from(p in __MODULE__, from(p in __MODULE__,
join: c in assoc(p, :comments), left_join: c in assoc(p, :comments),
select: %{id: p.id, title: p.title, author: p.author, posted_at: p.posted_at, num_comments: count(c.id)}, select: %{id: p.id, title: p.title, author: p.author, posted_at: p.posted_at, num_comments: count(c.id)},
where: where:
p.subreddit_id == ^subreddit.id and type(p.posted_at, :date) >= ^start_date and p.subreddit_id == ^subreddit.id and type(p.posted_at, :date) >= ^start_date and
@ -63,7 +63,7 @@ defmodule BdfrBrowser.Post do
def by_author(author) do def by_author(author) do
from(p in __MODULE__, from(p in __MODULE__,
join: c in assoc(p, :comments), left_join: c in assoc(p, :comments),
join: s in assoc(p, :subreddit), join: s in assoc(p, :subreddit),
select: %{ select: %{
id: p.id, id: p.id,