using Core.Abstractions.Models;
using Core.DomainObjects;
using Core.DomainObjects.Documents;
using Core.DomainObjects.L10n;
using Core.Enumerations;
using ExtensionMethods;
using Extensions;
using System.ComponentModel.DataAnnotations;
using System.Diagnostics.CodeAnalysis;
using WeatherForecast.Models.Abstractions;
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($"{validationContext.DisplayName} ${Errors.NullOrEmpty}");
else {
foreach (var item in L10n)
foreach (var validationResult in item.Validate(validationContext))
yield return validationResult;
}
if (string.IsNullOrWhiteSpace(BrandName))
yield return new ValidationResult($"{validationContext.DisplayName} ${Errors.NullOrWhiteSpace}");
if (Price == null)
yield return new ValidationResult($"{validationContext.DisplayName} {Errors.NullOrEmpty.Name}");
if (Quantity == null)
yield return new ValidationResult($"{validationContext.DisplayName} ${Errors.NullOrEmpty}");
}
[MemberNotNullWhen(false, new[] { nameof(BrandName), nameof(Price), nameof(Quantity) })]
private bool HasValidationErrors(ShopItemRequestModel validationContext) => Validate(new ValidationContext(validationContext)).Any();
}
}