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

75 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.Implementations;
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>
/// Сопоставить текущие шаблоны для Job, создать недостущие шаблоны
/// </summary>
[HttpPost(ApiRoutes.SyncTask.MatchTemplatesForJob)]
public async Task<IActionResult> MatchTemplatesForJob([FromRoute] Guid jobId, [FromBody] MatchTemplatesForJobRequest request)
{
//24b3529a-dd43-444a-b10e-ac54fefd046a
var matchTemplateTask = new TemplateMatcherMq
{
Id = jobId,
EntityType = Constants.SyncTaskEntityTypeEnum.Job,
Action = request.Action,
Initiator = new HistoryInitiator
{
InitiatorIp = clientService.GetClientIp()?.ToString(),
InitiatorParrComponentId = ParrComponentsEnum.Api,
InitiatorComment = $"Отправлен запрос из GUI, action: {request.Action.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(createdUri, new Response<string?>(null, true, new List<ErrorModel>(), "Отправлен запрос на сопоставление шаблонов и Job"));
}
}
}