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 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 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 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 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}"); } } }