Skip to content

Commit

Permalink
Merge pull request #10 from jcdcdev/dev
Browse files Browse the repository at this point in the history
0.3.1
  • Loading branch information
jcdcdev authored Apr 5, 2024
2 parents ca3dea5 + aa39660 commit 9e4c705
Show file tree
Hide file tree
Showing 10 changed files with 107 additions and 65 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ namespace jcdcdev.Umbraco.ReadingTime.Core;
public interface IReadingTimeService
{
Task ScanTree(int homeId);
Task ScanAll();
Task Process(IContent item);
Task<int> DeleteAsync(Guid key);
Task<ReadingTimeDto?> GetAsync(Guid key, Guid dataTypeKey);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using Umbraco.Cms.Infrastructure.Migrations;

namespace jcdcdev.Umbraco.ReadingTime.Infrastructure.Migrations;

public class InitialMigration : NoopMigration
{
public InitialMigration(IMigrationContext context) : base(context)
{
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using Umbraco.Cms.Infrastructure.Migrations;

namespace jcdcdev.Umbraco.ReadingTime.Infrastructure.Migrations;

public class MultiplePropertyEditorSupport : NoopMigration
{
public MultiplePropertyEditorSupport(IMigrationContext context) : base(context)
{
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
using jcdcdev.Umbraco.ReadingTime.Core;
using jcdcdev.Umbraco.ReadingTime.Infrastructure.Persistence;
using Microsoft.Extensions.Logging;
using Umbraco.Cms.Infrastructure.Migrations;

namespace jcdcdev.Umbraco.ReadingTime.Infrastructure.Migrations;

public class RebuildDatabase : MigrationBase
{
private readonly IReadingTimeService _readingTimeService;

public RebuildDatabase(IMigrationContext context, IReadingTimeService readingTimeService) : base(context)
{
_readingTimeService = readingTimeService;
}

protected override void Migrate()
{
Logger.LogInformation("Rebuilding ReadingTime database");
if (TableExists(Constants.TableName))
{
Delete.ForeignKey("FK_jcdcdevReadingTime_umbracoNode_uniqueId").OnTable(Constants.TableName).Do();
Delete.Table(Constants.TableName).Do();
}

Create.Table<ReadingTimePoco>().Do();

_readingTimeService.ScanAll();
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using jcdcdev.Umbraco.ReadingTime.Core;
using Umbraco.Cms.Core.Packaging;
using Umbraco.Cms.Infrastructure.Migrations;

namespace jcdcdev.Umbraco.ReadingTime.Infrastructure.Migrations;

Expand All @@ -12,7 +13,13 @@ public MigrationPlan() : base(Constants.Package.Name)
protected override void DefinePlan()
{
From(string.Empty);
To<InitialMigration>(nameof(InitialMigration));
To<MultiplePropertyEditorSupport>(nameof(MultiplePropertyEditorSupport));
To<InitialMigration>();
To<MultiplePropertyEditorSupport>();
To<RebuildDatabase>();
}

private void To<T>() where T : MigrationBase
{
To<T>(typeof(T).Name);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@ public class ReadingTimePoco
public int Id { get; set; }

[Column(Name = "key")]
[ForeignKey(typeof(NodeDto), Column = "uniqueId")]
[ForeignKey(typeof(NodeDto), Column = "uniqueId", Name = "FK_jcdcdevReadingTime_content_umbracoNode_uniqueId")]
[NullSetting(NullSetting = NullSettings.NotNull)]
public Guid Key { get; set; }

[Column(Name = "dataTypeKey")]
[ForeignKey(typeof(NodeDto), Column = "uniqueId")]
[ForeignKey(typeof(NodeDto), Column = "uniqueId", Name = "FK_jcdcdevReadingTime_dataTypeKey_umbracoNode_uniqueId")]
[NullSetting(NullSetting = NullSettings.NotNull)]
public Guid DataTypeKey { get; set; }

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

[Column(Name = "dataTypeId")]
[ForeignKey(typeof(NodeDto), Column = "id")]
[ForeignKey(typeof(NodeDto), Column = "id", Name = "FK_jcdcdevReadingTime_dataTypeId_umbracoNode_uniqueId")]
[NullSetting(NullSetting = NullSettings.NotNull)]
public int DataTypeId { get; set; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using jcdcdev.Umbraco.ReadingTime.Core.Models;
using jcdcdev.Umbraco.ReadingTime.Core.PropertyEditors;
using jcdcdev.Umbraco.ReadingTime.Infrastructure.Persistence;
using Microsoft.Extensions.Logging;
using Umbraco.Cms.Core.Models;
using Umbraco.Cms.Core.Services;
using Umbraco.Extensions;
Expand All @@ -15,29 +16,38 @@ public class ReadingTimeService : IReadingTimeService
private readonly ReadingTimeValueProviderCollection _convertors;
private readonly IDataTypeService _dataTypeService;
private readonly IReadingTimeRepository _readingTimeRepository;
private readonly ILogger _logger;

public ReadingTimeService(
IContentService contentService,
ReadingTimeValueProviderCollection convertors,
IReadingTimeRepository readingTimeRepository,
IDataTypeService dataTypeService)
IDataTypeService dataTypeService,
ILogger<ReadingTimeService> logger)
{
_contentService = contentService;
_convertors = convertors;
_readingTimeRepository = readingTimeRepository;
_dataTypeService = dataTypeService;
_logger = logger;
}

public async Task<ReadingTimeDto?> GetAsync(Guid key, Guid dataTypeKey) => await _readingTimeRepository.Get(key, dataTypeKey);

public async Task<ReadingTimeDto?> GetAsync(Guid key, int dataTypeId) => await _readingTimeRepository.Get(key, dataTypeId);

public async Task<int> DeleteAsync(Guid key) => await _readingTimeRepository.DeleteAsync(key);
public async Task<int> DeleteAsync(Guid key)
{
_logger.LogDebug("Deleting reading time for {Key}", key);
return await _readingTimeRepository.DeleteAsync(key);
}

public async Task ScanTree(int homeId)
{
var content = _contentService.GetById(homeId);
if (content == null)
{
_logger.LogWarning("Content with id {HomeId} not found", homeId);
return;
}

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

await Process(current);
if (current.Published)
{
await Process(current);
}
}
}

public async Task ScanAll()
{
var root = _contentService.GetRootContent().ToList();
_logger.LogInformation("Scanning {Count} root content items", root.Count);
foreach (var content in root)
{
await ScanTree(content.Id);
}
}

Expand All @@ -75,6 +98,7 @@ public async Task Process(IContent item)
return;
}

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

var config = dataType.ConfigurationAs<ReadingTimeConfiguration>();
if (config == null)
{
_logger.LogWarning("Configuration not found for property {PropertyId}", readingTimeProperty.Id);
return;
}

Expand All @@ -100,13 +126,16 @@ private async Task ProcessPropertyEditor(IContent item, IProperty readingTimePro
var propertyType = readingTimeProperty.PropertyType;
if (propertyType.VariesByCulture())
{
_logger.LogDebug("Processing culture variants for {Id}:{Item}", item.Id, item.Name);
foreach (var culture in item.AvailableCultures)
{
_logger.LogDebug("Processing culture {Culture}", culture);
var model = GetModel(item, culture, null, config);
models.Add(model);
}
}

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

Expand Down Expand Up @@ -134,14 +163,24 @@ private ReadingTimeVariantDto GetModel(IContent item, string? culture, string? s
foreach (var property in item.Properties)
{
var convertor = _convertors.FirstOrDefault(x => x.CanConvert(property.PropertyType));
if (convertor == null)
{
_logger.LogDebug("No convertor found for {PropertyId}:{PropertyEditorAlias}", property.Id, property.PropertyType.PropertyEditorAlias);
continue;
}

_logger.LogDebug("Processing property {PropertyId}:{PropertyEditorAlias}", property.Id, property.PropertyType.PropertyEditorAlias);

var cCulture = property.PropertyType.VariesByCulture() ? culture : null;
var cSegment = property.PropertyType.VariesBySegment() ? segment : null;
var readingTime = convertor?.GetReadingTime(property, cCulture, cSegment, item.AvailableCultures, config);
if (!readingTime.HasValue)
{
_logger.LogDebug("No reading time found for {PropertyId}:{PropertyEditorAlias}", property.Id, property.PropertyType.PropertyEditorAlias);
continue;
}

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

Expand Down
3 changes: 2 additions & 1 deletion src/jcdcdev.Umbraco.ReadingTime/ManifestFilter.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using jcdcdev.Umbraco.ReadingTime.Core;
using Umbraco.Cms.Core.Manifest;

namespace jcdcdev.Umbraco.ReadingTime;
Expand All @@ -8,7 +9,7 @@ public void Filter(List<PackageManifest> manifests)
{
manifests.Add(new PackageManifest
{
PackageName = "jcdcdev.Umbraco.ReadingTime",
PackageName = Constants.Package.Name,
Version = GetType().Assembly.GetName().Version?.ToString(3) ?? "0.1.0",
AllowPackageTelemetry = true
});
Expand Down

0 comments on commit 9e4c705

Please sign in to comment.