podbox_ash/lib/podbox/podcast/resources/episode.ex

116 lines
3.2 KiB
Elixir

defmodule Podbox.Podcast.Episode do
@moduledoc """
A podcast episode
"""
alias Podbox.{
Podcast,
Podcast.Category,
Podcast.Enclosure,
Podcast.Show,
Repo
}
use Ash.Resource,
data_layer: AshSqlite.DataLayer,
domain: Podcast,
notifiers: [Ash.Notifier.PubSub]
@type t :: Ash.Resource.record()
attributes do
uuid_primary_key :id
attribute :title, :string, allow_nil?: true, public?: true
attribute :link, :string, allow_nil?: true, public?: true
attribute :description, :string, allow_nil?: true, public?: true
attribute :author, :string, allow_nil?: true, public?: true
attribute :comments, :string, allow_nil?: true, public?: true
attribute :guid, :string, allow_nil?: false, public?: true
attribute :pub_date, :datetime, allow_nil?: true, public?: true
attribute :content, :string, allow_nil?: true, public?: true
# comes from itunes extension
attribute :image, :string, allow_nil?: true, public?: true
attribute :duration, :time, allow_nil?: true, public?: true
attribute :explicit?, :boolean, allow_nil?: true, public?: true
attribute :closed_captioned?, :boolean, allow_nil?: true, public?: true
attribute :order, :string, allow_nil?: true, public?: true
attribute :subtitle, :string, allow_nil?: true, public?: true
attribute :episode, :string, allow_nil?: true, public?: true
attribute :season, :string, allow_nil?: true, public?: true
attribute :episode_type, :atom,
allow_nil?: true,
public?: true,
constraints: [one_of: [:full, :trailer, :bonus]]
create_timestamp :inserted_at
update_timestamp :updated_at
end
relationships do
belongs_to :show, Show, attribute_writable?: true, public?: true
has_many :categories, Category do
relationship_context %{data_layer: %{table: "podcast_episode_categories"}}
destination_attribute :resource_id
end
has_one :enclosure, Enclosure, public?: true
end
actions do
defaults [:read, :destroy, update: :*]
create :upsert do
argument :categories, {:array, :map},
allow_nil?: false,
public?: true,
constraints: [
items: [
fields: [
name: [type: :string, allow_nil?: false],
domain: [type: :string, allow_nil?: true]
]
]
],
default: []
argument :enclosure, :map,
allow_nil?: true,
public?: true,
constraints: [
fields: [
url: [type: :string, allow_nil?: false],
length: [type: :integer, allow_nil?: false, constraints: [min: 0]],
mime_type: [type: :string, allow_nil?: false]
]
]
accept :*
upsert? true
upsert_identity :unique_guid_per_show
change manage_relationship(:categories, :categories, type: :append_and_remove)
change manage_relationship(:enclosure, :enclosure, type: :direct_control),
where: [present(:enclosure)]
end
end
sqlite do
repo Repo
table "podcast_episodes"
end
pub_sub do
module Podbox.PubSub
prefix "podcast:episode"
publish :create, "created"
end
identities do
identity :unique_guid_per_show, [:guid, :show_id]
end
end