Files
wanderer/lib/wanderer_app/api/map_character_settings.ex
2024-12-14 23:31:42 +01:00

102 lines
2.3 KiB
Elixir

defmodule WandererApp.Api.MapCharacterSettings do
@moduledoc false
use Ash.Resource,
domain: WandererApp.Api,
data_layer: AshPostgres.DataLayer
postgres do
repo(WandererApp.Repo)
table("map_character_settings_v1")
end
code_interface do
define(:create, action: :create)
define(:destroy, action: :destroy)
define(:read_by_map,
action: :read_by_map
)
define(:by_map_filtered,
action: :by_map_filtered
)
define(:tracked_by_map_filtered,
action: :tracked_by_map_filtered
)
define(:tracked_by_map_all,
action: :tracked_by_map_all
)
define(:track, action: :track)
define(:untrack, action: :untrack)
end
actions do
default_accept [
:map_id,
:character_id,
:tracked
]
defaults [:create, :read, :update, :destroy]
read :by_map_filtered do
argument(:map_id, :string, allow_nil?: false)
argument(:character_ids, {:array, :uuid}, allow_nil?: false)
filter(expr(map_id == ^arg(:map_id) and character_id in ^arg(:character_ids)))
end
read :tracked_by_map_filtered do
argument(:map_id, :string, allow_nil?: false)
argument(:character_ids, {:array, :uuid}, allow_nil?: false)
filter(
expr(map_id == ^arg(:map_id) and tracked == true and character_id in ^arg(:character_ids))
)
end
read :read_by_map do
argument(:map_id, :string, allow_nil?: false)
filter(expr(map_id == ^arg(:map_id)))
end
read :tracked_by_map_all do
argument(:map_id, :string, allow_nil?: false)
filter(expr(map_id == ^arg(:map_id) and tracked == true))
end
update :track do
change(set_attribute(:tracked, true))
end
update :untrack do
change(set_attribute(:tracked, false))
end
end
attributes do
uuid_primary_key :id
attribute :tracked, :boolean do
default false
allow_nil? true
end
create_timestamp(:inserted_at)
update_timestamp(:updated_at)
end
relationships do
belongs_to :map, WandererApp.Api.Map, primary_key?: true, allow_nil?: false
belongs_to :character, WandererApp.Api.Character, primary_key?: true, allow_nil?: false
end
identities do
identity :uniq_map_character, [:map_id, :character_id]
end
end