55 lines
930 B
Elixir
55 lines
930 B
Elixir
defmodule JOL.Blog do
|
|
alias JOL.Blog.{Post, Tag}
|
|
alias JOL.Repo
|
|
import Ecto.Query
|
|
|
|
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
|
|
|
|
def format_date(date) do
|
|
Calendar.strftime(date, "%Y-%m-%d")
|
|
end
|
|
end
|