96 lines
2.7 KiB
C#
96 lines
2.7 KiB
C#
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 {
|
|
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
public class ShopItemRequestModel : PostItemRequestModelBase<ShopItem> {
|
|
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
public string? BrandName { get; set; }
|
|
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
public decimal? Rating { get; set; }
|
|
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
public decimal? Price { get; set; }
|
|
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
public decimal? NewPrice { get; set; }
|
|
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
public uint? Quantity { get; set; }
|
|
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
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
|
|
};
|
|
}
|
|
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
/// <param name="validationContext"></param>
|
|
/// <returns></returns>
|
|
public override IEnumerable<ValidationResult> 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();
|
|
}
|
|
}
|