123 lines
4.7 KiB
C#
123 lines
4.7 KiB
C#
|
|
using FluentValidation;
|
|||
|
|
using Microsoft.EntityFrameworkCore;
|
|||
|
|
using PARR.API.Contracts.V1.Requests;
|
|||
|
|
using PARR.DAL.Models.Job;
|
|||
|
|
using PARR.DAL.Services.Interfaces;
|
|||
|
|
using PARR.DAL.Services.Interfaces.Job;
|
|||
|
|
using PARR.DAL.Services.Interfaces.Unit;
|
|||
|
|
|
|||
|
|
namespace PARR.API.Validators
|
|||
|
|
{
|
|||
|
|
public class JobRequestValidator : AbstractValidator<JobRequest>
|
|||
|
|
{
|
|||
|
|
private readonly ITnkService tnkService;
|
|||
|
|
private readonly IJobGroupService jobGroupService;
|
|||
|
|
private readonly IJobService jobService;
|
|||
|
|
private readonly IUnitFieldService unitFieldService;
|
|||
|
|
private JobGroup? jobGroup;
|
|||
|
|
|
|||
|
|
public JobRequestValidator(
|
|||
|
|
ITnkService tnkService,
|
|||
|
|
IJobGroupService jobGroupService,
|
|||
|
|
IJobService jobService,
|
|||
|
|
IUnitFieldService unitFieldService
|
|||
|
|
)
|
|||
|
|
{
|
|||
|
|
this.tnkService = tnkService;
|
|||
|
|
this.jobGroupService = jobGroupService;
|
|||
|
|
this.jobService = jobService;
|
|||
|
|
this.unitFieldService = unitFieldService;
|
|||
|
|
RuleFor(t => t.Name)
|
|||
|
|
.NotNull().NotEmpty();
|
|||
|
|
|
|||
|
|
RuleFor(t => t.WorkName)
|
|||
|
|
.NotNull().NotEmpty();
|
|||
|
|
|
|||
|
|
RuleFor(t => t.TnkId)
|
|||
|
|
.MustAsync(async (entity, value, c) => await IsTnkExist(entity))
|
|||
|
|
.WithMessage("Указан несуществующий Id ТНК({PropertyValue})");
|
|||
|
|
|
|||
|
|
RuleFor(t => t.GroupId)
|
|||
|
|
.MustAsync(async (entity, value, c) => await IsGroupExist(entity))
|
|||
|
|
.WithMessage("Указан несуществующий Id группы работ({PropertyValue})");
|
|||
|
|
|
|||
|
|
RuleForEach(t => t.UnitFilters)
|
|||
|
|
.NotNull().NotEmpty();
|
|||
|
|
|
|||
|
|
RuleForEach(t => t.UnitFilters)
|
|||
|
|
.MustAsync(async (entity, value, c) => await IsUnitFiltersCorrect(entity))
|
|||
|
|
.WithMessage("Неверно заданы параметры фильтров. Внимательнее, пожалуйста!");
|
|||
|
|
|
|||
|
|
RuleFor(t => t.MinValueRelationships)
|
|||
|
|
.MustAsync(async (entity, value, c) => await IsMinValueRelationShipsExist(entity))
|
|||
|
|
.WithMessage("Работа с таким минимальным количеством связей уже существует");
|
|||
|
|
|
|||
|
|
RuleFor(t => t.MaxValueRelationships)
|
|||
|
|
.GreaterThanOrEqualTo(t => t.MinValueRelationships);
|
|||
|
|
|
|||
|
|
RuleFor(t => t.MaxValueRelationships)
|
|||
|
|
.MustAsync(async (entity, value, c) => await IsMaxValueRelationShipsExist(entity))
|
|||
|
|
.WithMessage("Работа с таким максимальным количеством связей уже существует");
|
|||
|
|
|
|||
|
|
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
private async Task<bool> IsUnitFiltersCorrect(JobRequest entity)
|
|||
|
|
{
|
|||
|
|
foreach (var unitFilter in entity.UnitFilters)
|
|||
|
|
{
|
|||
|
|
if (string.IsNullOrEmpty(unitFilter.UnitFilterMask))
|
|||
|
|
return false;
|
|||
|
|
|
|||
|
|
if (unitFilter.FieldFilters == null || !unitFilter.FieldFilters.Any())
|
|||
|
|
return false;
|
|||
|
|
|
|||
|
|
foreach (var fieldFilter in unitFilter.FieldFilters)
|
|||
|
|
{
|
|||
|
|
var fieldId = fieldFilter.FieldId;
|
|||
|
|
if (await unitFieldService.GetAsync(fieldId) == null)
|
|||
|
|
return false;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
if (unitFilter.RelationshipFilters != null || unitFilter.FieldFilters.Any())
|
|||
|
|
foreach (var relationshipFilter in unitFilter.RelationshipFilters!)
|
|||
|
|
{
|
|||
|
|
var fieldId = relationshipFilter.FieldId;
|
|||
|
|
if (await unitFieldService.GetAsync(fieldId) == null)
|
|||
|
|
return false;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
return true;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
private async Task<bool> IsGroupExist(JobRequest entity)
|
|||
|
|
{
|
|||
|
|
jobGroup = await jobGroupService.GetAsync(entity.GroupId);
|
|||
|
|
|
|||
|
|
return jobGroup != null;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
|
|||
|
|
private async Task<bool> IsTnkExist(JobRequest entity)
|
|||
|
|
{
|
|||
|
|
return await tnkService.GetAsync(entity.TnkId) != null;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
private async Task<bool> IsMinValueRelationShipsExist(JobRequest entity)
|
|||
|
|
{
|
|||
|
|
if (!jobGroup?.IsUmbrella == true)
|
|||
|
|
return true;
|
|||
|
|
|
|||
|
|
return await jobService.Get().CountAsync(t => t.GroupId == entity.GroupId && t.MinValueRelationships == entity.MinValueRelationships) == 0;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
private async Task<bool> IsMaxValueRelationShipsExist(JobRequest entity)
|
|||
|
|
{
|
|||
|
|
if (!jobGroup?.IsUmbrella == true)
|
|||
|
|
return true;
|
|||
|
|
|
|||
|
|
return await jobService.Get().CountAsync(t => t.GroupId == entity.GroupId && t.MaxValueRelationships == entity.MaxValueRelationships) == 0;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|