2024-05-21 16:52:37 +00:00
|
|
|
defmodule JOL.Blog do
|
2024-09-24 16:31:41 +00:00
|
|
|
alias JOL.Blog.{Post, Tag}
|
|
|
|
alias JOL.Repo
|
|
|
|
import Ecto.Query
|
2024-07-30 15:51:18 +00:00
|
|
|
|
2024-06-04 18:09:14 +00:00
|
|
|
defmodule NotFoundError do
|
|
|
|
defexception [:message, plug_status: 404]
|
|
|
|
end
|
2024-05-21 16:43:36 +00:00
|
|
|
|
2024-09-24 14:56:54 +00:00
|
|
|
def all_posts do
|
|
|
|
[]
|
|
|
|
end
|
2024-05-21 16:43:36 +00:00
|
|
|
|
2024-09-24 14:56:54 +00:00
|
|
|
def unique_tag_list do
|
2024-09-24 16:31:41 +00:00
|
|
|
Repo.all(Tag, order_by: :name)
|
2024-09-24 14:56:54 +00:00
|
|
|
end
|
2024-06-04 18:09:14 +00:00
|
|
|
|
|
|
|
def recent_posts(num \\ 10) do
|
2024-09-24 16:31:41 +00:00
|
|
|
Repo.all(
|
|
|
|
from p in Post,
|
|
|
|
limit: ^num,
|
|
|
|
order_by: {:desc, :published_at}
|
|
|
|
)
|
2024-06-04 18:09:14 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def get_post_by_slug!(slug) do
|
2024-09-24 16:31:41 +00:00
|
|
|
Repo.one(
|
|
|
|
from p in Post,
|
|
|
|
where: [slug: ^slug],
|
|
|
|
preload: [:tags]
|
|
|
|
)
|
2024-06-04 18:09:14 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def get_posts_by_tag!(tag) do
|
2024-09-24 16:31:41 +00:00
|
|
|
Repo.all(
|
|
|
|
from p in Post,
|
|
|
|
join: t in assoc(p, :tags),
|
|
|
|
preload: [tags: t],
|
|
|
|
where: t.name == ^tag
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
def get_tag_by_id!(id) do
|
|
|
|
Repo.one(
|
|
|
|
from t in Tag,
|
|
|
|
where: t.id == ^id,
|
|
|
|
preload: [:posts]
|
|
|
|
)
|
2024-06-04 18:09:14 +00:00
|
|
|
end
|
2024-08-25 16:56:26 +00:00
|
|
|
|
|
|
|
def format_date(date) do
|
|
|
|
Calendar.strftime(date, "%Y-%m-%d")
|
|
|
|
end
|
2024-05-21 16:43:36 +00:00
|
|
|
end
|