DeactivateTemplates
This commit is contained in:
10
PARR.GeneratorTemplates/Services/ITemplateManager.cs
Normal file
10
PARR.GeneratorTemplates/Services/ITemplateManager.cs
Normal file
@@ -0,0 +1,10 @@
|
||||
using PARR.BLL.Domain.Mq;
|
||||
|
||||
namespace PARR.GeneratorTemplates.Services
|
||||
{
|
||||
internal interface ITemplateManager
|
||||
{
|
||||
Task CreateTemplates(GeneratorTemplateMq query);
|
||||
Task DeactivateTemplates(GeneratorTemplateMq query);
|
||||
}
|
||||
}
|
||||
13
PARR.GeneratorTemplates/Services/ITransformService.cs
Normal file
13
PARR.GeneratorTemplates/Services/ITransformService.cs
Normal file
@@ -0,0 +1,13 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace PARR.GeneratorTemplates.Services
|
||||
{
|
||||
internal interface ITransformService
|
||||
{
|
||||
T? GetModelFromJson<T>(string str);
|
||||
}
|
||||
}
|
||||
7
PARR.GeneratorTemplates/Services/IValidatorService.cs
Normal file
7
PARR.GeneratorTemplates/Services/IValidatorService.cs
Normal file
@@ -0,0 +1,7 @@
|
||||
namespace PARR.GeneratorTemplates.Services
|
||||
{
|
||||
internal interface IValidatorService
|
||||
{
|
||||
Task<bool> IsValidApplicationAndWorksAsync(Guid applicationId, Guid workId);
|
||||
}
|
||||
}
|
||||
71
PARR.GeneratorTemplates/Services/TemplateManager.cs
Normal file
71
PARR.GeneratorTemplates/Services/TemplateManager.cs
Normal file
@@ -0,0 +1,71 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using PARR.BLL.Domain.Mq;
|
||||
using PARR.DAL.Contracts;
|
||||
using PARR.DAL.Services.Interfaces;
|
||||
|
||||
namespace PARR.GeneratorTemplates.Services
|
||||
{
|
||||
internal class TemplateManager : ITemplateManager
|
||||
{
|
||||
private readonly ITemplateService templateService;
|
||||
private readonly ILogger<TemplateManager> logger;
|
||||
|
||||
public TemplateManager(
|
||||
ITemplateService templateService,
|
||||
ILogger<TemplateManager> logger
|
||||
)
|
||||
{
|
||||
this.templateService = templateService;
|
||||
this.logger = logger;
|
||||
}
|
||||
|
||||
public async Task CreateTemplates(GeneratorTemplateMq query)
|
||||
{
|
||||
//Согласно параметрам, находит все ЭК в таблице Hosts.
|
||||
//Далее проверяет есть ли такие шаблоны в таблице Templates, если нет, то создает.
|
||||
//Если такие шаблоны уже есть, ничего с ними не делает.
|
||||
|
||||
//TODO:
|
||||
}
|
||||
|
||||
public async Task DeactivateTemplates(GeneratorTemplateMq query)
|
||||
{
|
||||
//Ищет все активные шаблоны согласно параметрам в таблице Templates, и деактивирует их. Меняем на статус Updating
|
||||
|
||||
// делаем sql форматдля like из запроса.
|
||||
string pattern = query.Ek.Replace("*", "%").ToLower();
|
||||
|
||||
var existTemplates = await templateService.Get()
|
||||
.Include(t => t.Host)
|
||||
.Include(t => t.ApplicationsInWork)
|
||||
.Where(t =>
|
||||
t.IsActive == true
|
||||
&& t.ApplicationsInWork!.ApplicationId == query.ApplicationId
|
||||
&& t.ApplicationsInWork!.WorkId == query.WorkId
|
||||
&& EF.Functions.Like(t.Host!.Ek.ToLower(), pattern)
|
||||
)
|
||||
.ToListAsync();
|
||||
|
||||
if (!existTemplates.Any())
|
||||
{
|
||||
logger.LogInformation("Нет шаблонов для деактивации");
|
||||
return;
|
||||
}
|
||||
|
||||
existTemplates.ForEach(item =>
|
||||
{
|
||||
item.IsActive = false;
|
||||
item.StatusCode = (int)StatusTemplateEnum.Updating;
|
||||
item.DateModified = DateTimeOffset.UtcNow;
|
||||
});
|
||||
|
||||
if (await templateService.CommitAsync())
|
||||
existTemplates.ForEach(item => logger.LogInformation($"Деактивирован шаблон: {item.Name}"));
|
||||
else
|
||||
existTemplates.ForEach(item => logger.LogError($"Ошибка при дактивации шаблона: {item.Name}"));
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
29
PARR.GeneratorTemplates/Services/TransformService.cs
Normal file
29
PARR.GeneratorTemplates/Services/TransformService.cs
Normal file
@@ -0,0 +1,29 @@
|
||||
using Microsoft.Extensions.Logging;
|
||||
using System.Text.Json;
|
||||
|
||||
namespace PARR.GeneratorTemplates.Services
|
||||
{
|
||||
internal class TransformService : ITransformService
|
||||
{
|
||||
private readonly ILogger<TransformService> logger;
|
||||
|
||||
public TransformService(ILogger<TransformService> logger)
|
||||
{
|
||||
this.logger = logger;
|
||||
}
|
||||
|
||||
public T? GetModelFromJson<T>(string str)
|
||||
{
|
||||
try
|
||||
{
|
||||
return JsonSerializer.Deserialize<T>(str);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
logger.LogError(ex, $"Ошибка при конвертации строки в модель. {str}");
|
||||
|
||||
return default;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
34
PARR.GeneratorTemplates/Services/ValidatorService.cs
Normal file
34
PARR.GeneratorTemplates/Services/ValidatorService.cs
Normal file
@@ -0,0 +1,34 @@
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using PARR.DAL.Services.Interfaces;
|
||||
|
||||
namespace PARR.GeneratorTemplates.Services
|
||||
{
|
||||
internal class ValidatorService : IValidatorService
|
||||
{
|
||||
private readonly IServiceProvider serviceProvider;
|
||||
|
||||
public ValidatorService(IServiceProvider serviceProvider)
|
||||
{
|
||||
this.serviceProvider = serviceProvider;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Проверка, валидны ли поля и связана ли Работа с Приложением
|
||||
/// </summary>
|
||||
/// <param name="applicationId"></param>
|
||||
/// <param name="workId"></param>
|
||||
/// <returns></returns>
|
||||
public async Task<bool> IsValidApplicationAndWorksAsync(Guid applicationId, Guid workId)
|
||||
{
|
||||
using (var scope = serviceProvider.CreateScope())
|
||||
{
|
||||
var service = scope.ServiceProvider.GetService<IApplicationsInWorkService>();
|
||||
|
||||
if (service == null)
|
||||
throw new Exception($"Не найден сервис: {nameof(IApplicationsInWorkService)}");
|
||||
|
||||
return await service.Get(applicationId, workId) != null;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user