2025-11-26 14:34:03 +10:00
|
|
|
|
using Microsoft.Extensions.DependencyInjection;
|
2024-07-25 17:23:08 +10:00
|
|
|
|
using Microsoft.Extensions.Logging;
|
2026-04-30 10:35:31 +10:00
|
|
|
|
using PARR.Core.Repositories.Base;
|
|
|
|
|
|
using PARR.Core.Repositories.Interfaces;
|
2026-07-03 11:17:48 +10:00
|
|
|
|
using PARR.Core.Repositories.Interfaces.JobGroupRepositories;
|
|
|
|
|
|
using PARR.Core.Repositories.Interfaces.JobRepositories;
|
2026-04-30 10:35:31 +10:00
|
|
|
|
using PARR.Domain.Entities;
|
2026-04-13 16:58:30 +10:00
|
|
|
|
using PARR.Domain.Entities.Base;
|
2026-07-03 11:17:48 +10:00
|
|
|
|
using PARR.Domain.Entities.JobEntities;
|
2026-06-15 16:56:23 +10:00
|
|
|
|
using PARR.Domain.Entities.JobGroupEntities;
|
2026-04-14 12:01:49 +10:00
|
|
|
|
using PARR.Domain.Enums;
|
2024-07-25 17:23:08 +10:00
|
|
|
|
|
|
|
|
|
|
namespace PARR.TemplateActivator;
|
2025-11-26 14:34:03 +10:00
|
|
|
|
|
2024-07-25 17:23:08 +10:00
|
|
|
|
internal class ValidatorService : IValidatorService
|
|
|
|
|
|
{
|
|
|
|
|
|
private readonly IServiceProvider serviceProvider;
|
|
|
|
|
|
private readonly ILogger<ValidatorService> logger;
|
|
|
|
|
|
|
|
|
|
|
|
public ValidatorService(IServiceProvider serviceProvider,
|
|
|
|
|
|
ILogger<ValidatorService> logger
|
|
|
|
|
|
)
|
|
|
|
|
|
{
|
|
|
|
|
|
this.serviceProvider = serviceProvider;
|
|
|
|
|
|
this.logger = logger;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-08-14 15:20:04 +10:00
|
|
|
|
|
2025-11-26 14:34:03 +10:00
|
|
|
|
public async Task<bool> IsValidObjectIdAsync(Guid id, SyncTaskEntityTypeEnum entityType)
|
2024-07-25 17:23:08 +10:00
|
|
|
|
{
|
2025-11-26 14:34:03 +10:00
|
|
|
|
switch (entityType)
|
2024-07-25 17:23:08 +10:00
|
|
|
|
{
|
2025-11-26 14:34:03 +10:00
|
|
|
|
case (SyncTaskEntityTypeEnum.JobGroup):
|
2026-04-30 10:35:31 +10:00
|
|
|
|
return await IsValidAync<IJobGroupRepository, JobGroup>(id);
|
2025-11-26 14:34:03 +10:00
|
|
|
|
|
|
|
|
|
|
case (SyncTaskEntityTypeEnum.Job):
|
2026-04-30 10:35:31 +10:00
|
|
|
|
return await IsValidAync<IJobRepository, Job>(id);
|
2025-11-26 14:34:03 +10:00
|
|
|
|
|
|
|
|
|
|
case (SyncTaskEntityTypeEnum.Template):
|
2026-04-30 10:35:31 +10:00
|
|
|
|
return await IsValidAync<ITemplateRepository, Template>(id);
|
2025-11-26 14:34:03 +10:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-07-25 17:23:08 +10:00
|
|
|
|
|
2025-11-26 14:34:03 +10:00
|
|
|
|
private async Task<bool> IsValidAync<TService, TEntity>(Guid id)
|
2026-04-30 10:35:31 +10:00
|
|
|
|
where TService : class, IBaseRepository<TEntity>
|
2026-04-13 16:58:30 +10:00
|
|
|
|
where TEntity : class, IBaseEntity
|
2025-11-26 14:34:03 +10:00
|
|
|
|
{
|
|
|
|
|
|
using (var scope = serviceProvider.CreateScope())
|
|
|
|
|
|
{
|
|
|
|
|
|
var service = scope.ServiceProvider.GetService<TService>();
|
2024-07-25 17:23:08 +10:00
|
|
|
|
if (service == null)
|
2025-11-26 14:34:03 +10:00
|
|
|
|
throw new Exception($"Не найден сервис: {typeof(TService).Name}");
|
2024-07-25 17:23:08 +10:00
|
|
|
|
|
2025-11-26 14:34:03 +10:00
|
|
|
|
var obj = await service.GetAsync(id);
|
2024-07-25 17:23:08 +10:00
|
|
|
|
|
2025-11-26 14:34:03 +10:00
|
|
|
|
if (obj == null)
|
2024-07-25 17:23:08 +10:00
|
|
|
|
{
|
2025-11-26 14:34:03 +10:00
|
|
|
|
logger.LogError($"Не объект {nameof(id)}: {id} в {typeof(TService).Name}");
|
2024-07-25 17:23:08 +10:00
|
|
|
|
return false;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2025-11-26 14:34:03 +10:00
|
|
|
|
|
2024-07-25 17:23:08 +10:00
|
|
|
|
}
|