Skip to content

Commit 9e4c705

Browse files
authored
Merge pull request #10 from jcdcdev/dev
0.3.1
2 parents ca3dea5 + aa39660 commit 9e4c705

File tree

10 files changed

+107
-65
lines changed

10 files changed

+107
-65
lines changed

src/jcdcdev.Umbraco.ReadingTime/Core/IReadingTimeService.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ namespace jcdcdev.Umbraco.ReadingTime.Core;
66
public interface IReadingTimeService
77
{
88
Task ScanTree(int homeId);
9+
Task ScanAll();
910
Task Process(IContent item);
1011
Task<int> DeleteAsync(Guid key);
1112
Task<ReadingTimeDto?> GetAsync(Guid key, Guid dataTypeKey);
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
using Umbraco.Cms.Infrastructure.Migrations;
2+
3+
namespace jcdcdev.Umbraco.ReadingTime.Infrastructure.Migrations;
4+
5+
public class InitialMigration : NoopMigration
6+
{
7+
public InitialMigration(IMigrationContext context) : base(context)
8+
{
9+
}
10+
}

src/jcdcdev.Umbraco.ReadingTime/Infrastructure/Migrations/0.2.0/MultiplePropertyEditorSupport.cs

Lines changed: 0 additions & 34 deletions
This file was deleted.
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
using Umbraco.Cms.Infrastructure.Migrations;
2+
3+
namespace jcdcdev.Umbraco.ReadingTime.Infrastructure.Migrations;
4+
5+
public class MultiplePropertyEditorSupport : NoopMigration
6+
{
7+
public MultiplePropertyEditorSupport(IMigrationContext context) : base(context)
8+
{
9+
}
10+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
using jcdcdev.Umbraco.ReadingTime.Core;
2+
using jcdcdev.Umbraco.ReadingTime.Infrastructure.Persistence;
3+
using Microsoft.Extensions.Logging;
4+
using Umbraco.Cms.Infrastructure.Migrations;
5+
6+
namespace jcdcdev.Umbraco.ReadingTime.Infrastructure.Migrations;
7+
8+
public class RebuildDatabase : MigrationBase
9+
{
10+
private readonly IReadingTimeService _readingTimeService;
11+
12+
public RebuildDatabase(IMigrationContext context, IReadingTimeService readingTimeService) : base(context)
13+
{
14+
_readingTimeService = readingTimeService;
15+
}
16+
17+
protected override void Migrate()
18+
{
19+
Logger.LogInformation("Rebuilding ReadingTime database");
20+
if (TableExists(Constants.TableName))
21+
{
22+
Delete.ForeignKey("FK_jcdcdevReadingTime_umbracoNode_uniqueId").OnTable(Constants.TableName).Do();
23+
Delete.Table(Constants.TableName).Do();
24+
}
25+
26+
Create.Table<ReadingTimePoco>().Do();
27+
28+
_readingTimeService.ScanAll();
29+
}
30+
}

src/jcdcdev.Umbraco.ReadingTime/Infrastructure/Migrations/InitialMigration.cs

Lines changed: 0 additions & 22 deletions
This file was deleted.

src/jcdcdev.Umbraco.ReadingTime/Infrastructure/Migrations/MigrationPlan.cs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using jcdcdev.Umbraco.ReadingTime.Core;
22
using Umbraco.Cms.Core.Packaging;
3+
using Umbraco.Cms.Infrastructure.Migrations;
34

45
namespace jcdcdev.Umbraco.ReadingTime.Infrastructure.Migrations;
56

@@ -12,7 +13,13 @@ public MigrationPlan() : base(Constants.Package.Name)
1213
protected override void DefinePlan()
1314
{
1415
From(string.Empty);
15-
To<InitialMigration>(nameof(InitialMigration));
16-
To<MultiplePropertyEditorSupport>(nameof(MultiplePropertyEditorSupport));
16+
To<InitialMigration>();
17+
To<MultiplePropertyEditorSupport>();
18+
To<RebuildDatabase>();
19+
}
20+
21+
private void To<T>() where T : MigrationBase
22+
{
23+
To<T>(typeof(T).Name);
1724
}
1825
}

src/jcdcdev.Umbraco.ReadingTime/Infrastructure/Persistence/ReadingTimePoco.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,12 @@ public class ReadingTimePoco
1515
public int Id { get; set; }
1616

1717
[Column(Name = "key")]
18-
[ForeignKey(typeof(NodeDto), Column = "uniqueId")]
18+
[ForeignKey(typeof(NodeDto), Column = "uniqueId", Name = "FK_jcdcdevReadingTime_content_umbracoNode_uniqueId")]
1919
[NullSetting(NullSetting = NullSettings.NotNull)]
2020
public Guid Key { get; set; }
2121

2222
[Column(Name = "dataTypeKey")]
23-
[ForeignKey(typeof(NodeDto), Column = "uniqueId")]
23+
[ForeignKey(typeof(NodeDto), Column = "uniqueId", Name = "FK_jcdcdevReadingTime_dataTypeKey_umbracoNode_uniqueId")]
2424
[NullSetting(NullSetting = NullSettings.NotNull)]
2525
public Guid DataTypeKey { get; set; }
2626

@@ -30,7 +30,7 @@ public class ReadingTimePoco
3030
public string? TextData { get; set; }
3131

3232
[Column(Name = "dataTypeId")]
33-
[ForeignKey(typeof(NodeDto), Column = "id")]
33+
[ForeignKey(typeof(NodeDto), Column = "id", Name = "FK_jcdcdevReadingTime_dataTypeId_umbracoNode_uniqueId")]
3434
[NullSetting(NullSetting = NullSettings.NotNull)]
3535
public int DataTypeId { get; set; }
3636
}

