Adding
This commit is contained in:
+150
@@ -0,0 +1,150 @@
|
||||
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<WorkItemExecution> 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<WorkItemExecution> UpdateCompleteWorkItemExecution(WorkItemExecution workItemExecution, StatusType finalStatus)
|
||||
{
|
||||
workItemExecution = WorkItemExecution.SetAsFinal(workItemExecution, finalStatus);
|
||||
var updatedFailedWorkItem = await UpdateAsync(workItemExecution);
|
||||
return updatedFailedWorkItem;
|
||||
}
|
||||
|
||||
public async Task<WorkItemExecution> UpdateCompleteWorkItemExecutionWithoutSave(WorkItemExecution workItemExecution, StatusType finalStatus)
|
||||
{
|
||||
workItemExecution = WorkItemExecution.SetAsFinal(workItemExecution, finalStatus);
|
||||
var updatedFailedWorkItem = await UpdateAsyncWithoutSave(workItemExecution);
|
||||
return updatedFailedWorkItem;
|
||||
}
|
||||
|
||||
public async Task<WorkItemExecution> UpdateProcessingWorkItemExecution(WorkItemExecution workItemExecution)
|
||||
{
|
||||
workItemExecution.Status = StatusType.Processing;
|
||||
return await UpdateAsync(workItemExecution);
|
||||
}
|
||||
public async Task<WorkItemExecution> UpdateFailedWorkItemExecution(WorkItemExecution workItemExecution)
|
||||
{
|
||||
workItemExecution.Status = StatusType.Failed;
|
||||
return await UpdateAsync(workItemExecution);
|
||||
}
|
||||
|
||||
public async Task<WorkItemExecution> 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<WorkItemExecution> 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<WorkItemExecution> CreateWorkItemExecution(int workItemId)
|
||||
{
|
||||
var newWorkItemExecution = new WorkItemExecution(workItemId);
|
||||
return await SaveAsync(newWorkItemExecution);
|
||||
}
|
||||
|
||||
public async Task<WorkItemExecution> 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<IEnumerable<WorkItemExecution>> 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<IEnumerable<WorkItemExecution>> 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}.");
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user