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

145 lines
8.1 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 ApplicationInWorkValidator : AbstractValidator<ApplicationInWorkRequest>
{
private readonly IApplicationsInWorkService applicationsInWorkService;
private readonly IWorkService workService;
private readonly IEsppSchTypeConfigService esppSchTypeConfigService;
public ApplicationInWorkValidator(
IApplicationsInWorkService applicationsInWorkService,
IWorkService workService,
IEsppSchTypeConfigService esppSchTypeConfigService
)
{
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}$")
.WithMessage("Длительность данного задания на выполнение работ должна соответствовать шаблону dd hh:mm:ss(7 00:00:00)");
RuleFor(t => t.ShortDescription)
.NotNull().NotEmpty().WithMessage("Краткое описание данного задания на выполнение работ не может быть пустым");
RuleFor(t => t.FullDescription)
.NotNull().NotEmpty().WithMessage("Подробное описание данного задания на выполнение работ не может быть пустым");
RuleFor(t => t.Solution)
.NotNull().NotEmpty().WithMessage("Решение данного задания на выполнение работ не может быть пустым");
RuleFor(t => t.WorkId)
.MustAsync(async (entity, value, c) => await IsWorkExist(entity))
.WithMessage("У данного задания на выполнение работ указан несуществующий Id работы");
RuleFor(t => t.ApplicationId)
.MustAsync(async (entity, value, c) => await IsApplicationExist(entity))
.WithMessage("У данного задания на выполнение работ указан несуществующий Id программного обеспечения");
//Проверяем настройки планировщика
RuleFor(t => t.Schedule).NotNull().NotEmpty().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).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 IsEsppSchValuesExist(ApplicationInWorkRequest request)
{
var configs = esppSchTypeConfigService.GetWithSchIncludes().ToList();
//Проверяем полученные 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;
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 curTypeConfig = configs.Where(t => t.Id == item.TypeConfigId).Single();
var isCurValueExist = curTypeConfig!.EsppSchType!.EsppSchTypeValues.Where(t => t.Id == item.TypeValueId).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;
//}
}
}