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

98 lines
5.3 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.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 && t.Schedule.TypeScheduleId > 0).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)
{
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;
}
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;
}
}
}