2021-07-13 08:29:55 +12:00
|
|
|
defmodule AshPhoenix.Test.Post do
|
2021-07-13 09:19:17 +12:00
|
|
|
@moduledoc false
|
2021-07-13 08:29:55 +12:00
|
|
|
use Ash.Resource, data_layer: Ash.DataLayer.Ets
|
|
|
|
|
|
|
|
ets do
|
|
|
|
private?(true)
|
|
|
|
end
|
|
|
|
|
|
|
|
attributes do
|
|
|
|
uuid_primary_key(:id)
|
2021-07-16 06:09:15 +12:00
|
|
|
attribute(:text, :string, allow_nil?: false)
|
2021-11-30 15:40:46 +13:00
|
|
|
attribute(:title, :string)
|
2021-07-13 08:29:55 +12:00
|
|
|
end
|
|
|
|
|
|
|
|
actions do
|
2022-05-17 07:34:33 +12:00
|
|
|
defaults([:read, :destroy])
|
|
|
|
|
2021-07-13 08:29:55 +12:00
|
|
|
create :create do
|
2022-05-17 07:34:33 +12:00
|
|
|
primary?(true)
|
2022-05-14 12:45:07 +12:00
|
|
|
argument(:author, :map, allow_nil?: true)
|
2021-07-13 08:29:55 +12:00
|
|
|
argument(:comments, {:array, :map})
|
2021-07-16 06:09:15 +12:00
|
|
|
argument(:linked_posts, {:array, :map})
|
|
|
|
change(manage_relationship(:comments, type: :direct_control))
|
|
|
|
change(manage_relationship(:linked_posts, type: :direct_control))
|
2022-05-14 12:45:07 +12:00
|
|
|
change(manage_relationship(:author, type: :direct_control, on_missing: :unrelate))
|
2021-07-13 08:29:55 +12:00
|
|
|
end
|
|
|
|
|
2022-08-23 06:31:28 +12:00
|
|
|
create :create_with_non_map_relationship_args do
|
|
|
|
argument(:comment_ids, {:array, :integer})
|
|
|
|
change(manage_relationship(:comment_ids, :comments, type: :replace))
|
|
|
|
end
|
|
|
|
|
2022-05-18 02:39:46 +12:00
|
|
|
create :create_author_required do
|
|
|
|
argument(:author, :map, allow_nil?: false)
|
|
|
|
change(manage_relationship(:author, type: :direct_control, on_missing: :unrelate))
|
|
|
|
end
|
|
|
|
|
2022-03-22 15:24:19 +13:00
|
|
|
update :update_with_replace do
|
|
|
|
argument(:comments, {:array, :map})
|
|
|
|
change(manage_relationship(:comments, type: :replace))
|
|
|
|
end
|
|
|
|
|
2021-07-13 08:29:55 +12:00
|
|
|
update :update do
|
2022-03-22 15:24:19 +13:00
|
|
|
primary?(true)
|
2022-05-14 12:45:07 +12:00
|
|
|
argument(:author, :map, allow_nil?: true)
|
2021-07-13 08:29:55 +12:00
|
|
|
argument(:comments, {:array, :map})
|
2021-07-16 06:09:15 +12:00
|
|
|
change(manage_relationship(:comments, type: :direct_control))
|
2022-05-14 12:45:07 +12:00
|
|
|
change(manage_relationship(:author, type: :direct_control, on_missing: :unrelate))
|
2021-07-13 08:29:55 +12:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
relationships do
|
|
|
|
has_many(:comments, AshPhoenix.Test.Comment)
|
2022-05-14 12:45:07 +12:00
|
|
|
belongs_to(:author, AshPhoenix.Test.Author)
|
2021-07-13 08:29:55 +12:00
|
|
|
has_one(:featured_comment, AshPhoenix.Test.Comment, read_action: :featured)
|
|
|
|
|
|
|
|
many_to_many(:linked_posts, AshPhoenix.Test.Post,
|
|
|
|
through: AshPhoenix.Test.PostLink,
|
2022-08-24 06:56:13 +12:00
|
|
|
destination_attribute_on_join_resource: :destination_post_id,
|
|
|
|
source_attribute_on_join_resource: :source_post_id
|
2021-07-13 08:29:55 +12:00
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|