Files
monorepo/LCU.API.Internal.Dispatcher/LCU.Api.Internal.Dispatcher/Services/ExecutionMessageService.cs
T
2026-03-24 15:26:03 -05:00

90 lines
3.2 KiB
C#

using System.Collections.Generic;
using LCU.Api.Internal.DispatcherLib.Repositories;
namespace LCU.Api.Internal.Dispatcher.Services;
public class ExecutionMessageService : IExecutionMessageService
{
private readonly IUnitOfWork _unitOfWork;
private readonly IExecutionMessageRepository _executionMessageRepository;
public ExecutionMessageService(IUnitOfWork unitOfWork,
IExecutionMessageRepository executionMessageRepository)
{
_unitOfWork = unitOfWork;
_executionMessageRepository = executionMessageRepository;
}
public async Task SaveExecutionMessages(List<ExecutionMessage> executionMessageList)
{
try
{
if (executionMessageList == null || !executionMessageList.Any())
{
return;
}
foreach (var message in executionMessageList)
{
if (string.IsNullOrWhiteSpace(message.Message))
{
continue;
}
var savedExecutionMessage = await SaveAsync(message);
ArgumentNullException.ThrowIfNull(savedExecutionMessage);
}
}
catch (Exception exception)
{
Log.Error(exception, "During SaveExecutionMessages there was an error. ");
throw new ServiceException($"During SaveExecutionMessages there was an error. " +
"ExceptionMessage: {exception.Message}");
}
}
public async Task SaveExecutionMessages(int workItemExecutionId, List<string> messageList)
{
try
{
if (messageList == null || !messageList.Any())
{
return;
}
foreach (var message in messageList)
{
if (string.IsNullOrWhiteSpace(message))
{
continue;
}
var executionMessage = new ExecutionMessage(workItemExecutionId, message);
var savedExecutionMessage = await SaveAsync(executionMessage);
ArgumentNullException.ThrowIfNull(savedExecutionMessage);
}
}
catch (Exception exception)
{
Log.Error(exception, $"During SaveExecutionMessages there was an error. " +
$"WorkItemExecutionId: {workItemExecutionId}");
throw new ServiceException($"During SaveExecutionMessages there was an error. " +
$"WorkItemExecutionId: {workItemExecutionId}, " +
$"ExceptionMessage: {exception.Message}");
}
}
public async Task<ExecutionMessage> SaveAsync(ExecutionMessage workItemExecution)
{
try
{
var result = await _executionMessageRepository.AddAsync(workItemExecution);
await _unitOfWork.CompleteAsync();
return result;
}
catch (Exception exception)
{
Log.Error(exception, "Failure saving dispatcher Execution Messages", workItemExecution);
throw new ServiceException($"Failure saving dispatcher Execution Messages. ExceptionMessage: { exception.Message }");
}
}
}