Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions lib/access/action_helper.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# SPDX-FileCopyrightText: 2025 diffo_example contributors <https://github.com/diffo-dev/diffo_example/graphs.contributors>
#
# SPDX-License-Identifier: MIT

defmodule DiffoExample.Access.ActionHelper do
@moduledoc """
Diffo - TMF Service and Resource Management with a difference

ActionHelper - helping with actions
"""

alias Diffo.Provider.Instance.Specification
alias Diffo.Provider.Instance.Relationship
alias Diffo.Provider.Instance.Feature
alias Diffo.Provider.Instance.Characteristic
alias Diffo.Provider.Instance.Place
alias Diffo.Provider.Instance.Party
alias DiffoExample.Access

@doc """
build before_action helper, injects instance dsl configuration into the changeset
"""
def build_before(changeset) do
changeset
|> Specification.set_specified_by_argument()
|> Feature.set_features_argument()
|> Characteristic.set_characteristics_argument()
end

@doc """
build after_action helper, relates TMF entities to the new instance
"""
def build_after(changeset, result, get_by_id_method) do
with {:ok, result} <- Specification.relate_instance(result, changeset),
{:ok, result} <- Relationship.relate_instance(result, changeset),
{:ok, result} <- Feature.relate_instance(result, changeset),
{:ok, result} <- Characteristic.relate_instance(result, changeset),
{:ok, result} <- Place.relate_instance(result, changeset),
{:ok, result} <- Party.relate_instance(result, changeset),
{:ok, result} <- apply(Access, get_by_id_method, [result.id]),
do: {:ok, result}
end
end
42 changes: 12 additions & 30 deletions lib/access/resources/cable.ex
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,13 @@ defmodule DiffoExample.Access.Cable do
"""

alias Diffo.Provider.BaseInstance
alias Diffo.Provider.Instance.Specification
alias Diffo.Provider.Instance.Relationship
alias Diffo.Provider.Instance.Feature
alias Diffo.Provider.Instance.Characteristic
alias Diffo.Provider.Instance.Place
alias Diffo.Provider.Instance.Party
alias Diffo.Provider.Assigner
alias Diffo.Provider.Assignment

alias DiffoExample.Access
alias DiffoExample.Access.ActionHelper

use Ash.Resource,
fragments: [BaseInstance],
Expand Down Expand Up @@ -56,25 +53,10 @@ defmodule DiffoExample.Access.Cable do

change set_attribute(:type, :resource)

change before_action(fn changeset, _context ->
changeset
|> Specification.set_specified_by_argument()
|> Feature.set_features_argument()
|> Characteristic.set_characteristics_argument()
end)
change before_action(fn changeset, _context -> ActionHelper.build_before(changeset) end)

change after_action(fn changeset, result, _context ->
with {:ok, with_specification} <- Specification.relate_instance(result, changeset),
{:ok, with_relationships} <-
Relationship.relate_instance(with_specification, changeset),
{:ok, with_features} <-
Feature.relate_instance(with_relationships, changeset),
{:ok, with_characteristics} <-
Characteristic.relate_instance(with_features, changeset),
{:ok, with_places} <- Place.relate_instance(with_characteristics, changeset),
{:ok, _with_parties} <- Party.relate_instance(with_places, changeset),
{:ok, cable} <- Access.get_cable_by_id(result.id),
do: {:ok, cable}
ActionHelper.build_after(changeset, result, :get_cable_by_id)
end)

change load [:href]
Expand All @@ -86,9 +68,9 @@ defmodule DiffoExample.Access.Cable do
argument :characteristic_value_updates, {:array, :term}

change after_action(fn changeset, result, _context ->
with {:ok, _result} <- Characteristic.update_values(result, changeset),
{:ok, cable} <- Access.get_cable_by_id(result.id),
do: {:ok, cable}
with {:ok, result} <- Characteristic.update_values(result, changeset),
{:ok, result} <- Access.get_cable_by_id(result.id),
do: {:ok, result}
end)
end

Expand All @@ -97,9 +79,9 @@ defmodule DiffoExample.Access.Cable do
argument :relationships, {:array, :struct}

change after_action(fn changeset, result, _context ->
with {:ok, _cable} <- Relationship.relate_instance(result, changeset),
{:ok, cable} <- Access.get_cable_by_id(result.id),
do: {:ok, cable}
with {:ok, result} <- Relationship.relate_instance(result, changeset),
{:ok, result} <- Access.get_cable_by_id(result.id),
do: {:ok, result}
end)
end

Expand All @@ -108,9 +90,9 @@ defmodule DiffoExample.Access.Cable do
argument :assignment, :struct, constraints: [instance_of: Assignment]

change after_action(fn changeset, result, _context ->
with {:ok, _cable} <- Assigner.assign(result, changeset, :pairs, :pair),
{:ok, cable} <- Access.get_cable_by_id(result.id),
do: {:ok, cable}
with {:ok, result} <- Assigner.assign(result, changeset, :pairs, :pair),
{:ok, result} <- Access.get_cable_by_id(result.id),
do: {:ok, result}
end)
end
end
Expand Down
42 changes: 12 additions & 30 deletions lib/access/resources/card.ex
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,13 @@ defmodule DiffoExample.Access.Card do
"""

