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

78 lines
3.1 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.Common.Domain;
using PARR.Constants;
using System.Text.Json;
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;
public SyncTaskController(
ILogger<SyncTaskController> logger,
IMqService mqService,
IUriService uriService,
MqSettings mqSettings,
IClientService clientService
)
{
this.logger = logger;
this.mqService = mqService;
this.uriService = uriService;
this.mqSettings = mqSettings;
this.clientService = clientService;
}
/// <summary>
/// Отправить задание в Matcher
/// </summary>
[HttpPost(ApiRoutes.SyncTask.MatchTemplates)]
public async Task<IActionResult> MatchTemplatesForJob([FromBody] MatchTemplatesRequest request)
{
//todo: Валидатор! Валидатор то забыли!!!
//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<ErrorModel> { new ErrorModel { Message = "Ошибка отправки сообщения в очередь" } }));
//var createdUri = uriService.GetAllUri(ApiRoutes.SyncTask.MatchTemplatesForJob);
return Created("", new Response<string?>(null, true, new List<ErrorModel>(), "Отправлен запрос на сопоставление шаблонов"));
}
}
}