src/jcdcdev.Umbraco.ReadingTime/Infrastructure/ReadingTimeService.cs

Lines changed: 42 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using jcdcdev.Umbraco.ReadingTime.Core.Models;
44
using jcdcdev.Umbraco.ReadingTime.Core.PropertyEditors;
55
using jcdcdev.Umbraco.ReadingTime.Infrastructure.Persistence;
6+
using Microsoft.Extensions.Logging;
67
using Umbraco.Cms.Core.Models;
78
using Umbraco.Cms.Core.Services;
89
using Umbraco.Extensions;
@@ -15,29 +16,38 @@ public class ReadingTimeService : IReadingTimeService
1516
private readonly ReadingTimeValueProviderCollection _convertors;
1617
private readonly IDataTypeService _dataTypeService;
1718
private readonly IReadingTimeRepository _readingTimeRepository;
19+
private readonly ILogger _logger;
1820

1921
public ReadingTimeService(
2022
IContentService contentService,
2123
ReadingTimeValueProviderCollection convertors,
2224
IReadingTimeRepository readingTimeRepository,
23-
IDataTypeService dataTypeService)
25+
IDataTypeService dataTypeService,
26+
ILogger<ReadingTimeService> logger)
2427
{
2528
_contentService = contentService;
2629
_convertors = convertors;
2730
_readingTimeRepository = readingTimeRepository;
2831
_dataTypeService = dataTypeService;
32+
_logger = logger;
2933
}
3034

3135
public async Task<ReadingTimeDto?> GetAsync(Guid key, Guid dataTypeKey) => await _readingTimeRepository.Get(key, dataTypeKey);
36+
3237
public async Task<ReadingTimeDto?> GetAsync(Guid key, int dataTypeId) => await _readingTimeRepository.Get(key, dataTypeId);
3338

34-
public async Task<int> DeleteAsync(Guid key) => await _readingTimeRepository.DeleteAsync(key);
39+
public async Task<int> DeleteAsync(Guid key)
40+
{
41+
_logger.LogDebug("Deleting reading time for {Key}", key);
42+
return await _readingTimeRepository.DeleteAsync(key);
43+
}
3544

3645
public async Task ScanTree(int homeId)
3746
{
3847
var content = _contentService.GetById(homeId);
3948
if (content == null)
4049
{
50+
_logger.LogWarning("Content with id {HomeId} not found", homeId);
4151
return;
4252
}
4353

@@ -63,7 +73,20 @@ public async Task ScanTree(int homeId)
6373
moreRecords = (page + 1) * 100 <= totalRecords;
6474
}
6575

