ash json api - update by other field

json_api do
type "tasks"

routes do
base "/tasks"
get :read_by_task_id, route: "/:task_id"
index :read
post :create
patch :update_by_task_id, route: "/:task_id"
end
end


actions do
default_accept :*
defaults [:create, :read, :update, :destroy]

read :read_by_task_id do
get_by :task_id
end

read :read_by_rlhf_link do
get_by :task_rlhf_link
end

update :update_by_task_id do
<<< how to read it by task id first and then uodate? >>
end
end
json_api do
type "tasks"

routes do
base "/tasks"
get :read_by_task_id, route: "/:task_id"
index :read
post :create
patch :update_by_task_id, route: "/:task_id"
end
end


actions do
default_accept :*
defaults [:create, :read, :update, :destroy]

read :read_by_task_id do
get_by :task_id
end

read :read_by_rlhf_link do
get_by :task_rlhf_link
end

update :update_by_task_id do
<<< how to read it by task id first and then uodate? >>
end
end
Here is what I want to do 1. read the task by task_id and update the params received in the json api for that task.
4 Replies
ZachDaniel
ZachDaniel2d ago
https://hexdocs.pm/ash_json_api/dsl-ashjsonapi-domain.html#json_api-routes-patch-read_action Use a different read action and then configure the route to have task_id in it
abeeshake456
abeeshake456OP2d ago
Here is the updated config, but that does not work, I am missing something.
# in json api block
patch :update_by_task_id do
route "/:task_id"
read_action :read_by_task_id
end


# in actions block

read :read_by_task_id do
get_by :task_id
end


update :update_by_task_id do
argument :task_id, :string do
allow_nil? false
end
end
# in json api block
patch :update_by_task_id do
route "/:task_id"
read_action :read_by_task_id
end


# in actions block

read :read_by_task_id do
get_by :task_id
end


update :update_by_task_id do
argument :task_id, :string do
allow_nil? false
end
end
Tasks.update_by_task_id("787878", %{task_rlhf_link: "rlhf link from json api "})
{:error,
%Ash.Error.Invalid{
errors: [
%Ash.Error.Invalid.InvalidPrimaryKey{
resource: AutoElixir.Tasker.Tasks,
value: "787878",
splode: Ash.Error,
bread_crumbs: [],
vars: [],
path: [],
stacktrace: #Splode.Stacktrace<>,
class: :invalid
}
]
}}
Tasks.update_by_task_id("787878", %{task_rlhf_link: "rlhf link from json api "})
{:error,
%Ash.Error.Invalid{
errors: [
%Ash.Error.Invalid.InvalidPrimaryKey{
resource: AutoElixir.Tasker.Tasks,
value: "787878",
splode: Ash.Error,
bread_crumbs: [],
vars: [],
path: [],
stacktrace: #Splode.Stacktrace<>,
class: :invalid
}
]
}}
ZachDaniel
ZachDaniel2d ago
Its different for AshJsonApi and the way you're calling it YOu don't need the argument on the update action AshJsonApi does
Resource
|> Ash.Query.for_read(:read_by_task_id, %{task_id: task_id})
|> Ash.bulk_update!(:update, %{...})
Resource
|> Ash.Query.for_read(:read_by_task_id, %{task_id: task_id})
|> Ash.bulk_update!(:update, %{...})
If you want a code interface that does that, you can just have the update action, and do define :update_by_task_id, get_by: [:task_id] IIRC
abeeshake456
abeeshake456OP2d ago
:thinkies: this is new to me! perfect this work! Thanks!

Did you find this page helpful?