ash_postgres/test/support/resources/post.ex

242 lines
6.7 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
custom_indexes do
index [:uniq_custom_one, :uniq_custom_two],
unique: true,
concurrently: true,
message: "dude what the heck"
end
end
resource do
base_filter(expr(type == type(:sponsored, ^Ash.Type.Atom)))
end
actions do
2022-04-20 03:08:28 +12:00
defaults([:update, :destroy])
read :read do
primary?(true)
end
read :paginated do
pagination(offset?: true, required?: true, countable: true)
end
create :create do
primary?(true)
argument(:rating, :map)
change(
manage_relationship(:rating, :ratings,
on_missing: :ignore,
on_no_match: :create,
on_match: :create
)
)
end
end
identities do
2022-07-02 11:18:42 +12:00
identity(:uniq_one_and_two, [:uniq_one, :uniq_two])
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)
2022-02-10 06:13:11 +13:00
attribute(:status_enum, AshPostgres.Test.Types.StatusEnum)
attribute(:status_enum_no_cast, AshPostgres.Test.Types.StatusEnumNoCast, source: :status_enum)
attribute(:point, AshPostgres.Test.Point)
attribute(:uniq_one, :string)
attribute(:uniq_two, :string)
attribute(:uniq_custom_one, :string)
attribute(:uniq_custom_two, :string)
2021-11-14 07:55:49 +13:00
create_timestamp(:created_at)
update_timestamp(:updated_at)
end
2022-11-01 04:53:03 +13:00
code_interface do
define_for(AshPostgres.Test.Api)
define(:get_by_id, action: :read, get_by: [:id])
end
relationships do
2022-06-30 07:08:49 +12:00
belongs_to(:author, AshPostgres.Test.Author)
2022-08-19 06:56:36 +12:00
has_many(:comments, AshPostgres.Test.Comment, destination_attribute: :post_id)
has_many :popular_comments, AshPostgres.Test.Comment do
2022-08-19 06:56:36 +12:00
destination_attribute(:post_id)
filter(expr(likes > 10))
end
has_many :comments_containing_title, AshPostgres.Test.Comment do
manual(AshPostgres.Test.Post.CommentsContainingTitle)
end
has_many(:ratings, AshPostgres.Test.Rating,
2022-08-19 06:56:36 +12:00
destination_attribute: :resource_id,
2021-05-14 17:20:10 +12:00
relationship_context: %{data_layer: %{table: "post_ratings"}}
)
has_many(:post_links, AshPostgres.Test.PostLink,
destination_attribute: :source_post_id,
filter: [state: :active]
)
many_to_many(:linked_posts, __MODULE__,
through: AshPostgres.Test.PostLink,
join_relationship: :post_links,
2022-08-19 06:56:36 +12:00
source_attribute_on_join_resource: :source_post_id,
destination_attribute_on_join_resource: :destination_post_id
)
end
2020-12-29 13:26:04 +13:00
calculations do
calculate(:category_label, :ci_string, expr("(" <> category <> ")"))
calculate(:c_times_p, :integer, expr(count_of_comments * count_of_linked_posts),
load: [:count_of_comments, :count_of_linked_posts]
)
calculate(
:calc_returning_json,
AshPostgres.Test.Money,
expr(
fragment("""
'{"amount":100, "currency": "usd"}'::json
""")
)
)
2021-11-14 07:55:49 +13:00
calculate(
:has_future_arbitrary_timestamp,
:boolean,
expr(latest_arbitrary_timestamp > fragment("now()"))
)
calculate(:has_future_comment, :boolean, expr(latest_comment_created_at > fragment("now()")))
calculate(
:was_created_in_the_last_month,
:boolean,
expr(
# This is written in a silly way on purpose, to test a regression
2022-05-21 05:42:20 +12:00
if(
2022-10-20 18:08:35 +13:00
fragment("(? <= (? - '1 month'::interval))", now(), created_at),
2022-05-21 05:42:20 +12:00
true,
false
2022-05-21 05:42:20 +12:00
)
)
)
end
2020-12-29 13:26:04 +13:00
aggregates do
2021-01-13 14:22:28 +13:00
count(:count_of_comments, :comments)
count(:count_of_linked_posts, :linked_posts)
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
count(:count_of_comments_containing_title, :comments_containing_title)
2020-12-29 13:26:04 +13:00
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
max(:highest_comment_rating, [:comments, :ratings], :score)
min(:lowest_comment_rating, [:comments, :ratings], :score)
avg(:avg_comment_rating, [:comments, :ratings], :score)
custom(:comment_authors, [:comments, :author], :string) do
implementation({AshPostgres.Test.StringAgg, field: :first_name, delimiter: ","})
end
2021-11-14 07:55:49 +13:00
first :latest_comment_created_at, :comments, :created_at do
sort(created_at: :desc)
end
2021-04-27 08:45:47 +12:00
list :comment_titles, :comments, :title do
sort(title: :asc_nils_last)
end
list :comment_titles_with_5_likes, :comments, :title do
sort(title: :asc_nils_last)
filter(expr(likes >= 5))
end
2021-04-05 08:05:41 +12:00
sum(:sum_of_comment_likes, :comments, :likes)
sum(:sum_of_comment_likes_with_default, :comments, :likes, default: 0)
2021-04-05 08:05:41 +12:00
sum :sum_of_comment_likes_called_match, :comments, :likes do
filter(title: "match")
end
2022-02-12 10:06:51 +13:00
# All of them will, but we want to test a related field
count :count_of_comments_that_have_a_post, :comments do
filter(expr(not is_nil(post.id)))
end
# All of them will, but we want to test a related field
count :count_of_comments_that_have_a_post_with_exists, :comments do
filter(expr(exists(post, not is_nil(id))))
end
count :count_of_popular_comments, :comments do
filter(expr(not is_nil(popular_ratings.id)))
end
sum :sum_of_recent_popular_comment_likes, :popular_comments, :likes do
# not(is_nil(post_category)) is silly but its here for tests
filter(expr(created_at > ago(10, :day) and not is_nil(post_category)))
end
count :count_of_recent_popular_comments, :popular_comments do
# not(is_nil(post_category)) is silly but its here for tests
filter(expr(created_at > ago(10, :day) and not is_nil(post_category)))
end
count(:count_of_comment_ratings, [:comments, :ratings])
count :count_of_popular_comment_ratings, [:comments, :ratings] do
filter(expr(score > 10))
end
list :ten_most_popular_comments, [:comments, :ratings], :id do
filter(expr(score > 10))
sort(score: :desc)
end
first :highest_rating, [:comments, :ratings], :score do
sort(score: :desc)
end
first :latest_arbitrary_timestamp, :comments, :arbitrary_timestamp do
sort(arbitrary_timestamp: :desc)
end
2020-12-29 13:26:04 +13:00
end
end