Files
parr_api/PARR.API/Validators/SubprocessValidator.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 SubprocessValidator : AbstractValidator<SubprocessRequest>
{
private readonly IProcessService processService;
private bool? isValid = null;
public SubprocessValidator(
IProcessService processService
)
{
this.processService = processService;
RuleFor(t => t.Name).NotEmpty().NotNull().WithMessage("Имя подпроцесса не может быть пустым");
RuleFor(t => t.EsppId).NotEmpty().NotNull().WithMessage("EsppId не может быть пустым");
RuleFor(t => t.ProcessId).NotEmpty().NotNull().WithMessage("Подпроцесс должна быть связан с процессом. ProcessId не может быть пустым");
RuleFor(t => t.ProcessId)
.MustAsync(async (entity, value, c) => await IsProcessExist(entity))
.WithMessage("У данного подпроцесса указан несуществующий Id процесса");
}
private async Task<bool> IsProcessExist(SubprocessRequest request)
{
//чтобы не делать 2 раза валидацию
if (isValid.HasValue)
return isValid.Value;
isValid = await processService.GetAsync(request.ProcessId) != null;
return isValid.Value;
}
}
}