Files
parr_api/PARR.API/Controllers/V1/SyncTaskController.cs

90 lines
4.2 KiB
C#

using Microsoft.AspNetCore.Mvc;
using PARR.API.Contracts.V1;
using PARR.API.Contracts.V1.Requests;
using PARR.API.Contracts.V1.Responses.Base;
using PARR.API.Controllers.V1.Base;
using PARR.API.Services.Interfaces;
using PARR.API.Settings;
using PARR.BLL.Domain.Mq;
using PARR.BLL.Services.Interfaces;
using PARR.DAL.DomainServices.Interfaces;
using PARR.Domain.Entities.Base.History;
using PARR.Domain.Enums;
namespace PARR.API.Controllers.V1
{
public class SyncTaskController : BaseApiController
{
private readonly ILogger<SyncTaskController> logger;
private readonly IMqService mqService;
private readonly IUriService uriService;
private readonly MqSettings mqSettings;
private readonly IClientService clientService;
private readonly IMatchingStatusService matchingStatusService;
public SyncTaskController(
ILogger<SyncTaskController> logger,
IMqService mqService,
IUriService uriService,
MqSettings mqSettings,
IClientService clientService,
IMatchingStatusService matchingStatusService
)
{
this.logger = logger;
this.mqService = mqService;
this.uriService = uriService;
this.mqSettings = mqSettings;
this.clientService = clientService;
this.matchingStatusService = matchingStatusService;
}
/// <summary>
/// Отправить задание в Matcher
/// </summary>
[HttpPost(ApiRoutes.SyncTask.MatchTemplates)]
public async Task<IActionResult> MatchTemplatesForJob([FromBody] MatchTemplatesRequest request)
{
//todo: Валидатор! Валидатор то забыли!!!
// проверяем, если уже идет синхронизация по этому объекту, то ахтунг, ошибка
var matchingStatus = await matchingStatusService.GetStatusAsync(request.ObjectId, request.EntityType);
if (matchingStatus.IsMatchingObject)
{
logger.LogWarning("Прекращена попытка повторного формирования задания для matcher. objectId: {objectId}, entityType: {entityType}, action: {action}", request.ObjectId, request.EntityType, request.Action);
return BadRequest(new Response(false, new List<ErrorModel> { new ErrorModel { Message = $"Уже выполняется задание синхронизации этого объекта" } }));
}
//24b3529a-dd43-444a-b10e-ac54fefd046a
var matchTemplateTask = new TemplateMatcherMq
{
Id = request.ObjectId,
// EntityType = Constants.SyncTaskEntityTypeEnum.Job,
EntityType = request.EntityType,
Action = request.Action,
Initiator = new HistoryInitiator
{
InitiatorIp = clientService.GetClientIp()?.ToString(),
InitiatorParrComponentId = ParrComponentsEnum.Api,
InitiatorComment = $"Отправлен запрос из GUI, action: {request.Action.ToString()}, entityType: {request.EntityType.ToString()}"
}
};
//var msg = JsonSerializer.Serialize(matchTemplateTask);
//logger.LogDebug("Подготовлено сообщение: {Message}", new[] { msg });
//var result = await mqService.SendAsync(mqSettings.TemplatesMatcher, new[] { msg });
var result = await mqService.SendAsync(mqSettings.TemplatesMatcher, new List<object> { matchTemplateTask });
logger.LogDebug("Получен код отпрвки: {IsSuccess}", result.IsSuccess);
if (!result.IsSuccess)
return BadRequest(new Response(false, new List<ErrorModel> { new ErrorModel { Message = "Ошибка отправки сообщения в очередь" } }));
//var createdUri = uriService.GetAllUri(ApiRoutes.SyncTask.MatchTemplatesForJob);
return Created("", new Response<string?>(null, true, new List<ErrorModel>(), "Отправлен запрос на сопоставление шаблонов"));
}
}
}