using Core.Abstractions.Models; using Core.DomainObjects; using Core.DomainObjects.Documents; using Core.Enumerations; using System.ComponentModel.DataAnnotations; using System.Diagnostics.CodeAnalysis; namespace WeatherForecast.Models.Requests { /// /// /// public class ShopCartItemRequestModel : RequestModelBase { /// /// /// public uint? Quantity { get; set; } /// /// /// /// public override ShopCartItem ToDomainObject() { if (HasValidationErrors()) throw new ValidationException(); return new ShopCartItem() { Quantity = Quantity.Value, Created = DateTime.UtcNow }; } /// /// /// /// /// public override IEnumerable Validate(ValidationContext validationContext) { if (Quantity == null || (Quantity != null && Quantity == 0)) yield return new ValidationResult($"{nameof(Quantity)} {Errors.NullOrEmpty}"); } [MemberNotNullWhen(false, new[] { nameof(Quantity) })] private bool HasValidationErrors() => Validate(new ValidationContext(this)).Any(); } }