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.Common.Domain; using PARR.Constants; using PARR.DAL.Cache.Services; using PARR.DAL.DomainServices.Interfaces; using System.Text.Json; namespace PARR.API.Controllers.V1 { public class SyncTaskController : BaseApiController { private readonly ILogger logger; private readonly IMqService mqService; private readonly IUriService uriService; private readonly MqSettings mqSettings; private readonly IClientService clientService; private readonly IMatchingStatusService matchingStatusService; public SyncTaskController( ILogger 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; } /// /// Отправить задание в Matcher /// [HttpPost(ApiRoutes.SyncTask.MatchTemplates)] public async Task 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 { 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 }); logger.LogDebug("Получен код отпрвки: {IsSuccess}", result.IsSuccess); if (!result.IsSuccess) return BadRequest(new Response(false, new List { new ErrorModel { Message = "Ошибка отправки сообщения в очередь" } })); //var createdUri = uriService.GetAllUri(ApiRoutes.SyncTask.MatchTemplatesForJob); return Created("", new Response(null, true, new List(), "Отправлен запрос на сопоставление шаблонов")); } } }