Skip to content

Commit 4621476

Browse files
Merge pull request #2 from diffo-dev/1-refactor-services-and-resources
1 refactor services and resources
2 parents 5291253 + 635eb0f commit 4621476

6 files changed

Lines changed: 97 additions & 141 deletions

File tree

lib/access/action_helper.ex

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# SPDX-FileCopyrightText: 2025 diffo_example contributors <https://github.com/diffo-dev/diffo_example/graphs.contributors>
2+
#
3+
# SPDX-License-Identifier: MIT
4+
5+
defmodule DiffoExample.Access.ActionHelper do
6+
@moduledoc """
7+
Diffo - TMF Service and Resource Management with a difference
8+
9+
ActionHelper - helping with actions
10+
"""
11+
12+
alias Diffo.Provider.Instance.Specification
13+
alias Diffo.Provider.Instance.Relationship
14+
alias Diffo.Provider.Instance.Feature
15+
alias Diffo.Provider.Instance.Characteristic
16+
alias Diffo.Provider.Instance.Place
17+
alias Diffo.Provider.Instance.Party
18+
alias DiffoExample.Access
19+
20+
@doc """
21+
build before_action helper, injects instance dsl configuration into the changeset
22+
"""
23+
def build_before(changeset) do
24+
changeset
25+
|> Specification.set_specified_by_argument()
26+
|> Feature.set_features_argument()
27+
|> Characteristic.set_characteristics_argument()
28+
end
29+
30+
@doc """
31+
build after_action helper, relates TMF entities to the new instance
32+
"""
33+
def build_after(changeset, result, get_by_id_method) do
34+
with {:ok, result} <- Specification.relate_instance(result, changeset),
35+
{:ok, result} <- Relationship.relate_instance(result, changeset),
36+
{:ok, result} <- Feature.relate_instance(result, changeset),
37+
{:ok, result} <- Characteristic.relate_instance(result, changeset),
38+
{:ok, result} <- Place.relate_instance(result, changeset),
39+
{:ok, result} <- Party.relate_instance(result, changeset),
40+
{:ok, result} <- apply(Access, get_by_id_method, [result.id]),
41+
do: {:ok, result}
42+
end
43+
end

lib/access/resources/cable.ex

