1
1
using System . Text . Json ;
2
+ using System . Text . Json . Serialization ;
2
3
using Umbraco . Cms . Core . Services ;
3
4
using Umbraco . Cms . Infrastructure . Migrations ;
4
5
using Umbraco . Cms . Infrastructure . Scoping ;
@@ -11,7 +12,7 @@ public class UpdateAlgoliaIndicesPostUpgrade : MigrationBase
11
12
private readonly IScopeProvider _scopeProvider ;
12
13
private readonly IContentTypeService _contentTypeService ;
13
14
14
- public UpdateAlgoliaIndicesPostUpgrade ( IMigrationContext context , IScopeProvider scopeProvider , IContentTypeService contentTypeService )
15
+ public UpdateAlgoliaIndicesPostUpgrade ( IMigrationContext context , IScopeProvider scopeProvider , IContentTypeService contentTypeService )
15
16
: base ( context )
16
17
{
17
18
_scopeProvider = scopeProvider ;
@@ -24,36 +25,71 @@ protected override void Migrate()
24
25
25
26
var indices = scope . Database . Fetch < AlgoliaIndex > ( ) ;
26
27
27
- foreach ( var index in indices )
28
+ foreach ( var index in indices )
28
29
{
29
- bool requiresUpdate = false ;
30
-
31
- var indexSerializedData = JsonSerializer . Deserialize < IEnumerable < ContentTypeDto > > ( index . SerializedData ) ;
30
+ var indexSerializedData = JsonSerializer . Deserialize < IEnumerable < AlgoliaMigrationIndexData > > ( index . SerializedData ) ;
32
31
if ( indexSerializedData == null ) continue ;
33
32
34
33
foreach ( var data in indexSerializedData . Where ( x => x . ContentType != null ) )
35
34
{
36
- requiresUpdate = true ;
35
+ var contentTypeDto = new ContentTypeDto ( ) ;
37
36
38
- var contentType = _contentTypeService . Get ( data . ContentType ! . Alias ) ;
37
+ var contentType = _contentTypeService . Get ( data . ContentType . Alias ) ;
39
38
if ( contentType != null )
40
39
{
41
- data . Id = contentType . Id ;
40
+ contentTypeDto . Id = contentType . Id ;
42
41
}
43
42
44
- data . Icon = data . ContentType ! . Icon ;
45
- data . Alias = data . ContentType ! . Alias ;
46
- data . Name = data . ContentType ! . Name ;
47
- }
43
+ contentTypeDto . Icon = data . ContentType . Icon ;
44
+ contentTypeDto . Alias = data . ContentType . Alias ;
45
+ contentTypeDto . Name = data . ContentType . Name ;
46
+ contentTypeDto . Properties = data . Properties . Select ( x => new ContentTypePropertyDto
47
+ {
48
+ Alias = x . Alias ,
49
+ Name = x . Name ,
50
+ Icon = x . Icon ,
51
+ Selected = true
52
+ } ) ;
48
53
49
- if ( requiresUpdate )
50
- {
51
- index . SerializedData = JsonSerializer . Serialize ( indexSerializedData ) ;
54
+ index . SerializedData = JsonSerializer . Serialize ( contentTypeDto ) ;
52
55
scope . Database . Update ( index ) ;
53
56
}
54
57
}
55
58
56
59
scope . Complete ( ) ;
57
60
}
61
+
62
+ private class AlgoliaMigrationIndexData
63
+ {
64
+ [ JsonPropertyName ( "contentType" ) ]
65
+ public AlgoliaMigrationIndexContentType ContentType { get ; set ; }
66
+
67
+ [ JsonPropertyName ( "properties" ) ]
68
+ public IEnumerable < AlgoliaMigrationIndexProperty > Properties { get ; set ; }
69
+ }
70
+
71
+ public class AlgoliaMigrationIndexContentType
72
+ {
73
+ [ JsonPropertyName ( "alias" ) ]
74
+ public string Alias { get ; set ; }
75
+
76
+ [ JsonPropertyName ( "name" ) ]
77
+ public string Name { get ; set ; }
78
+
79
+ [ JsonPropertyName ( "icon" ) ]
80
+ public string Icon { get ; set ; }
81
+ }
82
+
83
+ public class AlgoliaMigrationIndexProperty
84
+ {
85
+ [ JsonPropertyName ( "alias" ) ]
86
+ public string Alias { get ; set ; }
87
+
88
+ [ JsonPropertyName ( "name" ) ]
89
+ public string Name { get ; set ; }
90
+
91
+ [ JsonPropertyName ( "icon" ) ]
92
+ public string Icon { get ; set ; }
93
+ }
58
94
}
59
95
}
0 commit comments