Files
parr_api/PARR.API/Validators/TnkRequestValidator.cs

41 lines
1.5 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using FluentValidation;
using PARR.API.Contracts.V1.Requests;
using PARR.DAL.Services.Interfaces;
namespace PARR.API.Validators
{
public class TnkRequestValidator : AbstractValidator<TnkRequest>
{
private readonly ISubprocessService subprocessService;
private bool? isValid = null;
public TnkRequestValidator(
ISubprocessService subprocessService
)
{
this.subprocessService = subprocessService;
RuleFor(t => t.Name).NotEmpty().NotNull().WithMessage("Имя ТНК не может быть пустым");
RuleFor(t => t.EsppId).NotEmpty().NotNull().WithMessage("EsppId не может быть пустым");
RuleFor(t => t.SubprocessId).NotEmpty().NotNull().WithMessage("ТНК должна быть связана с подпроцессом. SubprocessId не может быть пустым");
RuleFor(t => t.SubprocessId)
.MustAsync(async (entity, value, c) => await IsSubprocessExist(entity))
.WithMessage("У данной ТНК указан несуществующий Id подпроцесса");
}
private async Task<bool> IsSubprocessExist(TnkRequest request)
{
//чтобы не делать 2 раза валидацию
if (isValid.HasValue)
return isValid.Value;
isValid = await subprocessService.GetAsync(request.SubprocessId) != null;
return isValid.Value;
}
}
}