ash_postgres/test/support/resources/post.ex

82 lines
1.9 KiB
Elixir
Raw Normal View History

defmodule AshPostgres.Test.Post do
@moduledoc false
use Ash.Resource,
data_layer: AshPostgres.DataLayer
postgres do
table("posts")
repo(AshPostgres.TestRepo)
base_filter_sql("type = 'sponsored'")
check_constraints do
check_constraint(:price, "price_must_be_positive",
message: "yo, bad price",
check: "price > 0"
)
end
end
resource do
base_filter(expr(type == type(:sponsored, ^Ash.Type.Atom)))
end
actions do
2021-01-13 14:22:28 +13:00
read(:read)
create :create do
argument(:rating, :map)
change(
manage_relationship(:rating, :ratings,
on_missing: :ignore,
on_no_match: :create,
on_match: :create
)
)
end
end
attributes do
uuid_primary_key(:id, writable?: true)
2021-01-13 14:22:28 +13:00
attribute(:title, :string)
attribute(:score, :integer)
attribute(:public, :boolean)
2021-01-24 16:45:15 +13:00
attribute(:category, :ci_string)
attribute(:type, :atom, default: :sponsored, private?: true, writable?: false)
attribute(:price, :integer)
attribute(:decimal, :decimal, default: Decimal.new(0))
2021-04-22 05:49:53 +12:00
attribute(:status, AshPostgres.Test.Types.Status)
end
relationships do
2021-01-13 14:22:28 +13:00
has_many(:comments, AshPostgres.Test.Comment, destination_field: :post_id)
has_many(:ratings, AshPostgres.Test.Rating,
destination_field: :resource_id,
context: %{data_layer: %{table: "post_ratings"}}
)
end
2020-12-29 13:26:04 +13:00
aggregates do
2021-01-13 14:22:28 +13:00
count(:count_of_comments, :comments)
2020-12-29 13:26:04 +13:00
count :count_of_comments_called_match, :comments do
2021-01-13 14:22:28 +13:00
filter(title: "match")
2020-12-29 13:26:04 +13:00
end
first :first_comment, :comments, :title do
2021-01-13 14:22:28 +13:00
sort(title: :asc_nils_last)
2020-12-29 13:26:04 +13:00
end
2021-04-05 08:05:41 +12:00
2021-04-27 08:45:47 +12:00
list :comment_titles, :comments, :title do
sort(title: :asc_nils_last)
end
2021-04-05 08:05:41 +12:00
sum(:sum_of_comment_likes, :comments, :likes)
sum :sum_of_comment_likes_called_match, :comments, :likes do
filter(title: "match")
end
2020-12-29 13:26:04 +13:00
end
end