Lines changed: 12 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,13 @@ defmodule DiffoExample.Access.Cable do
1010
"""
1111

1212
alias Diffo.Provider.BaseInstance
13-
alias Diffo.Provider.Instance.Specification
1413
alias Diffo.Provider.Instance.Relationship
15-
alias Diffo.Provider.Instance.Feature
1614
alias Diffo.Provider.Instance.Characteristic
17-
alias Diffo.Provider.Instance.Place
18-
alias Diffo.Provider.Instance.Party
1915
alias Diffo.Provider.Assigner
2016
alias Diffo.Provider.Assignment
2117

2218
alias DiffoExample.Access
19+
alias DiffoExample.Access.ActionHelper
2320

2421
use Ash.Resource,
2522
fragments: [BaseInstance],
@@ -56,25 +53,10 @@ defmodule DiffoExample.Access.Cable do
5653

5754
change set_attribute(:type, :resource)
5855

59-
change before_action(fn changeset, _context ->
60-
changeset
61-
|> Specification.set_specified_by_argument()
62-
|> Feature.set_features_argument()
63-
|> Characteristic.set_characteristics_argument()
64-
end)
56+
change before_action(fn changeset, _context -> ActionHelper.build_before(changeset) end)
6557

6658
change after_action(fn changeset, result, _context ->
67-
with {:ok, with_specification} <- Specification.relate_instance(result, changeset),
68-
{:ok, with_relationships} <-
69-
Relationship.relate_instance(with_specification, changeset),
70-
{:ok, with_features} <-
71-
Feature.relate_instance(with_relationships, changeset),
72-
{:ok, with_characteristics} <-
73-
Characteristic.relate_instance(with_features, changeset),
74-
{:ok, with_places} <- Place.relate_instance(with_characteristics, changeset),
75-
{:ok, _with_parties} <- Party.relate_instance(with_places, changeset),
76-
{:ok, cable} <- Access.get_cable_by_id(result.id),
77-
do: {:ok, cable}
59+
ActionHelper.build_after(changeset, result, :get_cable_by_id)
7860
end)
7961

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

8870
change after_action(fn changeset, result, _context ->
89-
with {:ok, _result} <- Characteristic.update_values(result, changeset),
90-
{:ok, cable} <- Access.get_cable_by_id(result.id),
91-
do: {:ok, cable}
71+
with {:ok, result} <- Characteristic.update_values(result, changeset),
72+
{:ok, result} <- Access.get_cable_by_id(result.id),
73+
do: {:ok, result}
9274
end)
9375
end
9476

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

9981
change after_action(fn changeset, result, _context ->
100-
with {:ok, _cable} <- Relationship.relate_instance(result, changeset),
101-
{:ok, cable} <- Access.get_cable_by_id(result.id),
102-
do: {:ok, cable}
82+
with {:ok, result} <- Relationship.relate_instance(result, changeset),
83+
{:ok, result} <- Access.get_cable_by_id(result.id),
84+
do: {:ok, result}
10385
end)
10486
end
10587

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

11092
change after_action(fn changeset, result, _context ->
111-
with {:ok, _cable} <- Assigner.assign(result, changeset, :pairs, :pair),
112-
{:ok, cable} <- Access.get_cable_by_id(result.id),
113-
do: {:ok, cable}
93+
with {:ok, result} <- Assigner.assign(result, changeset, :pairs, :pair),
94+
{:ok, result} <- Access.get_cable_by_id(result.id),
95+
do: {:ok, result}
11496
end)
11597
end
11698
end

lib/access/resources/card.ex

Lines changed: 12 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,13 @@ defmodule DiffoExample.Access.Card do
1010
"""
1111

1212
alias Diffo.Provider.BaseInstance
13-
alias Diffo.Provider.Instance.Specification
1413
alias Diffo.Provider.Instance.Relationship
15-
alias Diffo.Provider.Instance.Feature
1614
alias Diffo.Provider.Instance.Characteristic
17-
alias Diffo.Provider.Instance.Place
18-
alias Diffo.Provider.Instance.Party
1915
alias Diffo.Provider.Assigner
2016
alias Diffo.Provider.Assignment
2117

2218
alias DiffoExample.Access
19+
alias DiffoExample.Access.ActionHelper
2320

2421
use Ash.Resource,
2522
fragments: [BaseInstance],
@@ -56,25 +53,10 @@ defmodule DiffoExample.Access.Card do
5653

5754
change set_attribute(:type, :resource)
5855

59-
change before_action(fn changeset, _context ->
60-
changeset
61-
|> Specification.set_specified_by_argument()
62-
|> Feature.set_features_argument()
63-
|> Characteristic.set_characteristics_argument()
64-
end)
56+
change before_action(fn changeset, _context -> ActionHelper.build_before(changeset) end)
6557

6658
change after_action(fn changeset, result, _context ->
67-
with {:ok, with_specification} <- Specification.relate_instance(result, changeset),
68-
{:ok, with_relationships} <-
69-
Relationship.relate_instance(with_specification, changeset),
70-
{:ok, with_features} <-
71-
Feature.relate_instance(with_relationships, changeset),
72-
{:ok, with_characteristics} <-
73-
Characteristic.relate_instance(with_features, changeset),
74-
{:ok, with_places} <- Place.relate_instance(with_characteristics, changeset),
75-
{:ok, _with_parties} <- Party.relate_instance(with_places, changeset),
76-
{:ok, card} <- Access.get_card_by_id(result.id),
77-
do: {:ok, card}
59+
ActionHelper.build_after(changeset, result, :get_card_by_id)
7860
end)
7961

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

