using FluentValidation; using Microsoft.EntityFrameworkCore; using Newtonsoft.Json.Linq; using PARR.API.Contracts.V1.Requests; using PARR.DAL.Contracts; using PARR.DAL.Services.Interfaces.Job; using PARR.DAL.Services.Interfaces.Unit; namespace PARR.API.Validators { public class JobGroupValidator : AbstractValidator { public JobGroupValidator( IJobGroupTypeService jobGroupTypeService, IUnitFieldService unitFieldService ) { RuleFor(t => t.Name) .NotNull().NotEmpty(); RuleFor(t => t.ShortDescription) .NotNull().NotEmpty(); RuleFor(t => t.FullDescription) .NotNull().NotEmpty(); RuleFor(t => t.Solution) .NotNull().NotEmpty(); RuleFor(t => t.TemplateDuration) .NotNull().NotEmpty(); RuleFor(t => t.GroupTypeId) .MustAsync(async (entity, value, c) => await jobGroupTypeService.GetAsync(value) != null) .WithMessage("Указан несуществующий Id типа"); //Проверяем существует ли такой GroupingUnitFieldId в unitField RuleFor(t => t.GroupingUnitFieldId) .MustAsync(async (entity, value, c) => { if (!value.HasValue) { return true; } return await unitFieldService.GetAsync(value.Value) != null; }) .WithMessage("Указано несуществующий Id поля"); RuleFor(t => t.GroupingUnitFieldId) .MustAsync(async (entity, value, c) => { var groupingJobType = await jobGroupTypeService.Get().FirstAsync(t => t.Code == JobGroupTypesEnum.Group); // Если это сгруппированный тип, то у него обязательно должно быть заполнено поле GroupingUnitFieldId if (entity.GroupTypeId == groupingJobType.Id) return value.HasValue; return true; }) .WithMessage("Не указано поле для группировки"); } } }