reactredux/webapi/WeatherForecast/Models/Requests/MediaAttachmentRequestModel.cs

70 lines
2.0 KiB
C#

using Core.Abstractions;
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 {
/// <summary>
///
/// </summary>
public class MediaAttachmentRequestModel : RequestModelBase<MediaAttachment> {
/// <summary>
///
/// </summary>
public string? Src { get; set; }
/// <summary>
///
/// </summary>
public string? MediaType { get; set; }
/// <summary>
///
/// </summary>
public List<MediaAttachmentL10nModel>? L10n { get; set; }
/// <summary>
///
/// </summary>
/// <returns></returns>
/// <exception cref="NotImplementedException"></exception>
public override MediaAttachment ToDomainObject() {
if (HasValidationErrors(this))
throw new ValidationException();
return new MediaAttachment {
Src = Src,
MediaType = Enumeration.FromDisplayName<MediaTypes>(MediaType),
L10n = L10n.Select(x => x.ToDomainObject()).ToList()
};
}
/// <summary>
///
/// </summary>
/// <param name="validationContext"></param>
/// <returns></returns>
/// <exception cref="NotImplementedException"></exception>
public override IEnumerable<ValidationResult> Validate(ValidationContext validationContext) {
if (string.IsNullOrWhiteSpace(Src))
yield return new ValidationResult($"{nameof(Src)} {Errors.NullOrWhiteSpace}");
if (string.IsNullOrWhiteSpace(MediaType))
yield return new ValidationResult($"{nameof(MediaType)} {Errors.NullOrWhiteSpace}");
if (L10n.IsNullOrEmpty())
yield return new ValidationResult($"{nameof(L10n)} {Errors.NullOrEmpty}");
}
[MemberNotNullWhen(false, new[] { nameof(Src), nameof(MediaType), nameof(L10n) })]
private bool HasValidationErrors(MediaAttachmentRequestModel model) => Validate(new ValidationContext(model)).Any();
}
}