8870
change after_action(fn changeset, result, _context ->
89-
with {:ok, _result} <- Characteristic.update_values(result, changeset),
90-
{:ok, card} <- Access.get_card_by_id(result.id),
91-
do: {:ok, card}
71+
with {:ok, result} <- Characteristic.update_values(result, changeset),
72+
{:ok, result} <- Access.get_card_by_id(result.id),
73+
do: {:ok, result}
9274
end)
9375
end
9476

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

9981
change after_action(fn changeset, result, _context ->
100-
with {:ok, _card} <- Relationship.relate_instance(result, changeset),
101-
{:ok, card} <- Access.get_card_by_id(result.id),
102-
do: {:ok, card}
82+
with {:ok, result} <- Relationship.relate_instance(result, changeset),
83+
{:ok, result} <- Access.get_card_by_id(result.id),
84+
do: {:ok, result}
10385
end)
10486
end
10587

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

11092
change after_action(fn changeset, result, _context ->
111-
with {:ok, _card} <- Assigner.assign(result, changeset, :ports, :port),
112-
{:ok, card} <- Access.get_card_by_id(result.id),
113-
do: {:ok, card}
93+
with {:ok, result} <- Assigner.assign(result, changeset, :ports, :port),
94+
{:ok, result} <- Access.get_card_by_id(result.id),
95+
do: {:ok, result}
11496
end)
11597
end
11698
end

lib/access/resources/path.ex

Lines changed: 9 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,11 @@ defmodule DiffoExample.Access.Path do
1010
"""
1111

1212
alias Diffo.Provider.BaseInstance
13-
alias Diffo.Provider.Instance.Specification
1413
alias Diffo.Provider.Instance.Relationship
15-
alias Diffo.Provider.Instance.Feature
1614
alias Diffo.Provider.Instance.Characteristic
17-
alias Diffo.Provider.Instance.Place
18-
alias Diffo.Provider.Instance.Party
1915

2016
alias DiffoExample.Access
17+
alias DiffoExample.Access.ActionHelper
2118

2219
use Ash.Resource,
2320
fragments: [BaseInstance],
@@ -53,25 +50,10 @@ defmodule DiffoExample.Access.Path do
5350

5451
change set_attribute(:type, :resource)
5552

56-
change before_action(fn changeset, _context ->
57-
changeset
58-
|> Specification.set_specified_by_argument()
59-
|> Feature.set_features_argument()
60-
|> Characteristic.set_characteristics_argument()
61-
end)
53+
change before_action(fn changeset, _context -> ActionHelper.build_before(changeset) end)
6254

6355
change after_action(fn changeset, result, _context ->
64-
with {:ok, with_specification} <- Specification.relate_instance(result, changeset),
65-
{:ok, with_relationships} <-
66-
Relationship.relate_instance(with_specification, changeset),
67-
{:ok, with_features} <-
68-
Feature.relate_instance(with_relationships, changeset),
69-
{:ok, with_characteristics} <-
70-
Characteristic.relate_instance(with_features, changeset),
71-
{:ok, with_places} <- Place.relate_instance(with_characteristics, changeset),
72-
{:ok, _with_parties} <- Party.relate_instance(with_places, changeset),
73-
{:ok, path} <- Access.get_path_by_id(result.id),
74-
do: {:ok, path}
56+
ActionHelper.build_after(changeset, result, :get_path_by_id)
7557
end)
7658

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

8567
change after_action(fn changeset, result, _context ->
86-
with {:ok, _result} <- Characteristic.update_values(result, changeset),
87-
{:ok, path} <- Access.get_path_by_id(result.id),
88-
do: {:ok, path}
68+
with {:ok, result} <- Characteristic.update_values(result, changeset),
69+
{:ok, result} <- Access.get_path_by_id(result.id),
70+
do: {:ok, result}
8971
end)
9072
end
9173

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

9678
change after_action(fn changeset, result, _context ->
97-
with {:ok, _path} <- Relationship.relate_instance(result, changeset),
98-
{:ok, path} <- Access.get_path_by_id(result.id),
99-
do: {:ok, path}
79+
with {:ok, result} <- Relationship.relate_instance(result, changeset),
80+
{:ok, result} <- Access.get_path_by_id(result.id),
81+
do: {:ok, result}
10082
end)
10183
end
10284
end

lib/access/resources/shelf.ex

Lines changed: 12 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,13 @@ defmodule DiffoExample.Access.Shelf do
1010
"""
1111

