7
7
using Umbraco . Cms . Core . Deploy ;
8
8
using Umbraco . Cms . Core . Models ;
9
9
using Umbraco . Cms . Core . Serialization ;
10
+ using Umbraco . Commerce . Common . Logging ;
10
11
using Umbraco . Commerce . Core . Models ;
11
12
using Umbraco . Deploy . Core . Connectors . ValueConnectors ;
12
13
13
14
namespace Umbraco . Commerce . Deploy . Connectors . ValueConnectors
14
15
{
15
- public class UmbracoCommercePriceValueConnector (
16
- IUmbracoCommerceApi umbracoCommerceApi ,
17
- IJsonSerializer jsonSerializer )
18
- : ValueConnectorBase
16
+ public class UmbracoCommercePriceValueConnector : ValueConnectorBase
19
17
{
18
+ private IUmbracoCommerceApi _umbracoCommerceApi ;
19
+ private IJsonSerializer _jsonSerializer ;
20
+ private ILogger < UmbracoCommercePriceValueConnector > ? _logger ;
21
+
22
+ public UmbracoCommercePriceValueConnector ( IUmbracoCommerceApi umbracoCommerceApi ,
23
+ IJsonSerializer jsonSerializer ,
24
+ ILogger < UmbracoCommercePriceValueConnector > logger )
25
+ {
26
+ _umbracoCommerceApi = umbracoCommerceApi ?? throw new ArgumentNullException ( nameof ( umbracoCommerceApi ) ) ;
27
+ _jsonSerializer = jsonSerializer ?? throw new ArgumentNullException ( nameof ( jsonSerializer ) ) ;
28
+ _logger = logger ;
29
+ }
30
+
31
+ public UmbracoCommercePriceValueConnector ( IUmbracoCommerceApi umbracoCommerceApi ,
32
+ IJsonSerializer jsonSerializer )
33
+ {
34
+ _umbracoCommerceApi = umbracoCommerceApi ?? throw new ArgumentNullException ( nameof ( umbracoCommerceApi ) ) ;
35
+ _jsonSerializer = jsonSerializer ?? throw new ArgumentNullException ( nameof ( jsonSerializer ) ) ;
36
+ _logger = null ;
37
+ }
38
+
20
39
public override IEnumerable < string > PropertyEditorAliases => new [ ] { "Umbraco.Commerce.Price" } ;
21
40
22
41
public override async Task < string ? > ToArtifactAsync (
@@ -33,27 +52,35 @@ public class UmbracoCommercePriceValueConnector(
33
52
return null ;
34
53
}
35
54
36
- Dictionary < Guid , decimal ? > ? srcDict = jsonSerializer . Deserialize < Dictionary < Guid , decimal ? > > ( svalue ) ;
55
+ try
56
+ {
57
+ Dictionary < Guid , decimal ? > ? srcDict = _jsonSerializer . Deserialize < Dictionary < Guid , decimal ? > > ( svalue ) ;
37
58
38
- var dstDict = new Dictionary < string , decimal ? > ( ) ;
59
+ var dstDict = new Dictionary < string , decimal ? > ( ) ;
39
60
40
- foreach ( KeyValuePair < Guid , decimal ? > kvp in srcDict )
41
- {
42
- var udi = new GuidUdi ( UmbracoCommerceConstants . UdiEntityType . Currency , kvp . Key ) ;
61
+ foreach ( KeyValuePair < Guid , decimal ? > kvp in srcDict )
62
+ {
63
+ var udi = new GuidUdi ( UmbracoCommerceConstants . UdiEntityType . Currency , kvp . Key ) ;
43
64
44
- // Because we store Guid IDs anyway we don't necessarily need to fetch
45
- // the Currency entity to look anything up, it's mostly a question
46
- // of whether we want to validate the Currency exists. I'm not sure
47
- // whether this should really be the responsibility of the property editor
48
- // though and we should just be able to trust the property editor value
49
- // is valid?
65
+ // Because we store Guid IDs anyway we don't necessarily need to fetch
66
+ // the Currency entity to look anything up, it's mostly a question
67
+ // of whether we want to validate the Currency exists. I'm not sure
68
+ // whether this should really be the responsibility of the property editor
69
+ // though and we should just be able to trust the property editor value
70
+ // is valid?
50
71
51
- dependencies . Add ( new UmbracoCommerceArtifactDependency ( udi , ArtifactDependencyMode . Exist ) ) ;
72
+ dependencies . Add ( new UmbracoCommerceArtifactDependency ( udi , ArtifactDependencyMode . Exist ) ) ;
52
73
53
- dstDict . Add ( udi . ToString ( ) , kvp . Value ) ;
54
- }
74
+ dstDict . Add ( udi . ToString ( ) , kvp . Value ) ;
75
+ }
55
76
56
- return jsonSerializer . Serialize ( dstDict ) ;
77
+ return _jsonSerializer . Serialize ( dstDict ) ;
78
+ }
79
+ catch ( Exception ex )
80
+ {
81
+ _logger ? . Error ( ex , "Failed to serialize price value to artifact: {Value}" , svalue ) ;
82
+ return null ;
83
+ }
57
84
}
58
85
59
86
@@ -72,27 +99,36 @@ public class UmbracoCommercePriceValueConnector(
72
99
return null ;
73
100
}
74
101
75
- Dictionary < string , decimal ? > ? srcDict = jsonSerializer . Deserialize < Dictionary < string , decimal ? > > ( svalue ) ;
102
+ try
103
+ {
104
+ Dictionary < string , decimal ? > ? srcDict =
105
+ _jsonSerializer . Deserialize < Dictionary < string , decimal ? > > ( svalue ) ;
76
106
77
- var dstDict = new Dictionary < Guid , decimal ? > ( ) ;
107
+ var dstDict = new Dictionary < Guid , decimal ? > ( ) ;
78
108
79
- if ( srcDict != null )
80
- {
81
- foreach ( KeyValuePair < string , decimal ? > kvp in srcDict )
109
+ if ( srcDict != null )
82
110
{
83
- if ( UdiHelper . TryParseGuidUdi ( kvp . Key , out GuidUdi ? udi ) &&
84
- udi ! . EntityType == UmbracoCommerceConstants . UdiEntityType . Currency )
111
+ foreach ( KeyValuePair < string , decimal ? > kvp in srcDict )
85
112
{
86
- CurrencyReadOnly ? currencyEntity = await umbracoCommerceApi . GetCurrencyAsync ( udi . Guid ) ;
87
- if ( currencyEntity != null )
113
+ if ( UdiHelper . TryParseGuidUdi ( kvp . Key , out GuidUdi ? udi ) &&
114
+ udi ! . EntityType == UmbracoCommerceConstants . UdiEntityType . Currency )
88
115
{
89
- dstDict . Add ( currencyEntity . Id , kvp . Value ) ;
116
+ CurrencyReadOnly ? currencyEntity = await _umbracoCommerceApi . GetCurrencyAsync ( udi . Guid ) ;
117
+ if ( currencyEntity != null )
118
+ {
119
+ dstDict . Add ( currencyEntity . Id , kvp . Value ) ;
120
+ }
90
121
}
91
122
}
92
123
}
93
- }
94
124
95
- return jsonSerializer . Serialize ( dstDict ) ;
125
+ return _jsonSerializer . Serialize ( dstDict ) ;
126
+ }
127
+ catch ( Exception ex )
128
+ {
129
+ _logger ? . Error ( ex , "Failed to deserialize price value from artifact: {Value}" , svalue ) ;
130
+ return null ;
131
+ }
96
132
}
97
133
}
98
134
}
0 commit comments