119 lines
3.6 KiB
C#
119 lines
3.6 KiB
C#
using Core.Abstractions;
|
|
using Core.Abstractions.Models;
|
|
using Core.DomainObjects.L10n;
|
|
using Core.Enumerations;
|
|
using System.ComponentModel.DataAnnotations;
|
|
using System.Diagnostics.CodeAnalysis;
|
|
|
|
namespace WeatherForecast.Models.Requests.L10n {
|
|
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
public class PostItemL10nModel : RequestModelBase<PostItemL10n> {
|
|
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
public string? Locale { get; set; }
|
|
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
public string? Slug { get; set; }
|
|
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
public string? Description { get; set; }
|
|
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
public string? Title { get; set; }
|
|
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
public string? ShortText { get; set; }
|
|
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
public string? Text { get; set; }
|
|
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
public string? PlainText { get; set; }
|
|
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
public string? ContentType { get; set; }
|
|
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
public List<string>? Badges { get; set; }
|
|
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
public override PostItemL10n ToDomainObject() {
|
|
if (HasValidationErrors()) throw new ValidationException();
|
|
|
|
return new PostItemL10n() {
|
|
Locale = Enumeration.FromDisplayName<Locales>(Locale),
|
|
Slug = Slug,
|
|
Description = Description,
|
|
Title = Title,
|
|
Text = Text,
|
|
ShortText = ShortText,
|
|
|
|
// TODO: create plain text creation logic
|
|
PlainText = "TODO",
|
|
|
|
ContentType = Enumeration.FromDisplayName<ContentTypes>(ContentType),
|
|
Badges = Badges
|
|
};
|
|
}
|
|
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
/// <param name="validationContext"></param>
|
|
/// <returns></returns>
|
|
public override IEnumerable<ValidationResult> Validate(ValidationContext validationContext) {
|
|
if (string.IsNullOrWhiteSpace(Locale))
|
|
yield return new ValidationResult($"{validationContext.DisplayName} {Errors.NullOrWhiteSpace.Name}");
|
|
else if (Enumeration.FromDisplayName<Locales>(Locale) == null)
|
|
yield return new ValidationResult($"{validationContext.DisplayName} {Errors.WrongOrNotManaged}");
|
|
|
|
if (string.IsNullOrWhiteSpace(Slug))
|
|
yield return new ValidationResult($"{validationContext.DisplayName} {Errors.NullOrWhiteSpace.Name}");
|
|
|
|
if (string.IsNullOrWhiteSpace(Description))
|
|
yield return new ValidationResult($"{validationContext.DisplayName} {Errors.NullOrWhiteSpace.Name}");
|
|
|
|
if (string.IsNullOrWhiteSpace(Title))
|
|
yield return new ValidationResult($"{validationContext.DisplayName} {Errors.NullOrWhiteSpace.Name}");
|
|
|
|
if (string.IsNullOrWhiteSpace(Text))
|
|
yield return new ValidationResult($"{validationContext.DisplayName} {Errors.NullOrWhiteSpace.Name}");
|
|
|
|
if (string.IsNullOrWhiteSpace(ShortText))
|
|
yield return new ValidationResult($"{validationContext.DisplayName} {Errors.NullOrWhiteSpace.Name}");
|
|
|
|
if (string.IsNullOrWhiteSpace(ContentType))
|
|
yield return new ValidationResult($"{validationContext.DisplayName} {Errors.NullOrWhiteSpace.Name}");
|
|
else if (Enumeration.FromDisplayName<ContentTypes>(ContentType) == null)
|
|
yield return new ValidationResult($"{validationContext.DisplayName} {Errors.WrongOrNotManaged}");
|
|
}
|
|
|
|
[MemberNotNullWhen(false, new[] { nameof(Locale), nameof(Slug), nameof(Description), nameof(Title), nameof(Text), nameof(ShortText), nameof(ContentType) })]
|
|
private bool HasValidationErrors() => Validate(new ValidationContext(this)).Any();
|
|
}
|
|
}
|