1212
alias Diffo.Provider.BaseInstance
13-
alias Diffo.Provider.Instance.Specification
1413
alias Diffo.Provider.Instance.Relationship
15-
alias Diffo.Provider.Instance.Feature
1614
alias Diffo.Provider.Instance.Characteristic
17-
alias Diffo.Provider.Instance.Place
18-
alias Diffo.Provider.Instance.Party
1915
alias Diffo.Provider.Assigner
2016
alias Diffo.Provider.Assignment
2117

2218
alias DiffoExample.Access
19+
alias DiffoExample.Access.ActionHelper
2320

2421
use Ash.Resource,
2522
fragments: [BaseInstance],
@@ -56,25 +53,10 @@ defmodule DiffoExample.Access.Shelf do
5653

5754
change set_attribute(:type, :resource)
5855

59-
change before_action(fn changeset, _context ->
60-
changeset
61-
|> Specification.set_specified_by_argument()
62-
|> Feature.set_features_argument()
63-
|> Characteristic.set_characteristics_argument()
64-
end)
56+
change before_action(fn changeset, _context -> ActionHelper.build_before(changeset) end)
6557

6658
change after_action(fn changeset, result, _context ->
67-
with {:ok, with_specification} <- Specification.relate_instance(result, changeset),
68-
{:ok, with_relationships} <-
69-
Relationship.relate_instance(with_specification, changeset),
70-
{:ok, with_features} <-
71-
Feature.relate_instance(with_relationships, changeset),
72-
{:ok, with_characteristics} <-
73-
Characteristic.relate_instance(with_features, changeset),
74-
{:ok, with_places} <- Place.relate_instance(with_characteristics, changeset),
75-
{:ok, _with_parties} <- Party.relate_instance(with_places, changeset),
76-
{:ok, shelf} <- Access.get_shelf_by_id(result.id),
77-
do: {:ok, shelf}
59+
ActionHelper.build_after(changeset, result, :get_shelf_by_id)
7860
end)
7961

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

8870
change after_action(fn changeset, result, _context ->
89-
with {:ok, _result} <- Characteristic.update_values(result, changeset),
90-
{:ok, card} <- Access.get_shelf_by_id(result.id),
91-
do: {:ok, card}
71+
with {:ok, result} <- Characteristic.update_values(result, changeset),
72+
{:ok, result} <- Access.get_shelf_by_id(result.id),
73+
do: {:ok, result}
9274
end)
9375
end
9476

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

9981
change after_action(fn changeset, result, _context ->
100-
with {:ok, _shelf} <- Relationship.relate_instance(result, changeset),
101-
{:ok, shelf} <- Access.get_shelf_by_id(result.id),
102-
do: {:ok, shelf}
82+
with {:ok, result} <- Relationship.relate_instance(result, changeset),
83+
{:ok, result} <- Access.get_shelf_by_id(result.id),
84+
do: {:ok, result}
10385
end)
10486
end
10587

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

11092
change after_action(fn changeset, result, _context ->
111-
with {:ok, _card} <- Assigner.assign(result, changeset, :slots, :slot),
112-
{:ok, card} <- Access.get_shelf_by_id(result.id),
113-
do: {:ok, card}
93+
with {:ok, result} <- Assigner.assign(result, changeset, :slots, :slot),
94+
{:ok, result} <- Access.get_shelf_by_id(result.id),
95+
do: {:ok, result}
11496
end)
11597
end
11698
end

0 commit comments

Comments
 (0)