using System.ComponentModel.DataAnnotations; using System.Diagnostics.CodeAnalysis; using WeatherForecast.Models.Abstractions; using Core.DomainObjects.Documents; using Core.Enumerations; using Extensions; namespace WeatherForecast.Models.Requests { /// /// /// public class ShopItemRequestModel : PostItemRequestModelBase { /// /// /// public string? BrandName { get; set; } /// /// /// public decimal? Rating { get; set; } /// /// /// public decimal? Price { get; set; } /// /// /// public decimal? NewPrice { get; set; } /// /// /// public uint? Quantity { get; set; } /// /// /// /// public override ShopItem ToDomainObject() { if (HasValidationErrors(this) || HasValidationErrorsBase(HasValidationErrors(this))) throw new ValidationException(); return new ShopItem() { L10n = L10n.Select(x => x.ToDomainObject()).ToList(), // Images // Author Created = DateTime.UtcNow, Tags = Tags, Categories = Categories, FamilyFriendly = FamilyFriendly, BrandName = BrandName, Rating = Rating, Price = Price.Value, NewPrice = NewPrice, Quantity = Quantity.Value }; } /// /// /// /// /// public override IEnumerable Validate(ValidationContext validationContext) { if (L10n.IsNullOrEmpty()) yield return new ValidationResult($"{nameof(L10n)} ${Errors.NullOrEmpty}"); if (string.IsNullOrWhiteSpace(BrandName)) yield return new ValidationResult($"{nameof(BrandName)} ${Errors.NullOrWhiteSpace}"); if (Price == null || (Price != null && Price == 0)) yield return new ValidationResult($"{nameof(Price)} {Errors.NullOrEmpty.Name}"); if (Quantity == null || (Quantity != null && Quantity == 0)) yield return new ValidationResult($"{nameof(Quantity)} ${Errors.NullOrEmpty}"); } [MemberNotNullWhen(false, new[] { nameof(BrandName), nameof(Price), nameof(Quantity) })] private bool HasValidationErrors(ShopItemRequestModel model) => Validate(new ValidationContext(model)).Any(); } }