66-
await Process(current);
76+
if (current.Published)
77+
{
78+
await Process(current);
79+
}
80+
}
81+
}
82+
83+
public async Task ScanAll()
84+
{
85+
var root = _contentService.GetRootContent().ToList();
86+
_logger.LogInformation("Scanning {Count} root content items", root.Count);
87+
foreach (var content in root)
88+
{
89+
await ScanTree(content.Id);
6790
}
6891
}
6992

@@ -75,6 +98,7 @@ public async Task Process(IContent item)
7598
return;
7699
}
77100

101+
_logger.LogDebug("Processing {Id}:{Item}", item.Id, item.Name);
78102
foreach (var property in props)
79103
{
80104
await ProcessPropertyEditor(item, property);
@@ -86,12 +110,14 @@ private async Task ProcessPropertyEditor(IContent item, IProperty readingTimePro
86110
var dataType = _dataTypeService.GetDataType(readingTimeProperty.PropertyType.DataTypeId);
87111
if (dataType == null)
88112
{
113+
_logger.LogWarning("DataType not found for property {PropertyId}", readingTimeProperty.Id);
89114
return;
90115
}
91116

92117
var config = dataType.ConfigurationAs<ReadingTimeConfiguration>();
93118
if (config == null)
94119
{
120+
_logger.LogWarning("Configuration not found for property {PropertyId}", readingTimeProperty.Id);
95121
return;
96122
}
97123

@@ -100,13 +126,16 @@ private async Task ProcessPropertyEditor(IContent item, IProperty readingTimePro
100126
var propertyType = readingTimeProperty.PropertyType;
101127
if (propertyType.VariesByCulture())
102128
{
129+
_logger.LogDebug("Processing culture variants for {Id}:{Item}", item.Id, item.Name);
103130
foreach (var culture in item.AvailableCultures)
104131
{
132+
_logger.LogDebug("Processing culture {Culture}", culture);
105133
var model = GetModel(item, culture, null, config);
106134
models.Add(model);
107135
}
108136
}
109137

138+
_logger.LogDebug("Processing invariant variant for {Id}:{Item}", item.Id, item.Name);
110139
var invariant = GetModel(item, null, null, config);
111140
models.Add(invariant);
112141

@@ -134,14 +163,24 @@ private ReadingTimeVariantDto GetModel(IContent item, string? culture, string? s
134163
foreach (var property in item.Properties)
135164
{
136165
var convertor = _convertors.FirstOrDefault(x => x.CanConvert(property.PropertyType));
166+
if (convertor == null)
167+
{
168+
_logger.LogDebug("No convertor found for {PropertyId}:{PropertyEditorAlias}", property.Id, property.PropertyType.PropertyEditorAlias);
169+
continue;
170+
}
171+
172+
_logger.LogDebug("Processing property {PropertyId}:{PropertyEditorAlias}", property.Id, property.PropertyType.PropertyEditorAlias);
173+
137174
var cCulture = property.PropertyType.VariesByCulture() ? culture : null;
138175
var cSegment = property.PropertyType.VariesBySegment() ? segment : null;
139176
var readingTime = convertor?.GetReadingTime(property, cCulture, cSegment, item.AvailableCultures, config);
140177
if (!readingTime.HasValue)
141178
{
179+
_logger.LogDebug("No reading time found for {PropertyId}:{PropertyEditorAlias}", property.Id, property.PropertyType.PropertyEditorAlias);
142180
continue;
143181
}
144182

183+
_logger.LogDebug("Reading time found for {PropertyId}:{PropertyEditorAlias} ({Time})", property.Id, property.PropertyType.PropertyEditorAlias, readingTime.Value);
145184
time += readingTime.Value;
146185
}
147186

src/jcdcdev.Umbraco.ReadingTime/ManifestFilter.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using jcdcdev.Umbraco.ReadingTime.Core;
12
using Umbraco.Cms.Core.Manifest;
23

34
namespace jcdcdev.Umbraco.ReadingTime;
@@ -8,7 +9,7 @@ public void Filter(List<PackageManifest> manifests)
89
{
910
manifests.Add(new PackageManifest
1011
{
11-
PackageName = "jcdcdev.Umbraco.ReadingTime",
12+
PackageName = Constants.Package.Name,
1213
Version = GetType().Assembly.GetName().Version?.ToString(3) ?? "0.1.0",
1314
AllowPackageTelemetry = true
1415
});

0 commit comments

Comments
 (0)