alias Diffo.Provider.BaseInstance
alias Diffo.Provider.Instance.Specification
alias Diffo.Provider.Instance.Relationship
alias Diffo.Provider.Instance.Feature
alias Diffo.Provider.Instance.Characteristic
alias Diffo.Provider.Instance.Place
alias Diffo.Provider.Instance.Party
alias Diffo.Provider.Assigner
alias Diffo.Provider.Assignment

alias DiffoExample.Access
alias DiffoExample.Access.ActionHelper

use Ash.Resource,
fragments: [BaseInstance],
Expand Down Expand Up @@ -56,25 +53,10 @@ defmodule DiffoExample.Access.Card do

change set_attribute(:type, :resource)

change before_action(fn changeset, _context ->
changeset
|> Specification.set_specified_by_argument()
|> Feature.set_features_argument()
|> Characteristic.set_characteristics_argument()
end)
change before_action(fn changeset, _context -> ActionHelper.build_before(changeset) end)

change after_action(fn changeset, result, _context ->
with {:ok, with_specification} <- Specification.relate_instance(result, changeset),
{:ok, with_relationships} <-
Relationship.relate_instance(with_specification, changeset),
{:ok, with_features} <-
Feature.relate_instance(with_relationships, changeset),
{:ok, with_characteristics} <-
Characteristic.relate_instance(with_features, changeset),
{:ok, with_places} <- Place.relate_instance(with_characteristics, changeset),
{:ok, _with_parties} <- Party.relate_instance(with_places, changeset),
{:ok, card} <- Access.get_card_by_id(result.id),
do: {:ok, card}
ActionHelper.build_after(changeset, result, :get_card_by_id)
end)

change load [:href]
Expand All @@ -86,9 +68,9 @@ defmodule DiffoExample.Access.Card do
argument :characteristic_value_updates, {:array, :term}

change after_action(fn changeset, result, _context ->
with {:ok, _result} <- Characteristic.update_values(result, changeset),
{:ok, card} <- Access.get_card_by_id(result.id),
do: {:ok, card}
with {:ok, result} <- Characteristic.update_values(result, changeset),
{:ok, result} <- Access.get_card_by_id(result.id),
do: {:ok, result}
end)
end

Expand All @@ -97,9 +79,9 @@ defmodule DiffoExample.Access.Card do
argument :relationships, {:array, :struct}

change after_action(fn changeset, result, _context ->
with {:ok, _card} <- Relationship.relate_instance(result, changeset),
{:ok, card} <- Access.get_card_by_id(result.id),
do: {:ok, card}
with {:ok, result} <- Relationship.relate_instance(result, changeset),
{:ok, result} <- Access.get_card_by_id(result.id),
do: {:ok, result}
end)
end

Expand All @@ -108,9 +90,9 @@ defmodule DiffoExample.Access.Card do
argument :assignment, :struct, constraints: [instance_of: Assignment]

change after_action(fn changeset, result, _context ->
with {:ok, _card} <- Assigner.assign(result, changeset, :ports, :port),
{:ok, card} <- Access.get_card_by_id(result.id),
do: {:ok, card}
with {:ok, result} <- Assigner.assign(result, changeset, :ports, :port),
{:ok, result} <- Access.get_card_by_id(result.id),
do: {:ok, result}
end)
end
end
Expand Down
36 changes: 9 additions & 27 deletions lib/access/resources/path.ex
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,11 @@ defmodule DiffoExample.Access.Path do
"""

alias Diffo.Provider.BaseInstance
alias Diffo.Provider.Instance.Specification
alias Diffo.Provider.Instance.Relationship
alias Diffo.Provider.Instance.Feature
alias Diffo.Provider.Instance.Characteristic
alias Diffo.Provider.Instance.Place
alias Diffo.Provider.Instance.Party

alias DiffoExample.Access
alias DiffoExample.Access.ActionHelper

use Ash.Resource,
fragments: [BaseInstance],
Expand Down Expand Up @@ -53,25 +50,10 @@ defmodule DiffoExample.Access.Path do

change set_attribute(:type, :resource)

change before_action(fn changeset, _context ->
changeset
|> Specification.set_specified_by_argument()
|> Feature.set_features_argument()
|> Characteristic.set_characteristics_argument()
end)
change before_action(fn changeset, _context -> ActionHelper.build_before(changeset) end)

change after_action(fn changeset, result, _context ->
with {:ok, with_specification} <- Specification.relate_instance(result, changeset),
{:ok, with_relationships} <-
Relationship.relate_instance(with_specification, changeset),
{:ok, with_features} <-
Feature.relate_instance(with_relationships, changeset),
{:ok, with_characteristics} <-
Characteristic.relate_instance(with_features, changeset),
{:ok, with_places} <- Place.relate_instance(with_characteristics, changeset),
{:ok, _with_parties} <- Party.relate_instance(with_places, changeset),
{:ok, path} <- Access.get_path_by_id(result.id),
do: {:ok, path}
ActionHelper.build_after(changeset, result, :get_path_by_id)
end)

change load [:href]
Expand All @@ -83,9 +65,9 @@ defmodule DiffoExample.Access.Path do
argument :characteristic_value_updates, {:array, :term}

change after_action(fn changeset, result, _context ->
with {:ok, _result} <- Characteristic.update_values(result, changeset),
{:ok, path} <- Access.get_path_by_id(result.id),
do: {:ok, path}
with {:ok, result} <- Characteristic.update_values(result, changeset),
{:ok, result} <- Access.get_path_by_id(result.id),
do: {:ok, result}
end)
end

Expand All @@ -94,9 +76,9 @@ defmodule DiffoExample.Access.Path do
argument :relationships, {:array, :struct}

change after_action(fn changeset, result, _context ->
with {:ok, _path} <- Relationship.relate_instance(result, changeset),
{:ok, path} <- Access.get_path_by_id(result.id),
do: {:ok, path}
with {:ok, result} <- Relationship.relate_instance(result, changeset),
{:ok, result} <- Access.get_path_by_id(result.id),
do: {:ok, result}
end)
end
end
Expand Down
42 changes: 12 additions & 30 deletions lib/access/resources/shelf.ex
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,13 @@ defmodule DiffoExample.Access.Shelf do
"""

