2024-03-27 11:13:52 +10:00
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 ;
2024-03-28 16:21:22 +10:00
private readonly IEsppSchTypeConfigService esppSchTypeConfigService ;
2024-03-27 11:13:52 +10:00
public ApplicationInWorkValidator (
IApplicationsInWorkService applicationsInWorkService ,
2024-03-28 16:21:22 +10:00
IWorkService workService ,
IEsppSchTypeConfigService esppSchTypeConfigService
2024-03-27 11:13:52 +10:00
)
{
this . applicationsInWorkService = applicationsInWorkService ;
this . workService = workService ;
2024-03-28 16:21:22 +10:00
this . esppSchTypeConfigService = esppSchTypeConfigService ;
2024-03-27 11:13:52 +10:00
RuleFor ( t = > t . TemplateDuration )
. NotNull ( ) . NotEmpty ( ) . WithMessage ( "Длительность данного задания на выполнение работ не может быть пустым" )
2024-03-28 16:21:22 +10:00
. Matches ( "^\\d{1,2}\\s\\d{1,2}[:]\\d{1,2}[:]\\d{1,2}$" )
2024-03-27 11:13:52 +10:00
. 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 программного обеспечения" ) ;
2024-03-28 16:21:22 +10:00
//Проверяем настройки планировщика
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 ;
2024-03-27 11:13:52 +10:00
}
2024-03-28 16:21:22 +10:00
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 ;
}
2024-03-27 11:13:52 +10:00
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 ;
}
}
}