|
| 1 | +using System.Text.Json; |
| 2 | +using System.Text.Json.Serialization; |
| 3 | +using Umbraco.Cms.Core.Services; |
| 4 | +using Umbraco.Cms.Infrastructure.Migrations; |
| 5 | +using Umbraco.Cms.Infrastructure.Scoping; |
| 6 | +using Umbraco.Cms.Integrations.Search.Algolia.Models.ContentTypeDtos; |
| 7 | + |
| 8 | +namespace Umbraco.Cms.Integrations.Search.Algolia.Migrations |
| 9 | +{ |
| 10 | + public class UpdateAlgoliaIndicesPostUpgrade : MigrationBase |
| 11 | + { |
| 12 | + private readonly IScopeProvider _scopeProvider; |
| 13 | + private readonly IContentTypeService _contentTypeService; |
| 14 | + |
| 15 | + public UpdateAlgoliaIndicesPostUpgrade(IMigrationContext context, IScopeProvider scopeProvider, IContentTypeService contentTypeService) |
| 16 | + : base(context) |
| 17 | + { |
| 18 | + _scopeProvider = scopeProvider; |
| 19 | + _contentTypeService = contentTypeService; |
| 20 | + } |
| 21 | + |
| 22 | + protected override void Migrate() |
| 23 | + { |
| 24 | + using var scope = _scopeProvider.CreateScope(); |
| 25 | + |
| 26 | + var indices = scope.Database.Fetch<AlgoliaIndex>(); |
| 27 | + |
| 28 | + foreach (var index in indices) |
| 29 | + { |
| 30 | + var indexSerializedData = JsonSerializer.Deserialize<IEnumerable<AlgoliaMigrationIndexData>>(index.SerializedData); |
| 31 | + if (indexSerializedData == null) continue; |
| 32 | + |
| 33 | + foreach (var data in indexSerializedData.Where(x => x.ContentType != null)) |
| 34 | + { |
| 35 | + var contentTypeDto = new ContentTypeDto(); |
| 36 | + |
| 37 | + var contentType = _contentTypeService.Get(data.ContentType.Alias); |
| 38 | + if (contentType != null) |
| 39 | + { |
| 40 | + contentTypeDto.Id = contentType.Id; |
| 41 | + } |
| 42 | + |
| 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 | + }); |
| 53 | + |
| 54 | + index.SerializedData = JsonSerializer.Serialize(contentTypeDto); |
| 55 | + scope.Database.Update(index); |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + scope.Complete(); |
| 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 | + private 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 | + private 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 | + } |
| 94 | + } |
| 95 | +} |
0 commit comments