Files
monorepo/LCU.API.Internal.DocumentIngestor/LCU.API.Internal.DocumentIngestor/Services/PdfPartitionerLogService.cs
T
2026-03-25 14:50:13 -05:00

118 lines
4.5 KiB
C#

using System.Data.SqlClient;
using Serilog;
using LCU.API.Internal.DocumentIngestorLib.Repositories;
using LCU.Library.Internal.DocumentIngestorModels.Entities;
using LCU.Library.Internal.Exceptions;
namespace LCU.API.Internal.DocumentIngestor.Services;
public class PdfPartitionerLogService : IPdfPartitionerLogService
{
private readonly IPdfPartitionerLogRepository _pdfPartitionerLogRepository;
private readonly IUnitOfWork _unitOfWork;
public PdfPartitionerLogService(IUnitOfWork unitOfWork,
IPdfPartitionerLogRepository pdfPartitionerLogRepository)
{
_pdfPartitionerLogRepository = pdfPartitionerLogRepository;
_unitOfWork = unitOfWork;
}
public async Task<IEnumerable<PdfPartitionerLog>> GetAsync()
{
try
{
var pdfPartitionerLogs = await _pdfPartitionerLogRepository.FindAsync();
return pdfPartitionerLogs;
}
catch (Exception exception)
{
Log.Error(exception, $"Failure retrieving PdfPartitionerLogs.");
throw new ServiceException($"Failure retrieving PdfPartitionerLogs.");
}
}
public async Task<PdfPartitionerLog> GetByIdAsync(int id)
{
try
{
var pdfPartitionerLog = await _pdfPartitionerLogRepository.FindByIdAsync(id);
return pdfPartitionerLog;
}
catch (Exception exception)
{
Log.Error(exception, $"Failure retrieving PdfPartitionerLog with PdfPartitionerLogId {id}.");
throw new ServiceException($"Failure retrieving PdfPartitionerLog with PdfPartitionerLogId {id}.");
}
}
public async Task<IEnumerable<PdfPartitionerLog>> GetByPdfPartitionerRunInfoIdAsync(Guid pdfPartitionerRunInfoId)
{
try
{
var pdfPartitionerLogs = await _pdfPartitionerLogRepository.FindByPdfPartitionerRunInfoIdAsync(pdfPartitionerRunInfoId);
return pdfPartitionerLogs;
}
catch (Exception exception)
{
Log.Error(exception, $"Failure retrieving PdfPartitionerLogs with PdfPartitionerRunInfoId {pdfPartitionerRunInfoId}.");
throw new ServiceException($"Failure retrieving PdfPartitionerLogs with PdfPartitionerRunInfoId {pdfPartitionerRunInfoId}.");
}
}
public async Task SaveListAsync(Guid pdfPartitionerRunInfoId, List<string> messageLogs)
{
try
{
var pdfPartitionerLogs = new List<PdfPartitionerLog>();
foreach(var messageLog in messageLogs)
{
var newPdfPartitionerLog = new PdfPartitionerLog(pdfPartitionerRunInfoId, DateTime.Now, messageLog);
pdfPartitionerLogs.Add(await _pdfPartitionerLogRepository.AddAsync(newPdfPartitionerLog));
}
await _pdfPartitionerLogRepository.AddRangeAsync(pdfPartitionerLogs);
await _unitOfWork.CompleteAsync();
}
catch (Exception exception)
{
Log.Error(exception, $"Failure saving PdfPartitionerLog.");
throw new ServiceException($"Failure saving PdfPartitionerLog.");
}
}
public async Task<PdfPartitionerLog> SaveAsync(PdfPartitionerLog pdfPartitionerLog)
{
try
{
var result = await _pdfPartitionerLogRepository.AddAsync(pdfPartitionerLog);
await _unitOfWork.CompleteAsync();
return result;
}
catch (Exception exception)
{
Log.Error(exception, $"Failure saving PdfPartitionerLog.");
throw new ServiceException($"Failure saving PdfPartitionerLog.");
}
}
public async Task Delete(int id)
{
try
{
var pdfPartitionerLog = await GetByIdAsync(id);
ArgumentNullException.ThrowIfNull(pdfPartitionerLog);
_pdfPartitionerLogRepository.Remove(pdfPartitionerLog);
_unitOfWork.Complete();
}
catch (SqlException sqlException)
{
Log.Error(sqlException, $"SQL failure removing PdfPartitionerLog. PdfPartitionerLogId: {id}");
throw new ServiceException($"SQL failure removing PdfPartitionerLog. PdfPartitionerLogId: {id}");
}
catch (Exception exception)
{
Log.Error(exception, $"Failure removing PdfPartitionerLog. PdfPartitionerLogId: {id}");
throw new ServiceException($"Failure removing PdfPartitionerLog. PdfPartitionerLogId: {id}");
}
}
}