feat(api): JobControl переписан метод Create+Валидация

This commit is contained in:
Mikhail Kuznetsov
2025-09-11 16:03:51 +10:00
parent 76c289cef2
commit 39eb8ddc2c
10 changed files with 295 additions and 80 deletions

View File

@@ -0,0 +1,123 @@
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;
}
}
}

View File

@@ -1,48 +0,0 @@
using FluentValidation;
using PARR.API.Contracts.V1.Requests;
using PARR.DAL.Services.Interfaces;
using PARR.DAL.Services.Interfaces.Job;
namespace PARR.API.Validators
{
public class JobValidator : AbstractValidator<JobRequest>
{
private readonly ITnkService tnkService;
private readonly IJobGroupService jobGroupService;
public JobValidator(
ITnkService tnkService,
IJobGroupService jobGroupService
)
{
this.tnkService = tnkService;
this.jobGroupService = jobGroupService;
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 ТНК");
RuleFor(t => t.GroupId)
.MustAsync(async (entity, value, c) => await IsGroupExist(entity))
.WithMessage("Указан несуществующий Id группы работ");
}
private async Task<bool> IsGroupExist(JobRequest entity)
{
return await jobGroupService.GetAsync(entity.GroupId) != null;
}
private async Task<bool> IsTnkExist(JobRequest entity)
{
return await tnkService.GetAsync(entity.TnkId) != null;
}
}
}