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 ImageL10nModel : RequestModelBase { /// /// /// public string? Locale { get; set; } /// /// /// public string? Alt { get; set; } /// /// /// /// public override ImageL10n ToDomainObject() { if (HasValidationErrors()) throw new ValidationException(); return new ImageL10n() { Locale = Enumeration.FromDisplayName(Locale), Alt = Alt }; } /// /// /// /// /// public override IEnumerable Validate(ValidationContext validationContext) { if (string.IsNullOrWhiteSpace(Locale)) yield return new ValidationResult($"{nameof(Locale)} {Errors.NullOrWhiteSpace.Name}"); else if (Enumeration.FromDisplayName(Locale) == null) yield return new ValidationResult($"{nameof(Locale)} {Errors.WrongOrNotManaged}"); if (string.IsNullOrWhiteSpace(Alt)) yield return new ValidationResult($"{nameof(Alt)} {Errors.NullOrWhiteSpace.Name}"); } [MemberNotNullWhen(false, new[] { nameof(Locale), nameof(Alt) })] private bool HasValidationErrors() => Validate(new ValidationContext(this)).Any(); } }