Skip to content

Commit 1d5ad08

Browse files
author
Claus
committed
moving inner functions to private function.
few other fix-ups.
1 parent c7922c8 commit 1d5ad08

File tree

1 file changed

+38
-50
lines changed

1 file changed

+38
-50
lines changed

src/Umbraco.Deploy.Contrib.Connectors/ValueConnectors/MeganavValueConnector.cs

Lines changed: 38 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,12 @@ public class MeganavValueConnector : IValueConnector
1818
/// <summary>
1919
/// The entity service.
2020
/// </summary>
21-
private readonly IEntityService entityService;
21+
private readonly IEntityService _entityService;
2222

2323
/// <summary>
2424
/// Gets the property editor aliases that the value converter supports by default.
2525
/// </summary>
26-
public IEnumerable<string> PropertyEditorAliases => new string[] { "Cogworks.Meganav" };
26+
public IEnumerable<string> PropertyEditorAliases => new[] { "Cogworks.Meganav" };
2727

2828
/// <summary>
2929
/// Initializes a new instance of the <see cref="MeganavValueConnector"/> class.
@@ -32,7 +32,7 @@ public class MeganavValueConnector : IValueConnector
3232
/// <exception cref="ArgumentNullException">entityService</exception>
3333
public MeganavValueConnector(IEntityService entityService)
3434
{
35-
this.entityService = entityService ?? throw new ArgumentNullException("entityService");
35+
_entityService = entityService ?? throw new ArgumentNullException("entityService");
3636
}
3737

3838
/// <summary>
@@ -52,36 +52,7 @@ public string GetValue(Property property, ICollection<ArtifactDependency> depend
5252
}
5353

5454
// Parse links and convert ID to UDI
55-
JArray ParseLinks(JArray links)
56-
{
57-
foreach (var link in links)
58-
{
59-
GuidUdi guidUdi = null;
60-
int id = link.Value<int>("id");
61-
if (id != 0)
62-
{
63-
Attempt<Guid> keyForId = this.entityService.GetKeyForId(id, UmbracoObjectTypes.Document);
64-
if (keyForId.Success)
65-
{
66-
guidUdi = new GuidUdi("document", keyForId.Result);
67-
dependencies.Add(new ArtifactDependency(guidUdi, false, ArtifactDependencyMode.Exist));
68-
}
69-
}
70-
71-
link["id"] = guidUdi?.ToString();
72-
73-
// Parse children
74-
var children = link.Value<JArray>("children");
75-
if (children != null)
76-
{
77-
link["children"] = ParseLinks(children);
78-
}
79-
}
80-
81-
return links;
82-
}
83-
84-
var rootLinks = ParseLinks(JArray.Parse(value));
55+
var rootLinks = ParseLinks(JArray.Parse(value), dependencies, Direction.ToArtifact);
8556

8657
return rootLinks.ToString(Formatting.None);
8758
}
@@ -103,41 +74,58 @@ public void SetValue(IContentBase content, string alias, string value)
10374

10475
if (!StringExtensions.DetectIsJson(value))
10576
{
106-
// Skip invalid value (probably shoudn't be saved, right?)
77+
// Skip invalid values
10778
return;
10879
}
10980

11081
// Parse links and convert UDI back to local ID
111-
JArray ParseLinks(JArray links)
82+
var rootLinks = ParseLinks(JArray.Parse(value), null, Direction.FromArtifact);
83+
84+
content.SetValue(alias, rootLinks.ToString(Formatting.None));
85+
}
86+
87+
private JArray ParseLinks(JArray links, ICollection<ArtifactDependency> dependencies, Direction direction)
88+
{
89+
foreach (var link in links)
11290
{
113-
foreach (var link in links)
91+
if (direction == Direction.ToArtifact)
11492
{
115-
int id = 0;
93+
GuidUdi guidUdi = null;
94+
var id = link.Value<int>("id");
95+
if (id != 0)
96+
{
97+
var keyForId = _entityService.GetKeyForId(id, UmbracoObjectTypes.Document);
98+
if (keyForId.Success)
99+
{
100+
guidUdi = new GuidUdi(Constants.UdiEntityType.Document, keyForId.Result);
101+
dependencies.Add(new ArtifactDependency(guidUdi, false, ArtifactDependencyMode.Exist));
102+
}
103+
}
104+
link["id"] = guidUdi?.ToString();
105+
}
106+
else
107+
{
108+
var id = 0;
116109
if (GuidUdi.TryParse(link.Value<string>("id"), out GuidUdi guidUdi))
117110
{
118-
Attempt<int> idForUdi = this.entityService.GetIdForUdi(guidUdi);
111+
var idForUdi = _entityService.GetIdForUdi(guidUdi);
119112
if (idForUdi.Success)
120113
{
121114
id = idForUdi.Result;
122115
}
123116
}
124-
125117
link["id"] = id;
126-
127-
// Parse children
128-
var children = link.Value<JArray>("children");
129-
if (children != null)
130-
{
131-
link["children"] = ParseLinks(children);
132-
}
133118
}
134119

135-
return links;
120+
// Parse children
121+
var children = link.Value<JArray>("children");
122+
if (children != null)
123+
{
124+
link["children"] = ParseLinks(children, dependencies, direction);
125+
}
136126
}
137127

138-
var rootLinks = ParseLinks(JArray.Parse(value));
139-
140-
content.SetValue(alias, rootLinks.ToString(Formatting.None));
128+
return links;
141129
}
142130
}
143131
}

0 commit comments

Comments
 (0)