Browse Source

Fix for restore.

pull/357/head
Sebastian 7 years ago
parent
commit
9b56f886f2
  1. 32
      src/Squidex.Domain.Apps.Core.Operations/ValidateContent/Validators/UniqueValuesValidator.cs
  2. 4
      src/Squidex.Domain.Apps.Entities/Apps/BackupApps.cs
  3. 61
      tests/Squidex.Domain.Apps.Core.Tests/Operations/ValidateContent/Validators/UniqueValuesValidatorTests.cs
  4. 3
      tools/Migrate_01/OldEvents/AssetRenamed.cs
  5. 3
      tools/Migrate_01/OldEvents/AssetTagged.cs

32
src/Squidex.Domain.Apps.Core.Operations/ValidateContent/Validators/UniqueValuesValidator.cs

@ -0,0 +1,32 @@
// ==========================================================================
// Squidex Headless CMS
// ==========================================================================
// Copyright (c) Squidex UG (haftungsbeschraenkt)
// All rights reserved. Licensed under the MIT license.
// ==========================================================================
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Squidex.Infrastructure.Tasks;
namespace Squidex.Domain.Apps.Core.ValidateContent.Validators
{
public sealed class UniqueValuesValidator<T> : IValidator
{
public Task ValidateAsync(object value, ValidationContext context, AddError addError)
{
if (value is IEnumerable<T> items && items.Any())
{
var itemsArray = items.ToArray();
if (itemsArray.Length != itemsArray.Distinct().Count())
{
addError(context.Path, "Must not contain duplicate values.");
}
}
return TaskHelper.Done;
}
}
}

4
src/Squidex.Domain.Apps.Entities/Apps/BackupApps.cs

@ -127,7 +127,7 @@ namespace Squidex.Domain.Apps.Entities.Apps
{
if (!(isReserved = await appsByNameIndex.ReserveAppAsync(appId, appName)))
{
throw new BackupRestoreException("The app id or name is not available.");
throw new BackupRestoreException("vThe app id or name is not available.");
}
}
@ -135,7 +135,7 @@ namespace Squidex.Domain.Apps.Entities.Apps
{
if (isReserved)
{
await appsByNameIndex.ReserveAppAsync(appId, appName);
await appsByNameIndex.RemoveReservationAsync(appId, appName);
}
}

61
tests/Squidex.Domain.Apps.Core.Tests/Operations/ValidateContent/Validators/UniqueValuesValidatorTests.cs

@ -0,0 +1,61 @@
// ==========================================================================
// Squidex Headless CMS
// ==========================================================================
// Copyright (c) Squidex UG (haftungsbeschraenkt)
// All rights reserved. Licensed under the MIT license.
// ==========================================================================
using System.Collections.Generic;
using System.Threading.Tasks;
using FluentAssertions;
using Squidex.Domain.Apps.Core.ValidateContent.Validators;
using Xunit;
namespace Squidex.Domain.Apps.Core.Operations.ValidateContent.Validators
{
public class UniqueValuesValidatorTests
{
private readonly List<string> errors = new List<string>();
[Fact]
public async Task Should_not_add_error_if_value_is_null()
{
var sut = new UniqueValuesValidator<int>();
await sut.ValidateAsync(null, errors);
Assert.Empty(errors);
}
[Fact]
public async Task Should_not_add_error_if_value_is_not_collection()
{
var sut = new UniqueValuesValidator<int>();
await sut.ValidateAsync("value", errors);
Assert.Empty(errors);
}
[Fact]
public async Task Should_not_add_error_if_array_contains_no_duplicates()
{
var sut = new UniqueValuesValidator<int>();
await sut.ValidateAsync(new[] { 1, 2, 3 }, errors);
Assert.Empty(errors);
}
[Fact]
public async Task Should_add_error_if_array_contains_duplicates()
{
var sut = new UniqueValuesValidator<int>();
await sut.ValidateAsync(new[] { 1, 2, 2, 3 }, errors);
errors.Should().BeEquivalentTo(
new[] { "Must not contain duplicate values." });
}
}
}

3
tools/Migrate_01/OldEvents/AssetRenamed.cs

@ -9,6 +9,7 @@ using System;
using Squidex.Domain.Apps.Events.Assets;
using Squidex.Infrastructure;
using Squidex.Infrastructure.EventSourcing;
using Squidex.Infrastructure.Reflection;
namespace Migrate_01.OldEvents
{
@ -20,7 +21,7 @@ namespace Migrate_01.OldEvents
public IEvent Migrate()
{
return new AssetAnnotated { FileName = FileName };
return SimpleMapper.Map(this, new AssetAnnotated());
}
}
}

3
tools/Migrate_01/OldEvents/AssetTagged.cs

@ -10,6 +10,7 @@ using System.Collections.Generic;
using Squidex.Domain.Apps.Events.Assets;
using Squidex.Infrastructure;
using Squidex.Infrastructure.EventSourcing;
using Squidex.Infrastructure.Reflection;
namespace Migrate_01.OldEvents
{
@ -21,7 +22,7 @@ namespace Migrate_01.OldEvents
public IEvent Migrate()
{
return new AssetAnnotated { Tags = Tags };
return SimpleMapper.Map(this, new AssetAnnotated());
}
}
}

Loading…
Cancel
Save