77using Umbraco . Cms . Core . Deploy ;
88using Umbraco . Cms . Core . Models ;
99using Umbraco . Cms . Core . Serialization ;
10+ using Umbraco . Commerce . Common . Logging ;
1011using Umbraco . Commerce . Core . Models ;
1112using Umbraco . Deploy . Core . Connectors . ValueConnectors ;
1213
1314namespace Umbraco . Commerce . Deploy . Connectors . ValueConnectors
1415{
15- public class UmbracoCommercePriceValueConnector (
16- IUmbracoCommerceApi umbracoCommerceApi ,
17- IJsonSerializer jsonSerializer )
18- : ValueConnectorBase
16+ public class UmbracoCommercePriceValueConnector : ValueConnectorBase
1917 {
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+
2039 public override IEnumerable < string > PropertyEditorAliases => new [ ] { "Umbraco.Commerce.Price" } ;
2140
2241 public override async Task < string ? > ToArtifactAsync (
@@ -33,27 +52,35 @@ public class UmbracoCommercePriceValueConnector(
3352 return null ;
3453 }
3554
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 ) ;
3758
38- var dstDict = new Dictionary < string , decimal ? > ( ) ;
59+ var dstDict = new Dictionary < string , decimal ? > ( ) ;
3960
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 ) ;
4364
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?
5071
51- dependencies . Add ( new UmbracoCommerceArtifactDependency ( udi , ArtifactDependencyMode . Exist ) ) ;
72+ dependencies . Add ( new UmbracoCommerceArtifactDependency ( udi , ArtifactDependencyMode . Exist ) ) ;
5273
53- dstDict . Add ( udi . ToString ( ) , kvp . Value ) ;
54- }
74+ dstDict . Add ( udi . ToString ( ) , kvp . Value ) ;
75+ }
5576
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+ }
5784 }
5885
5986
@@ -72,27 +99,36 @@ public class UmbracoCommercePriceValueConnector(
7299 return null ;
73100 }
74101
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 ) ;
76106
77- var dstDict = new Dictionary < Guid , decimal ? > ( ) ;
107+ var dstDict = new Dictionary < Guid , decimal ? > ( ) ;
78108
79- if ( srcDict != null )
80- {
81- foreach ( KeyValuePair < string , decimal ? > kvp in srcDict )
109+ if ( srcDict != null )
82110 {
83- if ( UdiHelper . TryParseGuidUdi ( kvp . Key , out GuidUdi ? udi ) &&
84- udi ! . EntityType == UmbracoCommerceConstants . UdiEntityType . Currency )
111+ foreach ( KeyValuePair < string , decimal ? > kvp in srcDict )
85112 {
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 )
88115 {
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+ }
90121 }
91122 }
92123 }
93- }
94124
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+ }
96132 }
97133 }
98134}
0 commit comments