-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
3.5.0: UpdateResoureKey command added
- Loading branch information
Showing
10 changed files
with
188 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
using idee5.Globalization.Commands; | ||
using idee5.Globalization.Models; | ||
|
||
using MELT; | ||
|
||
using Microsoft.Extensions.Logging; | ||
using Microsoft.Extensions.Logging.Abstractions; | ||
using Microsoft.VisualStudio.TestTools.UnitTesting; | ||
|
||
using System; | ||
using System.Collections.Generic; | ||
using System.Collections.Immutable; | ||
using System.Linq; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
|
||
namespace idee5.Globalization.Test; | ||
|
||
[TestClass] | ||
public class UpdateResourceKeyTests : UnitTestBase { | ||
private const string _resourceSet = "UpdateResourceKey"; | ||
|
||
[TestInitialize] | ||
public void TestInitialize() { | ||
if (!context.Resources.Any(Specifications.InResourceSet(_resourceSet))) { | ||
resourceUnitOfWork.ResourceRepository.Add(new Resource { Id = "DeRemove", ResourceSet = _resourceSet, BinFile = null, Textfile = null, Comment = null, Customer = "", Industry = "", Language = "de", Value = "xx" }); | ||
resourceUnitOfWork.ResourceRepository.Add(new Resource { Id = "DeRemove", ResourceSet = _resourceSet, BinFile = null, Textfile = null, Comment = null, Customer = "", Industry = "", Language = "en-GB", Value = "xxx" }); | ||
resourceUnitOfWork.ResourceRepository.Add(new Resource { Id = "LogTest", ResourceSet = _resourceSet, BinFile = null, Textfile = null, Comment = null, Customer = "", Industry = "", Language = "en-GB", Value = "xxx" }); | ||
context.SaveChanges(); | ||
} | ||
} | ||
|
||
[TestMethod] | ||
public async Task CanCreateUpdateAndDeleteTranslation() { | ||
// Arrange | ||
var handler = new UpdateResourceKeyCommandHandler(resourceUnitOfWork, new NullLogger<UpdateResourceKeyCommandHandler>()); | ||
var rk = new ResourceKey() { ResourceSet = _resourceSet, Id ="DeRemove" }; | ||
var translations = new Dictionary<string, string> { | ||
{ "en-GB", "lsmf" }, | ||
{ "it", "xyz" } | ||
}; | ||
var command = new UpdateResourceKeyCommand(rk, translations.ToImmutableDictionary()); | ||
|
||
// Act | ||
await handler.HandleAsync(command, CancellationToken.None).ConfigureAwait(false); | ||
|
||
// Assert | ||
var rsc = await resourceUnitOfWork.ResourceRepository.GetAsync(r => r.ResourceSet == _resourceSet).ConfigureAwait(false); | ||
Assert.AreEqual(2, rsc.Count); | ||
Assert.AreEqual("lsmf", rsc.SingleOrDefault(r => r.Language == "en-GB")?.Value); | ||
Assert.AreEqual("xyz", rsc.SingleOrDefault(r => r.Language == "it")?.Value); | ||
} | ||
|
||
[TestMethod] | ||
public async Task CanCreateLogs() { | ||
// Arrange | ||
var loggerFactory = TestLoggerFactory.Create(); | ||
var logger = loggerFactory.CreateLogger<UpdateResourceKeyCommandHandler>(); | ||
var handler = new UpdateResourceKeyCommandHandler(resourceUnitOfWork, logger); | ||
var rk = new ResourceKey() { ResourceSet = _resourceSet, Id ="LogTest" }; | ||
var translations = new Dictionary<string, string> { | ||
{ "en-GB", "lsmf" }, | ||
{ "it", "xyz" } | ||
}; | ||
var command = new UpdateResourceKeyCommand(rk, translations.ToImmutableDictionary()); | ||
|
||
// Act | ||
await handler.HandleAsync(command, CancellationToken.None).ConfigureAwait(false); | ||
|
||
|
||
// Assert | ||
Assert.AreEqual(4, loggerFactory.Sink.LogEntries.Count()); | ||
// 2 create/update events | ||
Assert.AreEqual(2, loggerFactory.Sink.LogEntries.Count(le => le.EventId.Id == 4)); | ||
Assert.AreEqual(2, loggerFactory.Sink.LogEntries.Single(le =>le.EventId == 2).Properties.Single(p => p.Key == "Count").Value); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
using idee5.Globalization.Models; | ||
|
||
using System; | ||
using System.Collections.Generic; | ||
using System.Collections.Immutable; | ||
using System.Text; | ||
|
||
namespace idee5.Globalization.Commands; | ||
/// <summary> | ||
/// The update resource key command | ||
/// </summary> | ||
public record UpdateResourceKeyCommand : ResourceKey { | ||
public UpdateResourceKeyCommand(ResourceKey original, ImmutableDictionary<string, string> translations) : base(original) { | ||
Translations = translations ?? throw new ArgumentNullException(nameof(translations)); | ||
} | ||
|
||
/// <summary> | ||
/// Translations of the <see cref="ResourceKey">. | ||
/// The dictionary key is the <see cref="Resource.Language"/>, the value is the <see cref="Resource.Value"/> | ||
/// </summary> | ||
public ImmutableDictionary<string, string> Translations { get; set; } | ||
} |
46 changes: 46 additions & 0 deletions
46
idee5.Globalization/Commands/UpdateResourceKeyCommandHandler.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
using idee5.Common; | ||
using idee5.Globalization.Models; | ||
using idee5.Globalization.Repositories; | ||
|
||
using Microsoft.Extensions.Logging; | ||
|
||
using System.Linq; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
|
||
namespace idee5.Globalization.Commands; | ||
|
||
/// <summary> | ||
/// The update resource key command handler. Removes translations NOT in the given list. | ||
/// </summary> | ||
public class UpdateResourceKeyCommandHandler : ICommandHandlerAsync<UpdateResourceKeyCommand> { | ||
private readonly IResourceUnitOfWork _unitOfWork; | ||
private readonly ILogger<UpdateResourceKeyCommandHandler> _logger; | ||
|
||
public UpdateResourceKeyCommandHandler(IResourceUnitOfWork unitOfWork, ILogger<UpdateResourceKeyCommandHandler> logger) { | ||
_unitOfWork = unitOfWork; | ||
_logger = logger; | ||
} | ||
|
||
/// <inheritdoc/> | ||
public async Task HandleAsync(UpdateResourceKeyCommand command, CancellationToken cancellationToken = default) { | ||
_logger.TranslationsReceived(command.Translations.Count, command); | ||
Resource baseResource = new() { | ||
ResourceSet = command.ResourceSet, | ||
Id = command.Id, | ||
Customer = command.Customer, | ||
Industry = command.Industry | ||
}; | ||
// first remove all missing translations | ||
_logger.RemovingTranslations(); | ||
await _unitOfWork.ResourceRepository.RemoveAsync(Specifications.OfResourceKey(baseResource) & !Specifications.TranslatedTo(command.Translations.Keys), cancellationToken).ConfigureAwait(false); | ||
|
||
// then update or add the given translations | ||
foreach (var translation in command.Translations) { | ||
Resource rsc = baseResource with { Language = translation.Key, Value = translation.Value }; | ||
_logger.CreateOrUpdateResource(rsc); | ||
await _unitOfWork.ResourceRepository.UpdateOrAddAsync(rsc, cancellationToken).ConfigureAwait(false); | ||
} | ||
await _unitOfWork.SaveChangesAsync(cancellationToken).ConfigureAwait(false); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,16 @@ | ||
using Microsoft.Extensions.Logging; | ||
using idee5.Globalization.Models; | ||
|
||
using Microsoft.Extensions.Logging; | ||
|
||
namespace idee5.Globalization; | ||
internal static partial class Log { | ||
[LoggerMessage(1, LogLevel.Warning, "Resx file '{FilePath}' not found!")] | ||
public static partial void ResxFileNotFound(this ILogger logger, string FilePath); | ||
[LoggerMessage(2,LogLevel.Debug, "Received {Count} translations for {Resource}")] | ||
public static partial void TranslationsReceived(this ILogger logger, int Count, ResourceKey Resource); | ||
|
||
[LoggerMessage(3, LogLevel.Debug, "Removing translations ...")] | ||
public static partial void RemovingTranslations(this ILogger logger); | ||
[LoggerMessage(4, LogLevel.Debug, "Create or update Resource: {Resource}")] | ||
public static partial void CreateOrUpdateResource(this ILogger logger, Resource Resource); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters