using System.Collections.Generic; using Microsoft.Data.SqlClient; using LCU.Library.Internal.DispatcherModels.Enums; using LCU.Api.Internal.DispatcherLib.Repositories; namespace LCU.Api.Internal.Dispatcher.Services; public class WorkItemExecutionService : IWorkItemExecutionService { private readonly IUnitOfWork _unitOfWork; private readonly IWorkItemExecutionRepository _workItemExecutionRepository; public WorkItemExecutionService(IUnitOfWork unitOfWork, IWorkItemExecutionRepository workItemExecutionRepository) { _unitOfWork = unitOfWork; _workItemExecutionRepository = workItemExecutionRepository; } public async Task GetByIdAsync(int id) { WorkItemExecution workItemExecution = null; try { workItemExecution = await _workItemExecutionRepository.FindByIdAsync(id); ArgumentNullException.ThrowIfNull(workItemExecution); } catch (Exception exception) { Log.Error(exception, $"Failure retrieving dispatcher WorkItemExecution with id {id}."); throw new ServiceException($"Failure retrieving dispatcher WorkItemExecution with id {id}."); } return workItemExecution; } public async Task UpdateCompleteWorkItemExecution(WorkItemExecution workItemExecution, StatusType finalStatus) { workItemExecution = WorkItemExecution.SetAsFinal(workItemExecution, finalStatus); var updatedFailedWorkItem = await UpdateAsync(workItemExecution); return updatedFailedWorkItem; } public async Task UpdateCompleteWorkItemExecutionWithoutSave(WorkItemExecution workItemExecution, StatusType finalStatus) { workItemExecution = WorkItemExecution.SetAsFinal(workItemExecution, finalStatus); var updatedFailedWorkItem = await UpdateAsyncWithoutSave(workItemExecution); return updatedFailedWorkItem; } public async Task UpdateProcessingWorkItemExecution(WorkItemExecution workItemExecution) { workItemExecution.Status = StatusType.Processing; return await UpdateAsync(workItemExecution); } public async Task UpdateFailedWorkItemExecution(WorkItemExecution workItemExecution) { workItemExecution.Status = StatusType.Failed; return await UpdateAsync(workItemExecution); } public async Task UpdateAsync(WorkItemExecution workItemExecution) { try { var updatedWorkItemExecution = await UpdateAsyncWithoutSave(workItemExecution); await _unitOfWork.CompleteAsync(); return updatedWorkItemExecution; } catch (SqlException sqlException) { Log.Error(sqlException, $"SQL Failure updating dispatcher WorkItemExecution with id {workItemExecution.WorkItemId}"); throw new ServiceException($"SQL Failure updating dispatcher WorkItemExecution with id {workItemExecution.WorkItemId}"); } catch (Exception exception) { Log.Error(exception, $"Failure updating dispatcher WorkItemExecution with id {workItemExecution.WorkItemId}"); throw new ServiceException($"Failure updating dispatcher WorkItemExecution with id {workItemExecution.WorkItemId}"); } } public async Task UpdateAsyncWithoutSave(WorkItemExecution workItemExecution) { try { _workItemExecutionRepository.Update(workItemExecution); return workItemExecution; } catch (SqlException sqlException) { Log.Error(sqlException, $"SQL Failure updating dispatcher WorkItemExecution with id {workItemExecution.WorkItemId}"); throw new ServiceException($"SQL Failure updating dispatcher WorkItemExecution with id {workItemExecution.WorkItemId}"); } catch (Exception exception) { Log.Error(exception, $"Failure updating dispatcher WorkItemExecution with id {workItemExecution.WorkItemId}"); throw new ServiceException($"Failure updating dispatcher WorkItemExecution with id {workItemExecution.WorkItemId}"); } } public async Task CreateWorkItemExecution(int workItemId) { var newWorkItemExecution = new WorkItemExecution(workItemId); return await SaveAsync(newWorkItemExecution); } public async Task SaveAsync(WorkItemExecution workItemExecution) { try { var result = await _workItemExecutionRepository.AddAsync(workItemExecution); await _unitOfWork.CompleteAsync(); return result; } catch (Exception exception) { Log.Error(exception, "Failure saving dispatcher WorkItemExecution", workItemExecution); throw new ServiceException("Failure saving dispatcher WorkItemExecution"); } } public async Task> GetAllUnFinishedWorkItemExecutions(int workItemId) { try { var result = await _workItemExecutionRepository.FindUnFinishedWorkItemExecutions(workItemId); return result; } catch (Exception exception) { Log.Error(exception, $"Failure retrieving dispatcher WorkItemExecutions with id {workItemId}."); throw new ServiceException($"Failure retrieving dispatcher WorkItemExecutions with id {workItemId}."); } } public async Task> GetAllByWorkItemId(int workItemId) { try { var result = await _workItemExecutionRepository.FindAllByWorkItemId(workItemId); return result; } catch (Exception exception) { Log.Error(exception, $"Failure retrieving dispatcher WorkItemExecutions with WorkItemId {workItemId}."); throw new ServiceException($"Failure retrieving dispatcher WorkItemExecutions with WorkItemId {workItemId}."); } } }