Skip to content

Instantly share code, notes, and snippets.

@thejohnnybot
Created January 1, 2024 00:02
Show Gist options
  • Select an option

  • Save thejohnnybot/7eb75821f695ad7cbdf85c88f40c36f3 to your computer and use it in GitHub Desktop.

Select an option

Save thejohnnybot/7eb75821f695ad7cbdf85c88f40c36f3 to your computer and use it in GitHub Desktop.
ash resource
defmodule ExPay.Payment.Transaction do
use Ash.Resource,
data_layer: AshPostgres.DataLayer,
extensions: [AshJsonApi.Resource]
json_api do
type "transactions"
routes do
base "/transactions"
get :get_by_id
index :read
post :create
patch :update
delete :destroy
end
end
postgres do
table "transactions"
repo ExPay.Repo
end
actions do
defaults [:create, :update, :destroy]
read :read do
pagination keyset?: true, default_limit: 10, countable: :by_default
end
read :by_id do
argument :id, :uuid, allow_nil?: false
get? true
filter expr(id == ^arg(:id))
end
end
aggregates do
sum :sum_of_usd_balances, :transactions, :money do
filter expr(
fragment("(?).currency_code", money) == "USD"
)
end
end
code_interface do
define_for ExPay.Payment
define :create, action: :create
define :read, action: :read
define :update, action: :update
define :destroy, action: :destroy
define :get_by_id, args: [:id], action: :by_id
end
attributes do
uuid_primary_key :id
create_timestamp :created_at
update_timestamp :updated_at
attribute :money, AshMoney.Types.Money
attribute :data, :map
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment