JOL/lib/jol/blog.ex

55 lines
930 B
Elixir
Raw Normal View History

defmodule JOL.Blog do
alias JOL.Blog.{Post, Tag}
alias JOL.Repo
import Ecto.Query
2024-07-30 15:51:18 +00:00
defmodule NotFoundError do
defexception [:message, plug_status: 404]
end
def all_posts do
[]
end
def unique_tag_list do
Repo.all(Tag, order_by: :name)
end
def recent_posts(num \\ 10) do
Repo.all(
from p in Post,
limit: ^num,
order_by: {:desc, :published_at}
)
end
def get_post_by_slug!(slug) do
Repo.one(
from p in Post,
where: [slug: ^slug],
preload: [:tags]
)
end
def get_posts_by_tag!(tag) do
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]
)
end
2024-08-25 16:56:26 +00:00
def format_date(date) do
Calendar.strftime(date, "%Y-%m-%d")
end
end