2020-09-11 12:26:47 +12:00
|
|
|
defmodule AshPostgres.Test.Post do
|
|
|
|
@moduledoc false
|
|
|
|
use Ash.Resource,
|
|
|
|
data_layer: AshPostgres.DataLayer
|
|
|
|
|
|
|
|
postgres do
|
2021-04-27 05:21:57 +12:00
|
|
|
table("posts")
|
|
|
|
repo(AshPostgres.TestRepo)
|
|
|
|
base_filter_sql("type = 'sponsored'")
|
2021-04-20 06:26:41 +12:00
|
|
|
|
|
|
|
check_constraints do
|
|
|
|
check_constraint(:price, "price_must_be_positive",
|
|
|
|
message: "yo, bad price",
|
|
|
|
check: "price > 0"
|
|
|
|
)
|
|
|
|
end
|
2020-09-11 12:26:47 +12:00
|
|
|
end
|
|
|
|
|
2021-02-25 07:59:49 +13:00
|
|
|
resource do
|
|
|
|
base_filter(expr(type == type(:sponsored, ^Ash.Type.Atom)))
|
|
|
|
end
|
|
|
|
|
2020-09-11 12:26:47 +12:00
|
|
|
actions do
|
2021-05-07 09:37:00 +12:00
|
|
|
read :read do
|
|
|
|
primary?(true)
|
|
|
|
end
|
|
|
|
|
|
|
|
read :paginated do
|
|
|
|
pagination(offset?: true, required?: true, countable: true)
|
|
|
|
end
|
2021-02-06 12:59:33 +13:00
|
|
|
|
|
|
|
create :create do
|
|
|
|
argument(:rating, :map)
|
|
|
|
|
2021-02-25 07:59:49 +13:00
|
|
|
change(
|
|
|
|
manage_relationship(:rating, :ratings,
|
|
|
|
on_missing: :ignore,
|
|
|
|
on_no_match: :create,
|
|
|
|
on_match: :create
|
|
|
|
)
|
|
|
|
)
|
2021-02-06 12:59:33 +13:00
|
|
|
end
|
2020-09-11 12:26:47 +12:00
|
|
|
end
|
|
|
|
|
|
|
|
attributes do
|
2021-01-27 09:07:26 +13:00
|
|
|
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)
|
2021-02-25 07:59:49 +13:00
|
|
|
attribute(:type, :atom, default: :sponsored, private?: true, writable?: false)
|
2021-04-20 06:26:41 +12:00
|
|
|
attribute(:price, :integer)
|
2021-04-27 05:21:57 +12:00
|
|
|
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)
|
2022-02-15 05:39:50 +13:00
|
|
|
attribute(:status_enum_no_cast, AshPostgres.Test.Types.StatusEnumNoCast, source: :status_enum)
|
2021-11-14 07:55:49 +13:00
|
|
|
create_timestamp(:created_at)
|
2020-09-11 12:26:47 +12:00
|
|
|
end
|
|
|
|
|
|
|
|
relationships do
|
2021-01-13 14:22:28 +13:00
|
|
|
has_many(:comments, AshPostgres.Test.Comment, destination_field: :post_id)
|
2021-01-29 13:42:55 +13:00
|
|
|
|
2022-02-10 05:49:19 +13:00
|
|
|
has_many :popular_comments, AshPostgres.Test.Comment do
|
|
|
|
destination_field(:post_id)
|
|
|
|
# filter(expr(likes > 10))
|
|
|
|
end
|
|
|
|
|
2021-01-29 13:42:55 +13:00
|
|
|
has_many(:ratings, AshPostgres.Test.Rating,
|
|
|
|
destination_field: :resource_id,
|
2021-05-14 17:20:10 +12:00
|
|
|
relationship_context: %{data_layer: %{table: "post_ratings"}}
|
2021-01-29 13:42:55 +13:00
|
|
|
)
|
2021-04-30 09:31:19 +12:00
|
|
|
|
|
|
|
many_to_many(:linked_posts, __MODULE__,
|
|
|
|
through: AshPostgres.Test.PostLink,
|
|
|
|
source_field_on_join_table: :source_post_id,
|
|
|
|
destination_field_on_join_table: :destination_post_id
|
|
|
|
)
|
2020-09-11 12:26:47 +12:00
|
|
|
end
|
2020-12-29 13:26:04 +13:00
|
|
|
|
2021-06-04 17:48:35 +12:00
|
|
|
calculations do
|
|
|
|
calculate(:c_times_p, :integer, expr(count_of_comments * count_of_linked_posts),
|
|
|
|
load: [:count_of_comments, :count_of_linked_posts]
|
|
|
|
)
|
2021-11-14 07:56:14 +13:00
|
|
|
|
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()")))
|
2021-06-04 17:48:35 +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)
|
2021-05-13 05:17:26 +12:00
|
|
|
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
|
|
|
|
|
|
|
|
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-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
|
|
|
|
|
2021-04-05 08:05:41 +12:00
|
|
|
sum(:sum_of_comment_likes, :comments, :likes)
|
2021-10-26 11:53:34 +13:00
|
|
|
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
|
2021-07-03 04:41:44 +12:00
|
|
|
|
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
|
|
|
|
|
2022-02-10 05:49:19 +13:00
|
|
|
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
|
|
|
|
|
2021-07-03 04:41:44 +12:00
|
|
|
count(:count_of_comment_ratings, [:comments, :ratings])
|
2021-07-06 06:12:21 +12:00
|
|
|
|
|
|
|
first :highest_rating, [:comments, :ratings], :score do
|
|
|
|
sort(score: :desc)
|
|
|
|
end
|
2021-11-14 07:56:14 +13:00
|
|
|
|
|
|
|
first :latest_arbitrary_timestamp, :comments, :arbitrary_timestamp do
|
|
|
|
sort(arbitrary_timestamp: :desc)
|
|
|
|
end
|
2020-12-29 13:26:04 +13:00
|
|
|
end
|
2020-09-11 12:26:47 +12:00
|
|
|
end
|