55 lines
1.2 KiB
Elixir
55 lines
1.2 KiB
Elixir
defmodule Core.Posts do
|
|
@moduledoc false
|
|
|
|
alias Core.Posts.Post
|
|
|
|
def get!(id) do
|
|
Core.Repo.get!(Schema.Post, id)
|
|
end
|
|
|
|
def get_by_publish_date_and_slug!(%Date{} = publish_date, slug) when is_binary(slug) do
|
|
publish_date
|
|
|> Post.Query.where_publish_date_and_slug(slug)
|
|
|> Core.Repo.one!()
|
|
end
|
|
|
|
def change_post(%Schema.Post{} = post \\ %Schema.Post{}, attrs) do
|
|
Post.content_changeset(post, attrs)
|
|
end
|
|
|
|
def create_post(attrs) do
|
|
attrs
|
|
|> change_post()
|
|
|> Core.Repo.insert()
|
|
end
|
|
|
|
def update_post(%Schema.Post{} = post, attrs) do
|
|
post
|
|
|> change_post(attrs)
|
|
|> Core.Repo.update()
|
|
end
|
|
|
|
def publish_post(%Schema.Post{} = post, published_at \\ DateTime.utc_now()) do
|
|
post
|
|
|> Post.publish_changeset(published_at)
|
|
|> Core.Repo.update()
|
|
end
|
|
|
|
def delete_post(%Schema.Post{} = post, deleted_at \\ DateTime.utc_now()) do
|
|
post
|
|
|> Post.delete_changeset(deleted_at)
|
|
|> Core.Repo.update()
|
|
end
|
|
|
|
def unpublish_post(%Schema.Post{} = post) do
|
|
post
|
|
|> Post.unpublish_changeset()
|
|
|> Core.Repo.update()
|
|
end
|
|
|
|
def undelete_post(%Schema.Post{} = post) do
|
|
post
|
|
|> Post.undelete_changeset()
|
|
|> Core.Repo.update()
|
|
end
|
|
end
|