From d6517ace27253c0dc7b893116df8b45926b4f4eb Mon Sep 17 00:00:00 2001 From: Sebastian Date: Wed, 15 Jan 2020 20:42:07 +0100 Subject: [PATCH] Another minor fix. --- .../Assets/State/AssetFolderState.cs | 2 +- .../Assets/State/AssetState.cs | 27 ++++++++++++++++--- .../src/Squidex.Infrastructure/Commands/Is.cs | 12 ++++----- 3 files changed, 30 insertions(+), 11 deletions(-) diff --git a/backend/src/Squidex.Domain.Apps.Entities/Assets/State/AssetFolderState.cs b/backend/src/Squidex.Domain.Apps.Entities/Assets/State/AssetFolderState.cs index cd99c14a1..d3ed1ffac 100644 --- a/backend/src/Squidex.Domain.Apps.Entities/Assets/State/AssetFolderState.cs +++ b/backend/src/Squidex.Domain.Apps.Entities/Assets/State/AssetFolderState.cs @@ -42,7 +42,7 @@ namespace Squidex.Domain.Apps.Entities.Assets.State return true; } - case AssetFolderRenamed e when Is.ChangeWhenDefined(FolderName, e.FolderName): + case AssetFolderRenamed e when Is.OptionalChange(FolderName, e.FolderName): { FolderName = e.FolderName; diff --git a/backend/src/Squidex.Domain.Apps.Entities/Assets/State/AssetState.cs b/backend/src/Squidex.Domain.Apps.Entities/Assets/State/AssetState.cs index 5fec08529..ed5278f6a 100644 --- a/backend/src/Squidex.Domain.Apps.Entities/Assets/State/AssetState.cs +++ b/backend/src/Squidex.Domain.Apps.Entities/Assets/State/AssetState.cs @@ -86,6 +86,8 @@ namespace Squidex.Domain.Apps.Entities.Assets.State TotalSize += e.FileSize; + EnsureProperties(); + return true; } @@ -95,6 +97,8 @@ namespace Squidex.Domain.Apps.Entities.Assets.State TotalSize += e.FileSize; + EnsureProperties(); + return true; } @@ -102,34 +106,36 @@ namespace Squidex.Domain.Apps.Entities.Assets.State { var hasChanged = false; - if (Is.ChangeWhenDefined(FileName, e.FileName)) + if (Is.OptionalChange(FileName, e.FileName)) { FileName = e.FileName; hasChanged = true; } - if (Is.ChangeWhenDefined(Slug, e.Slug)) + if (Is.OptionalChange(Slug, e.Slug)) { Slug = e.Slug; hasChanged = true; } - if (Is.Change(Tags, e.Tags)) + if (Is.OptionalChange(Tags, e.Tags)) { Tags = e.Tags; hasChanged = true; } - if (Is.Change(Metadata, e.Metadata)) + if (Is.OptionalChange(Metadata, e.Metadata)) { Metadata = e.Metadata; hasChanged = true; } + EnsureProperties(); + return hasChanged; } @@ -150,5 +156,18 @@ namespace Squidex.Domain.Apps.Entities.Assets.State return false; } + + private void EnsureProperties() + { + if (Tags == null) + { + Tags = new HashSet(); + } + + if (Metadata == null) + { + Metadata = new AssetMetadata(); + } + } } } diff --git a/backend/src/Squidex.Infrastructure/Commands/Is.cs b/backend/src/Squidex.Infrastructure/Commands/Is.cs index 306be80e9..ef36fdf11 100644 --- a/backend/src/Squidex.Infrastructure/Commands/Is.cs +++ b/backend/src/Squidex.Infrastructure/Commands/Is.cs @@ -18,24 +18,24 @@ namespace Squidex.Infrastructure.Commands return !Equals(oldValue, newValue); } - public static bool Change(string? oldValue, string newValue) + public static bool Change(string oldValue, string newValue) { return !Equals(oldValue, newValue); } - public static bool ChangeWhenDefined(string? oldValue, string? newValue) + public static bool OptionalChange(string oldValue, string? newValue) { return !string.IsNullOrWhiteSpace(newValue) && !string.Equals(oldValue, newValue); } - public static bool Change(ISet? oldValue, [NotNullWhen(true)] ISet? newValue) + public static bool OptionalChange(ISet oldValue, [NotNullWhen(true)] ISet? newValue) { - return newValue != null && (oldValue == null || !newValue.SetEquals(oldValue)); + return newValue != null && !newValue.SetEquals(oldValue); } - public static bool Change(IReadOnlyDictionary? oldValue, [NotNullWhen(true)] IReadOnlyDictionary? newValue) where TKey : notnull + public static bool OptionalChange(IReadOnlyDictionary oldValue, [NotNullWhen(true)] IReadOnlyDictionary? newValue) where TKey : notnull { - return newValue != null && (oldValue == null || !newValue.EqualsDictionary(oldValue)); + return newValue != null && !newValue.EqualsDictionary(oldValue); } } } \ No newline at end of file