You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

97 lines
1.9 KiB

defmodule DecentralisedBookIndex.Metadata.Publisher do
use Ash.Resource,
otp_app: :decentralised_book_index,
domain: DecentralisedBookIndex.Metadata,
data_layer: AshPostgres.DataLayer,
extensions: [AshJsonApi.Resource],
authorizers: [Ash.Policy.Authorizer]
require Ash.Query
alias DecentralisedBookIndex.Metadata
policies do
bypass actor_attribute_equals(:role, :admin) do
authorize_if always()
end
policy action_type(:read) do
authorize_if always()
end
policy action_type(:create) do
authorize_if actor_attribute_equals(:role, :moderator)
end
policy action_type(:update) do
authorize_if actor_attribute_equals(:role, :moderator)
end
policy action_type(:destroy) do
authorize_if actor_attribute_equals(:role, :admin)
end
end
json_api do
type "publisher"
end
postgres do
table "publishers"
repo DecentralisedBookIndex.Repo
end
actions do
defaults [:read, :update, :destroy]
create :create do
primary? true
accept [:name]
end
create :sync_create do
accept [:id, :name, :inserted_at, :updated_at, :dbi_server_id]
end
read :by_id do
argument :id, :uuid, allow_nil?: false
get? true
filter expr(id == ^arg(:id))
end
read :search do
argument :name, :ci_string do
constraints allow_empty?: true
default ""
end
filter expr(contains(name, ^arg(:name)))
pagination offset?: true, default_limit: 10
end
update :sync do
accept [:name, :inserted_at, :updated_at, :dbi_server_id]
end
end
attributes do
uuid_primary_key :id, writable?: true
attribute :name, :string do
allow_nil? false
public? true
end
timestamps() do
writable? true
public? true
end
end
relationships do
belongs_to :dbi_server, Metadata.DBIServer
has_many :books, Metadata.Book
end
end