Current section

Files

Jump to
ash_json_api lib ash_json_api controllers patch_relationship.ex
Raw

lib/ash_json_api/controllers/patch_relationship.ex

# SPDX-FileCopyrightText: 2019 ash_json_api contributors <https://github.com/ash-project/ash_json_api/graphs/contributors>
#
# SPDX-License-Identifier: MIT
defmodule AshJsonApi.Controllers.PatchRelationship do
@moduledoc false
alias AshJsonApi.Controllers.{Helpers, Response}
alias AshJsonApi.Request
def init(options) do
# initialize options
options
end
def call(conn, options) do
action =
options[:action] || Ash.Resource.Info.primary_action!(options[:resource], :update)
domain = options[:domain]
route = options[:route]
all_domains = options[:all_domains]
argument =
action.arguments
|> Enum.find(fn argument ->
argument.name == options[:relationship]
end)
relationship =
Enum.find_value(action.changes, fn
%{change: {Ash.Resource.Change.ManageRelationship, opts}} ->
opts[:argument] == argument.name && opts[:relationship] &&
Ash.Resource.Info.relationship(options[:resource], opts[:relationship])
_ ->
nil
end)
if !relationship do
raise "Resource #{inspect(options[:resource])} must have a `change manage_relationship` with relationship #{options[:relationship]} for route #{inspect(options[:route])}"
end
if !argument do
raise "Action #{action.name} must have an argument #{options[:relationship]} for route #{inspect(options[:route])}"
end
conn
|> Request.from(options[:resource], action, domain, all_domains, route, options[:prefix])
|> Helpers.fetch_record_from_path()
|> Helpers.replace_relationship(relationship.name)
|> Helpers.fetch_metadata()
|> Helpers.render_or_render_errors(conn, fn conn, request ->
Response.render_many_relationship(conn, request, 200, relationship)
end)
end
end