alias Diffo.Provider.BaseInstance
alias Diffo.Provider.Instance.Specification
alias Diffo.Provider.Instance.Relationship
alias Diffo.Provider.Instance.Feature
alias Diffo.Provider.Instance.Characteristic
alias Diffo.Provider.Instance.Place
alias Diffo.Provider.Instance.Party
alias Diffo.Provider.Assigner
alias Diffo.Provider.Assignment

alias DiffoExample.Access
alias DiffoExample.Access.ActionHelper

use Ash.Resource,
fragments: [BaseInstance],
Expand Down Expand Up @@ -56,25 +53,10 @@ defmodule DiffoExample.Access.Shelf do

change set_attribute(:type, :resource)

change before_action(fn changeset, _context ->
changeset
|> Specification.set_specified_by_argument()
|> Feature.set_features_argument()
|> Characteristic.set_characteristics_argument()
end)
change before_action(fn changeset, _context -> ActionHelper.build_before(changeset) end)

change after_action(fn changeset, result, _context ->
with {:ok, with_specification} <- Specification.relate_instance(result, changeset),
{:ok, with_relationships} <-
Relationship.relate_instance(with_specification, changeset),
{:ok, with_features} <-
Feature.relate_instance(with_relationships, changeset),
{:ok, with_characteristics} <-
Characteristic.relate_instance(with_features, changeset),
{:ok, with_places} <- Place.relate_instance(with_characteristics, changeset),
{:ok, _with_parties} <- Party.relate_instance(with_places, changeset),
{:ok, shelf} <- Access.get_shelf_by_id(result.id),
do: {:ok, shelf}
ActionHelper.build_after(changeset, result, :get_shelf_by_id)
end)

change load [:href]
Expand All @@ -86,9 +68,9 @@ defmodule DiffoExample.Access.Shelf do
argument :characteristic_value_updates, {:array, :term}

change after_action(fn changeset, result, _context ->
with {:ok, _result} <- Characteristic.update_values(result, changeset),
{:ok, card} <- Access.get_shelf_by_id(result.id),
do: {:ok, card}
with {:ok, result} <- Characteristic.update_values(result, changeset),
{:ok, result} <- Access.get_shelf_by_id(result.id),
do: {:ok, result}
end)
end

Expand All @@ -97,9 +79,9 @@ defmodule DiffoExample.Access.Shelf do
argument :relationships, {:array, :struct}

change after_action(fn changeset, result, _context ->
with {:ok, _shelf} <- Relationship.relate_instance(result, changeset),
{:ok, shelf} <- Access.get_shelf_by_id(result.id),
do: {:ok, shelf}
with {:ok, result} <- Relationship.relate_instance(result, changeset),
{:ok, result} <- Access.get_shelf_by_id(result.id),
do: {:ok, result}
end)
end

Expand All @@ -108,9 +90,9 @@ defmodule DiffoExample.Access.Shelf do
argument :assignment, :struct, constraints: [instance_of: Assignment]

change after_action(fn changeset, result, _context ->
with {:ok, _card} <- Assigner.assign(result, changeset, :slots, :slot),
{:ok, card} <- Access.get_shelf_by_id(result.id),
do: {:ok, card}
with {:ok, result} <- Assigner.assign(result, changeset, :slots, :slot),
{:ok, result} <- Access.get_shelf_by_id(result.id),
do: {:ok, result}
end)
end
end
Expand Down
Loading