103 lines
2.8 KiB
Elixir
103 lines
2.8 KiB
Elixir
|
defmodule Podbox.Podcast.Show do
|
||
|
@moduledoc """
|
||
|
A podcast show.
|
||
|
"""
|
||
|
|
||
|
alias Podbox.{
|
||
|
Podcast,
|
||
|
Podcast.Category,
|
||
|
Podcast.Episode,
|
||
|
Podcast.Feed,
|
||
|
Podcast.Image,
|
||
|
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?: false, public?: true
|
||
|
attribute :link, :string, allow_nil?: false, public?: true
|
||
|
attribute :description, :string, allow_nil?: false, public?: true
|
||
|
attribute :language, :string, allow_nil?: true, public?: true
|
||
|
attribute :copyright, :string, allow_nil?: true, public?: true
|
||
|
attribute :managing_editor, :string, allow_nil?: true, public?: true
|
||
|
attribute :webmaster, :string, allow_nil?: true, public?: true
|
||
|
|
||
|
attribute :pub_date, :datetime, allow_nil?: true, public?: true
|
||
|
attribute :last_build_date, :datetime, allow_nil?: true, public?: true
|
||
|
|
||
|
attribute :generator, :string, allow_nil?: true, public?: true
|
||
|
attribute :docs, :string, allow_nil?: true, public?: true
|
||
|
attribute :cloud, :string, allow_nil?: true, public?: true
|
||
|
attribute :rating, :string, allow_nil?: true, public?: true
|
||
|
attribute :ttl, :integer, allow_nil?: true, public?: true, constraints: [min: 0]
|
||
|
|
||
|
create_timestamp :inserted_at
|
||
|
update_timestamp :updated_at
|
||
|
end
|
||
|
|
||
|
relationships do
|
||
|
belongs_to :feed, Feed, attribute_writable?: true, public?: true
|
||
|
|
||
|
has_many :categories, Category do
|
||
|
relationship_context %{data_layer: %{table: "podcast_show_categories"}}
|
||
|
destination_attribute :resource_id
|
||
|
end
|
||
|
|
||
|
has_one :image, Image, public?: true
|
||
|
has_many :episodes, Episode, 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 :image, :map, allow_nil?: true, public?: true
|
||
|
argument :feed_id, :uuid, allow_nil?: false, public?: true
|
||
|
|
||
|
accept :*
|
||
|
upsert? true
|
||
|
upsert_identity :unique_per_feed
|
||
|
|
||
|
change manage_relationship(:categories, :categories, type: :append_and_remove)
|
||
|
change manage_relationship(:image, :image, type: :direct_control), where: [present(:image)]
|
||
|
change manage_relationship(:feed_id, :feed, type: :append)
|
||
|
end
|
||
|
end
|
||
|
|
||
|
sqlite do
|
||
|
repo Repo
|
||
|
table "podcast_shows"
|
||
|
end
|
||
|
|
||
|
pub_sub do
|
||
|
module Podbox.PubSub
|
||
|
prefix "podcast:show"
|
||
|
publish :create, "created"
|
||
|
end
|
||
|
|
||
|
identities do
|
||
|
identity :unique_per_feed, [:feed_id]
|
||
|
end
|
||
|
end
|