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

140 lines
5.9 KiB
C#

using System.Data.SqlClient;
using Serilog;
using LCU.Library.Internal.DocumentIngestorModels.Entities;
using LCU.Library.Internal.Exceptions;
using LCU.API.Internal.DocumentIngestorLib.Repositories;
namespace LCU.API.Internal.DocumentIngestor.Services;
public class PdfPartitionerRunInfoService : IPdfPartitionerRunInfoService
{
private readonly IPdfPartitionerRunInfoRepository _pdfPartitionerRunInfoRepository;
private readonly IUnitOfWork _unitOfWork;
public PdfPartitionerRunInfoService(IUnitOfWork unitOfWork,
IPdfPartitionerRunInfoRepository pdfPartitionerRunInfoRepository)
{
_pdfPartitionerRunInfoRepository = pdfPartitionerRunInfoRepository;
_unitOfWork = unitOfWork;
}
public async Task<PdfPartitionerRunInfo> GetByIdAsync(Guid pdfPartitionerRunInfoId)
{
try
{
var pdfPartitionerRunInfo = await _pdfPartitionerRunInfoRepository.FindByIdAsync(pdfPartitionerRunInfoId);
return pdfPartitionerRunInfo;
}
catch (Exception exception)
{
Log.Error(exception, $"Failure retrieving PdfPartitionerRunInfo with PdfPartitionerRunInfoId {pdfPartitionerRunInfoId}.");
throw new ServiceException($"Failure retrieving PdfPartitionerRunInf with PdfPartitionerRunInfoId {pdfPartitionerRunInfoId}.");
}
}
public async Task<PdfPartitionerRunInfo> SaveAsync(PdfPartitionerRunInfo pdfPartitionerRunInfo)
{
try
{
var result = await _pdfPartitionerRunInfoRepository.AddAsync(pdfPartitionerRunInfo);
await _unitOfWork.CompleteAsync();
return result;
}
catch (Exception exception)
{
Log.Error(exception, $"Failure saving PdfPartitionerRunInfo.");
throw new ServiceException($"Failure saving PdfPartitionerRunInfo.");
}
}
public async Task<PdfPartitionerRunInfo> IncreaseCountsAndUpdate(Guid pdfPartitionerRunInfoId, bool successful)
{
try
{
var existing = await GetByIdAsync(pdfPartitionerRunInfoId);
ArgumentNullException.ThrowIfNull(existing);
if (successful)
{
existing.SuccessCount++;
}
else
{
existing.FailureCount++;
}
if (existing.PdfPartitionerCompleted)
{
existing.EndTimeStamp = DateTime.Now;
}
_pdfPartitionerRunInfoRepository.Update(existing);
_unitOfWork.Complete();
return existing;
}
catch (SqlException sqlException)
{
Log.Error(sqlException, $"SQL Failure IncreaseCountsAndUpdate PdfPartitionerRunInfo. PdfPartitionerRunInfoId: {pdfPartitionerRunInfoId}");
throw new ServiceException($"SQL Failure IncreaseCountsAndUpdate PdfPartitionerRunInfo. PdfPartitionerRunInfoId: {pdfPartitionerRunInfoId}");
}
catch (Exception exception)
{
Log.Error(exception, $"Failure updating IncreaseCountsAndUpdate. PdfPartitionerRunInfoId: {pdfPartitionerRunInfoId}");
throw new ServiceException($"Failure updating IncreaseCountsAndUpdate. PdfPartitionerRunInfoId: {pdfPartitionerRunInfoId}");
}
}
public async Task<PdfPartitionerRunInfo> Update(PdfPartitionerRunInfo pdfPartitionerRunInfo)
{
try
{
var existing = await GetByIdAsync(pdfPartitionerRunInfo.PdfPartitionerRunInfoId);
ArgumentNullException.ThrowIfNull(existing);
existing.StartTimeStamp = pdfPartitionerRunInfo.StartTimeStamp;
existing.EndTimeStamp = pdfPartitionerRunInfo.EndTimeStamp;
existing.PdfParserType = pdfPartitionerRunInfo.PdfParserType;
existing.Path = pdfPartitionerRunInfo.Path;
existing.IndexTotal = pdfPartitionerRunInfo.IndexTotal;
existing.FailureCount = pdfPartitionerRunInfo.FailureCount;
existing.SuccessCount = pdfPartitionerRunInfo.SuccessCount;
_pdfPartitionerRunInfoRepository.Update(existing);
_unitOfWork.Complete();
return existing;
}
catch (SqlException sqlException)
{
Log.Error(sqlException, $"SQL Failure updating PdfPartitionerRunInfo. PdfPartitionerRunInfoId: {pdfPartitionerRunInfo.PdfPartitionerRunInfoId}");
throw new ServiceException($"SQL Failure updating PdfPartitionerRunInfo. PdfPartitionerRunInfoId: {pdfPartitionerRunInfo.PdfPartitionerRunInfoId}");
}
catch (Exception exception)
{
Log.Error(exception, $"Failure updating PdfPartitionerRunInfo. PdfPartitionerRunInfoId: {pdfPartitionerRunInfo.PdfPartitionerRunInfoId}");
throw new ServiceException($"Failure updating PdfPartitionerRunInfo. PdfPartitionerRunInfoId: {pdfPartitionerRunInfo.PdfPartitionerRunInfoId}");
}
}
public async Task Delete(Guid pdfPartitionerRunInfoId)
{
try
{
var pdfPartitionerRunInfo = await _pdfPartitionerRunInfoRepository.FindByIdAsync(pdfPartitionerRunInfoId);
ArgumentNullException.ThrowIfNull(pdfPartitionerRunInfo);
_pdfPartitionerRunInfoRepository.Remove(pdfPartitionerRunInfo);
_unitOfWork.Complete();
}
catch (SqlException sqlException)
{
Log.Error(sqlException, $"SQL failure removing PdfPartitionerRunInfo. PdfPartitionerRunInfoId: {pdfPartitionerRunInfoId}");
throw new ServiceException($"SQL failure removing PdfPartitionerRunInfo. PdfPartitionerRunInfoId: {pdfPartitionerRunInfoId}");
}
catch (Exception exception)
{
Log.Error(exception, $"Failure removing PdfPartitionerRunInfo. PdfPartitionerRunInfoId: {pdfPartitionerRunInfoId}");
throw new ServiceException($"Failure removing PdfPartitionerRunInfo. PdfPartitionerRunInfoId: {pdfPartitionerRunInfoId}");
}
}
}