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 { /// /// /// public class PostItemL10nModel : RequestModelBase { /// /// /// public string? Locale { get; set; } /// /// /// public string? Slug { get; set; } /// /// /// public string? Description { get; set; } /// /// /// public string? Title { get; set; } /// /// /// public string? ShortText { get; set; } /// /// /// public string? Text { get; set; } /// /// /// public string? PlainText { get; set; } /// /// /// public string? ContentType { get; set; } /// /// /// public List? Badges { get; set; } /// /// /// /// public override PostItemL10n ToDomainObject() { if (HasValidationErrors()) throw new ValidationException(); return new PostItemL10n() { Locale = Enumeration.FromDisplayName(Locale), Slug = Slug, Description = Description, Title = Title, Text = Text, ShortText = ShortText, // TODO: create plain text creation logic PlainText = "TODO", ContentType = Enumeration.FromDisplayName(ContentType), Badges = Badges }; } /// /// /// /// /// public override IEnumerable Validate(ValidationContext validationContext) { if (string.IsNullOrWhiteSpace(Locale)) yield return new ValidationResult($"{validationContext.DisplayName} {Errors.NullOrWhiteSpace.Name}"); else if (Enumeration.FromDisplayName(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(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(); } }