feat(api): ApplicationInWorkValidator добававлена валидация планировщика, ApplicationInWorkController добавлена обьязательная настройка планировщика
This commit is contained in:
@@ -19,6 +19,8 @@ namespace PARR.API.Validators
|
||||
this.applicationsInWorkService = applicationsInWorkService;
|
||||
this.workService = workService;
|
||||
this.esppSchTypeConfigService = esppSchTypeConfigService;
|
||||
|
||||
|
||||
RuleFor(t => t.TemplateDuration)
|
||||
.NotNull().NotEmpty().WithMessage("Длительность данного задания на выполнение работ не может быть пустым")
|
||||
.Matches("^\\d{1,2}\\s\\d{1,2}[:]\\d{1,2}[:]\\d{1,2}$")
|
||||
@@ -45,53 +47,98 @@ namespace PARR.API.Validators
|
||||
//Проверяем настройки планировщика
|
||||
RuleFor(t => t.Schedule).NotNull().NotEmpty().WithMessage("Настройки планировщика задания на выполнение работ не могут быть пустыми");
|
||||
|
||||
RuleFor(t => t.Schedule.TypeScheduleId)
|
||||
.NotNull().NotEmpty().WithMessage($"Тип планировщика задания на выполнение работ не может быть пустым")
|
||||
.Must((entity, value, c) => IsTypeScheduleIdExist(entity))
|
||||
.When(t => t.Schedule != null).WithMessage($"Не удалось найти указанный тип планировщика задания на выполнение работ");
|
||||
RuleFor(t => t.Schedule.EsppSchValues)
|
||||
.NotNull().NotEmpty().When(t => t.Schedule != null).WithMessage("Настройки планировщика задания на выполнение работ не могут быть пустыми")
|
||||
.Must((entity, value, c) => IsEsppSchValuesExist(entity)).WithMessage("Не удалось найти подходящую конфигруцию планировщика задания на выполнение работ");
|
||||
|
||||
RuleFor(t => t.Schedule.Values)
|
||||
.NotNull().NotEmpty().WithMessage($"Настройки значений планировщика задания на выполнение работ не могут быть пустыми")
|
||||
.Must((entity, value, c) => IsScheduleTypeValueIdExist(entity))
|
||||
.When(t => t.Schedule != null && t.Schedule.TypeScheduleId > 0).WithMessage("Не удалось найти подходящую конфигруцию планировщика задания на выполнение работ");
|
||||
//RuleFor(t => t.Schedule.Values)
|
||||
// .NotNull().NotEmpty().WithMessage($"Настройки значений планировщика задания на выполнение работ не могут быть пустыми")
|
||||
// .Must((entity, value, c) => IsScheduleTypeValueIdExist(entity))
|
||||
// .When(t => t.Schedule != null).WithMessage("Не удалось найти подходящую конфигруцию планировщика задания на выполнение работ");
|
||||
|
||||
//RuleFor(t => t.Schedule.TypeScheduleId)
|
||||
// .NotNull().NotEmpty().WithMessage($"Тип планировщика задания на выполнение работ не может быть пустым")
|
||||
// .Must((entity, value, c) => IsTypeScheduleIdExist(entity))
|
||||
// .When(t => t.Schedule != null).WithMessage($"Не удалось найти указанный тип планировщика задания на выполнение работ");
|
||||
|
||||
//RuleFor(t => t.Schedule.Values)
|
||||
// .NotNull().NotEmpty().WithMessage($"Настройки значений планировщика задания на выполнение работ не могут быть пустыми")
|
||||
// .Must((entity, value, c) => IsScheduleTypeValueIdExist(entity))
|
||||
// .When(t => t.Schedule != null).WithMessage("Не удалось найти подходящую конфигруцию планировщика задания на выполнение работ");
|
||||
}
|
||||
|
||||
private bool IsTypeScheduleIdExist(ApplicationInWorkRequest request)
|
||||
{
|
||||
var configs = esppSchTypeConfigService.GetWithSchIncludes().ToList();
|
||||
var result = configs.Where(t => t.TypeScheduleId == request.Schedule.TypeScheduleId).FirstOrDefault() != null;
|
||||
return result;
|
||||
}
|
||||
|
||||
private bool IsScheduleTypeValueIdExist(ApplicationInWorkRequest request)
|
||||
private bool IsEsppSchValuesExist(ApplicationInWorkRequest request)
|
||||
{
|
||||
var configs = esppSchTypeConfigService.GetWithSchIncludes().ToList();
|
||||
|
||||
var mustValues = configs.Where(t => t.TypeScheduleId == request.Schedule.TypeScheduleId).ToList();
|
||||
var values = request.Schedule.Values;
|
||||
//Проверяем полученные Id конфигураций и Id конфигураций в базе
|
||||
var typeConfigIdList = request.Schedule.EsppSchValues.Select(t => t.TypeConfigId);
|
||||
var foundTypeConfigIdList = configs.Where(t => typeConfigIdList.Contains(t.Id)).Distinct();
|
||||
if (foundTypeConfigIdList.Count() == 0 //Не найдено совпадений с базой
|
||||
|| foundTypeConfigIdList.Select(t => t.TypeScheduleId).Distinct().Count() > 1) //или имеют разный тип планировщика
|
||||
return false;
|
||||
|
||||
foreach (var mustValue in mustValues)
|
||||
var typeScheduleId = foundTypeConfigIdList.Select(t => t.TypeScheduleId).FirstOrDefault();
|
||||
|
||||
if (foundTypeConfigIdList.Count() != configs.Where(t => t.TypeScheduleId == typeScheduleId).Count()) //не все typeConfig найдены в базе
|
||||
return false;
|
||||
|
||||
//Теперь проверяем значения
|
||||
foreach (var item in request.Schedule.EsppSchValues)
|
||||
{
|
||||
var mlValues = mustValue?.EsppSchType?.EsppSchTypeValues.Select(t => t.Id);
|
||||
var curTypeConfig = configs.Where(t => t.Id == item.TypeConfigId).Single();
|
||||
|
||||
if (mlValues == null)
|
||||
return false;
|
||||
var isCurValueExist = curTypeConfig!.EsppSchType!.EsppSchTypeValues.Where(t => t.Id == item.TypeValueId).Any();
|
||||
|
||||
if (!mlValues.Intersect(values).Any())
|
||||
if (!isCurValueExist)
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
private async Task<bool> IsWorkExist(ApplicationInWorkRequest request)
|
||||
{
|
||||
return await workService.GetAsync(request.WorkId) != null;
|
||||
}
|
||||
|
||||
|
||||
private async Task<bool> IsApplicationExist(ApplicationInWorkRequest request)
|
||||
{
|
||||
return await workService.GetAsync(request.WorkId) != null;
|
||||
}
|
||||
|
||||
|
||||
//private bool IsTypeScheduleIdExist(ApplicationInWorkRequest request)
|
||||
//{
|
||||
// var configs = esppSchTypeConfigService.GetWithSchIncludes().ToList();
|
||||
//var result = configs.Where(t => t.TypeId == request.Schedule.TypeScheduleId).FirstOrDefault() != null;
|
||||
//return result;
|
||||
// return false;
|
||||
//}
|
||||
|
||||
|
||||
//private bool IsScheduleTypeValueIdExist(ApplicationInWorkRequest request)
|
||||
//{
|
||||
// var configs = esppSchTypeConfigService.GetWithSchIncludes().ToList();
|
||||
|
||||
// var mustValues = configs.Where(t => t.TypeScheduleId == request.Schedule.TypeScheduleId).ToList();
|
||||
// var values = request.Schedule.Values;
|
||||
|
||||
// foreach (var mustValue in mustValues)
|
||||
// {
|
||||
// var mlValues = mustValue?.EsppSchType?.EsppSchTypeValues.Select(t => t.Id);
|
||||
|
||||
// if (mlValues == null)
|
||||
// return false;
|
||||
|
||||
// if (!mlValues.Intersect(values).Any())
|
||||
// return false;
|
||||
// }
|
||||
|
||||
// return true;
|
||||
//}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user