defmodule TestApp.Accounts.User do
use Ash.Resource, domain: TestApp.Accounts
actions do
defaults [:read]
create :create do
accept [:email, :first_name, :last_name]
end
create :add_to_hub do
#
# ?
#
end
end
attributes do
uuid_v7_primary_key :id
attribute :email, :string do
allow_nil? false
public? true
end
end
relationships do
many_to_many :hubs, TestApp.Hubs.Hub do
through TestApp.Hubs.UsersToHubs
source_attribute_on_join_resource :user_id
destination_attribute_on_join_resource :hub_id
end
end
end
defmodule TestApp.Hubs.UsersToHubs do
use Ash.Resource, domain: TestApp.Hubs
actions do
defaults [:read, :destroy, create: :*, update: :*]
end
attributes do
create_timestamp :inserted_at
update_timestamp :updated_at
end
relationships do
belongs_to :hub,
TestApp.Hubs.Hub,
primary_key?: true,
allow_nil?: false
belongs_to :user,
TestApp.Accounts.User,
primary_key?: true,
allow_nil?: false
end
end
defmodule TestApp.Accounts.User do
use Ash.Resource, domain: TestApp.Accounts
actions do
defaults [:read]
create :create do
accept [:email, :first_name, :last_name]
end
create :add_to_hub do
#
# ?
#
end
end
attributes do
uuid_v7_primary_key :id
attribute :email, :string do
allow_nil? false
public? true
end
end
relationships do
many_to_many :hubs, TestApp.Hubs.Hub do
through TestApp.Hubs.UsersToHubs
source_attribute_on_join_resource :user_id
destination_attribute_on_join_resource :hub_id
end
end
end
defmodule TestApp.Hubs.UsersToHubs do
use Ash.Resource, domain: TestApp.Hubs
actions do
defaults [:read, :destroy, create: :*, update: :*]
end
attributes do
create_timestamp :inserted_at
update_timestamp :updated_at
end
relationships do
belongs_to :hub,
TestApp.Hubs.Hub,
primary_key?: true,
allow_nil?: false
belongs_to :user,
TestApp.Accounts.User,
primary_key?: true,
allow_nil?: false
end
end