2023-08-13 23:18:55 +00:00
|
|
|
defmodule BdfrBrowser.Post do
|
|
|
|
use Ecto.Schema
|
|
|
|
|
|
|
|
import Ecto.Query, only: [from: 2]
|
|
|
|
|
|
|
|
alias BdfrBrowser.{Comment, Subreddit}
|
|
|
|
|
|
|
|
@primary_key {:id, :string, autogenerate: false}
|
|
|
|
|
|
|
|
schema "posts" do
|
|
|
|
field :title, :string
|
|
|
|
field :selftext, :string
|
|
|
|
field :url, :string
|
|
|
|
field :permalink, :string
|
|
|
|
field :author, :string
|
|
|
|
field :upvote_ratio, :float
|
|
|
|
field :posted_at, :utc_datetime
|
|
|
|
field :filename, :string
|
|
|
|
|
|
|
|
belongs_to :subreddit, Subreddit
|
|
|
|
has_many :comments, Comment
|
|
|
|
end
|
|
|
|
|
|
|
|
def date_listing(subreddit) do
|
|
|
|
from(p in __MODULE__,
|
|
|
|
select: fragment("to_char(?, 'YYYY-MM')", p.posted_at),
|
|
|
|
where: p.subreddit_id == ^subreddit.id,
|
|
|
|
distinct: true,
|
|
|
|
order_by: [desc: fragment("to_char(?, 'YYYY-MM')", p.posted_at)]
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
def during_month(subreddit, month_str) do
|
|
|
|
{:ok, d} = Date.from_iso8601("#{month_str}-01")
|
|
|
|
during_range(subreddit, Date.beginning_of_month(d), Date.end_of_month(d))
|
|
|
|
end
|
|
|
|
|
|
|
|
def during_range(subreddit, start_date, end_date) do
|
|
|
|
from(p in __MODULE__,
|
2023-08-16 11:22:57 +00:00
|
|
|
left_join: c in assoc(p, :comments),
|
2023-08-13 23:18:55 +00:00
|
|
|
select: %{id: p.id, title: p.title, author: p.author, posted_at: p.posted_at, num_comments: count(c.id)},
|
|
|
|
where:
|
|
|
|
p.subreddit_id == ^subreddit.id and type(p.posted_at, :date) >= ^start_date and
|
|
|
|
type(p.posted_at, :date) <= ^end_date,
|
|
|
|
order_by: [desc: p.posted_at],
|
|
|
|
group_by: p.id
|
|
|
|
)
|
|
|
|
end
|
2023-08-15 13:03:46 +00:00
|
|
|
|
2023-08-15 18:52:53 +00:00
|
|
|
def get_full(id) do
|
|
|
|
from(p in __MODULE__,
|
|
|
|
where: p.id == ^id,
|
|
|
|
preload: [
|
|
|
|
comments:
|
|
|
|
^from(c in Comment,
|
|
|
|
where: is_nil(c.parent_id),
|
|
|
|
order_by: [asc: c.posted_at],
|
|
|
|
preload: [children: ^from(c1 in Comment, order_by: [asc: c1.posted_at])]
|
|
|
|
)
|
|
|
|
]
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2023-08-15 13:03:46 +00:00
|
|
|
def by_author(author) do
|
|
|
|
from(p in __MODULE__,
|
2023-08-16 11:22:57 +00:00
|
|
|
left_join: c in assoc(p, :comments),
|
2023-08-15 13:03:46 +00:00
|
|
|
join: s in assoc(p, :subreddit),
|
|
|
|
select: %{
|
|
|
|
id: p.id,
|
|
|
|
title: p.title,
|
|
|
|
posted_at: p.posted_at,
|
|
|
|
num_comments: count(c.id),
|
|
|
|
subreddit: s.name,
|
|
|
|
date: fragment("to_char(?, 'YYYY-MM')", p.posted_at)
|
|
|
|
},
|
|
|
|
where: p.author == ^author,
|
|
|
|
order_by: [desc: p.posted_at],
|
|
|
|
group_by: [p.id, s.name]
|
|
|
|
)
|
|
|
|
end
|
2023-08-18 14:55:47 +00:00
|
|
|
|
|
|
|
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
|
2023-08-13 23:18:55 +00:00
|
|
|
end
|