mirror of https://github.com/Squidex/squidex.git
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
100 lines
3.5 KiB
100 lines
3.5 KiB
// ==========================================================================
|
|
// GuardApp.cs
|
|
// Squidex Headless CMS
|
|
// ==========================================================================
|
|
// Copyright (c) Squidex Group
|
|
// All rights reserved.
|
|
// ==========================================================================
|
|
|
|
using Squidex.Domain.Apps.Core.Apps;
|
|
using Squidex.Domain.Apps.Write.Apps.Commands;
|
|
using Squidex.Infrastructure;
|
|
|
|
namespace Squidex.Domain.Apps.Write.Apps.Guards
|
|
{
|
|
public static class GuardAppLanguages
|
|
{
|
|
public static void CanAdd(LanguagesConfig languages, AddLanguage command)
|
|
{
|
|
Guard.NotNull(command, nameof(command));
|
|
|
|
Validate.It(() => "Cannot add language.", error =>
|
|
{
|
|
if (command.Language == null)
|
|
{
|
|
error(new ValidationError("Language cannot be null.", nameof(command.Language)));
|
|
}
|
|
else if (languages.Contains(command.Language))
|
|
{
|
|
error(new ValidationError("Language already added.", nameof(command.Language)));
|
|
}
|
|
});
|
|
}
|
|
|
|
public static void CanRemove(LanguagesConfig languages, RemoveLanguage command)
|
|
{
|
|
Guard.NotNull(command, nameof(command));
|
|
|
|
var languageConfig = GetLanguageConfigOrThrow(languages, command.Language);
|
|
|
|
Validate.It(() => "Cannot remove language.", error =>
|
|
{
|
|
if (command.Language == null)
|
|
{
|
|
error(new ValidationError("Language cannot be null.", nameof(command.Language)));
|
|
}
|
|
|
|
if (languages.Master == languageConfig)
|
|
{
|
|
error(new ValidationError("Language config is master.", nameof(command.Language)));
|
|
}
|
|
});
|
|
}
|
|
|
|
public static void CanUpdate(LanguagesConfig languages, UpdateLanguage command)
|
|
{
|
|
Guard.NotNull(command, nameof(command));
|
|
|
|
var languageConfig = GetLanguageConfigOrThrow(languages, command.Language);
|
|
|
|
Validate.It(() => "Cannot update language.", error =>
|
|
{
|
|
if (command.Language == null)
|
|
{
|
|
error(new ValidationError("Language cannot be null.", nameof(command.Language)));
|
|
}
|
|
|
|
if ((languages.Master == languageConfig || command.IsMaster) && command.IsOptional)
|
|
{
|
|
error(new ValidationError("Cannot make master language optional.", nameof(command.IsMaster)));
|
|
}
|
|
|
|
if (command.Fallback != null)
|
|
{
|
|
foreach (var fallback in command.Fallback)
|
|
{
|
|
if (!languages.Contains(fallback))
|
|
{
|
|
error(new ValidationError($"Config does not contain fallback language {fallback}.", nameof(command.Fallback)));
|
|
}
|
|
}
|
|
}
|
|
});
|
|
}
|
|
|
|
private static LanguageConfig GetLanguageConfigOrThrow(LanguagesConfig languages, Language language)
|
|
{
|
|
if (language == null)
|
|
{
|
|
return null;
|
|
}
|
|
|
|
if (!languages.TryGetConfig(language, out var languageConfig))
|
|
{
|
|
throw new DomainObjectNotFoundException(language, "Languages", typeof(AppDomainObject));
|
|
}
|
|
|
|
return languageConfig;
|
|
}
|
|
}
|
|
}
|
|
|