feat(api): ApplicationInWorkValidator добававлена валидация планировщика, ApplicationInWorkController добавлена обьязательная настройка планировщика
This commit is contained in:
@@ -28,16 +28,19 @@
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public class ScheduleRequest
|
public class ScheduleRequest
|
||||||
{
|
{
|
||||||
public DateTimeOffset NextRun { get; set; }
|
public DateTimeOffset NextRun { get; set; }
|
||||||
|
|
||||||
//public DateTimeOffset? LastRun { get; set; }
|
public List<EsppSchValueRequest> EsppSchValues { get; set; } = new List<EsppSchValueRequest>();
|
||||||
|
}
|
||||||
|
|
||||||
//public string Timezone { get; set; } = string.Empty;
|
|
||||||
|
|
||||||
public required int TypeScheduleId { get; set; }
|
public class EsppSchValueRequest
|
||||||
|
{
|
||||||
|
public Guid TypeValueId { get; set; }
|
||||||
|
|
||||||
public List<Guid> Values { get; set; } = new List<Guid>();
|
public Guid TypeConfigId { get; set; }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -63,7 +63,7 @@ namespace PARR.API.Controllers.V1
|
|||||||
|
|
||||||
if (existSameAiW != null)
|
if (existSameAiW != null)
|
||||||
return BadRequest(new Response(false, new List<ErrorModel> {
|
return BadRequest(new Response(false, new List<ErrorModel> {
|
||||||
new ErrorModel { Message = $"Уже существует задание на выполнение работ для программного обеспечения id(\"{request.ApplicationId}\") и работой id({request.WorkId})." } }
|
new ErrorModel { Message = $"Уже существует задание на выполнение работ для программного обеспечения id({request.ApplicationId}) и работой id({request.WorkId})." } }
|
||||||
));
|
));
|
||||||
|
|
||||||
var applicationInWork = new ApplicationsInWork
|
var applicationInWork = new ApplicationsInWork
|
||||||
@@ -82,7 +82,16 @@ namespace PARR.API.Controllers.V1
|
|||||||
AgentScript = request.AgentScript
|
AgentScript = request.AgentScript
|
||||||
};
|
};
|
||||||
|
|
||||||
//TODO Schedule
|
//Добавляем настройки планировщика
|
||||||
|
foreach (var esppSchValue in request.Schedule.EsppSchValues)
|
||||||
|
{
|
||||||
|
applicationInWork.EsppSchValues.Add(new EsppSchValue
|
||||||
|
{
|
||||||
|
ApplicationsInWorkId = applicationInWork.Id,
|
||||||
|
TypeConfigId = esppSchValue.TypeConfigId,
|
||||||
|
TypeValueId = esppSchValue.TypeValueId
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
if (!await applicationsInWorkService.CreateAsync(applicationInWork) || !await applicationsInWorkService.CommitAsync())
|
if (!await applicationsInWorkService.CreateAsync(applicationInWork) || !await applicationsInWorkService.CommitAsync())
|
||||||
return BadRequest(new Response(false, new List<ErrorModel> { new ErrorModel { Message = "Ошибка при записи нового задания на выполнение работ в базу данных" } }));
|
return BadRequest(new Response(false, new List<ErrorModel> { new ErrorModel { Message = "Ошибка при записи нового задания на выполнение работ в базу данных" } }));
|
||||||
|
|||||||
@@ -19,6 +19,8 @@ namespace PARR.API.Validators
|
|||||||
this.applicationsInWorkService = applicationsInWorkService;
|
this.applicationsInWorkService = applicationsInWorkService;
|
||||||
this.workService = workService;
|
this.workService = workService;
|
||||||
this.esppSchTypeConfigService = esppSchTypeConfigService;
|
this.esppSchTypeConfigService = esppSchTypeConfigService;
|
||||||
|
|
||||||
|
|
||||||
RuleFor(t => t.TemplateDuration)
|
RuleFor(t => t.TemplateDuration)
|
||||||
.NotNull().NotEmpty().WithMessage("Длительность данного задания на выполнение работ не может быть пустым")
|
.NotNull().NotEmpty().WithMessage("Длительность данного задания на выполнение работ не может быть пустым")
|
||||||
.Matches("^\\d{1,2}\\s\\d{1,2}[:]\\d{1,2}[:]\\d{1,2}$")
|
.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).NotNull().NotEmpty().WithMessage("Настройки планировщика задания на выполнение работ не могут быть пустыми");
|
||||||
|
|
||||||
RuleFor(t => t.Schedule.TypeScheduleId)
|
RuleFor(t => t.Schedule.EsppSchValues)
|
||||||
.NotNull().NotEmpty().WithMessage($"Тип планировщика задания на выполнение работ не может быть пустым")
|
.NotNull().NotEmpty().When(t => t.Schedule != null).WithMessage("Настройки планировщика задания на выполнение работ не могут быть пустыми")
|
||||||
.Must((entity, value, c) => IsTypeScheduleIdExist(entity))
|
.Must((entity, value, c) => IsEsppSchValuesExist(entity)).WithMessage("Не удалось найти подходящую конфигруцию планировщика задания на выполнение работ");
|
||||||
.When(t => t.Schedule != null).WithMessage($"Не удалось найти указанный тип планировщика задания на выполнение работ");
|
|
||||||
|
|
||||||
RuleFor(t => t.Schedule.Values)
|
//RuleFor(t => t.Schedule.Values)
|
||||||
.NotNull().NotEmpty().WithMessage($"Настройки значений планировщика задания на выполнение работ не могут быть пустыми")
|
// .NotNull().NotEmpty().WithMessage($"Настройки значений планировщика задания на выполнение работ не могут быть пустыми")
|
||||||
.Must((entity, value, c) => IsScheduleTypeValueIdExist(entity))
|
// .Must((entity, value, c) => IsScheduleTypeValueIdExist(entity))
|
||||||
.When(t => t.Schedule != null && t.Schedule.TypeScheduleId > 0).WithMessage("Не удалось найти подходящую конфигруцию планировщика задания на выполнение работ");
|
// .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)
|
private bool IsEsppSchValuesExist(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 configs = esppSchTypeConfigService.GetWithSchIncludes().ToList();
|
||||||
|
|
||||||
var mustValues = configs.Where(t => t.TypeScheduleId == request.Schedule.TypeScheduleId).ToList();
|
//Проверяем полученные Id конфигураций и Id конфигураций в базе
|
||||||
var values = request.Schedule.Values;
|
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)
|
var isCurValueExist = curTypeConfig!.EsppSchType!.EsppSchTypeValues.Where(t => t.Id == item.TypeValueId).Any();
|
||||||
return false;
|
|
||||||
|
|
||||||
if (!mlValues.Intersect(values).Any())
|
if (!isCurValueExist)
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
private async Task<bool> IsWorkExist(ApplicationInWorkRequest request)
|
private async Task<bool> IsWorkExist(ApplicationInWorkRequest request)
|
||||||
{
|
{
|
||||||
return await workService.GetAsync(request.WorkId) != null;
|
return await workService.GetAsync(request.WorkId) != null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
private async Task<bool> IsApplicationExist(ApplicationInWorkRequest request)
|
private async Task<bool> IsApplicationExist(ApplicationInWorkRequest request)
|
||||||
{
|
{
|
||||||
return await workService.GetAsync(request.WorkId) != null;
|
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;
|
||||||
|
//}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,4 @@
|
|||||||
using Microsoft.EntityFrameworkCore;
|
using Microsoft.EntityFrameworkCore;
|
||||||
using System.ComponentModel.DataAnnotations;
|
|
||||||
using System.ComponentModel.DataAnnotations.Schema;
|
using System.ComponentModel.DataAnnotations.Schema;
|
||||||
|
|
||||||
namespace PARR.DAL.Models
|
namespace PARR.DAL.Models
|
||||||
|
|||||||
Reference in New Issue
Block a user