53 lines
1.5 KiB
C#
53 lines
1.5 KiB
C#
using Core.Abstractions.Models;
|
|
using Core.DomainObjects;
|
|
using Core.DomainObjects.L10n;
|
|
using Core.Enumerations;
|
|
using Extensions;
|
|
using System.ComponentModel.DataAnnotations;
|
|
using System.Diagnostics.CodeAnalysis;
|
|
using WeatherForecast.Models.Requests.L10n;
|
|
|
|
namespace WeatherForecast.Models.Requests {
|
|
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
public class CategoryItemRequestModel : RequestModelBase<Category> {
|
|
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
public List<CategoryL10nModel>? L10n { get; set; }
|
|
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
public override Category ToDomainObject() {
|
|
if (HasValidationErrors()) throw new ValidationException();
|
|
|
|
return new Category() {
|
|
L10n = L10n.Select(x => x.ToDomainObject()).ToList()
|
|
};
|
|
}
|
|
|
|
/// <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;
|
|
}
|
|
}
|
|
|
|
[MemberNotNullWhen(false, new[] { nameof(L10n) })]
|
|
private bool HasValidationErrors() => Validate(new ValidationContext(this)).Any();
|
|
}
|
|
}
|