150 lines
6.0 KiB
C#
150 lines
6.0 KiB
C#
using Microsoft.Extensions.Logging;
|
|
using TwoFromTheCasketContracts.BusinessLogicsContracts;
|
|
using TwoFromTheCasketContracts.DataModels;
|
|
using TwoFromTheCasketContracts.Exceptions;
|
|
using TwoFromTheCasketContracts.StorageContracts;
|
|
|
|
namespace TwoFromTheCasketBusinessLogic.Implementation;
|
|
|
|
internal class WorkerComplitedWorkBusinessLogicContract(IWorkerComplitedWorkStorageContract _complitedWorkStorageContract,
|
|
ILogger _logger) : IWorkerComplitedWorkBusinessLogicContract
|
|
{
|
|
private IWorkerComplitedWorkStorageContract complitedWorkStorageContract = _complitedWorkStorageContract;
|
|
private ILogger logger = _logger;
|
|
|
|
public List<WorkerComplitedWorkDataModel> GetAllWorkerComplitedWorks()
|
|
{
|
|
logger.LogInformation("Fetching all worker completed works.");
|
|
|
|
var result = complitedWorkStorageContract.GetList();
|
|
if (result is null)
|
|
{
|
|
logger.LogError("No worker completed works found.");
|
|
throw new NullListException();
|
|
}
|
|
|
|
logger.LogInformation("Fetched {Count} worker completed works.", result.Count);
|
|
return result;
|
|
}
|
|
|
|
public List<WorkerComplitedWorkDataModel> GetWorkerComplitedWorksByWork(string workId)
|
|
{
|
|
logger.LogInformation("Fetching worker completed works by workId: {workId}", workId);
|
|
|
|
if (string.IsNullOrWhiteSpace(workId))
|
|
{
|
|
logger.LogError("Work ID is null or empty.");
|
|
throw new ArgumentNullException(nameof(workId));
|
|
}
|
|
|
|
if (!Guid.TryParse(workId, out _))
|
|
{
|
|
logger.LogError("Invalid work ID format: {workId}", workId);
|
|
throw new ValidationException("Invalid work ID format.");
|
|
}
|
|
|
|
var result = complitedWorkStorageContract.GetList(complitedWorkId: workId);
|
|
if (result is null)
|
|
{
|
|
logger.LogError("No completed works found for work ID: {workId}", workId);
|
|
throw new NullListException();
|
|
}
|
|
|
|
logger.LogInformation("Fetched {Count} worker completed works for workId: {workId}", result.Count, workId);
|
|
return result;
|
|
}
|
|
public List<WorkerComplitedWorkDataModel> GetWorkerComplitedWorksByWorker(string workerId)
|
|
{
|
|
logger.LogInformation("Fetching worker completed works by workerId: {workerId}", workerId);
|
|
|
|
if (string.IsNullOrWhiteSpace(workerId))
|
|
{
|
|
logger.LogError("Worker ID is null or empty.");
|
|
throw new ArgumentNullException(nameof(workerId));
|
|
}
|
|
|
|
if (!Guid.TryParse(workerId, out _))
|
|
{
|
|
logger.LogError("Invalid worker ID format: {workerId}", workerId);
|
|
throw new ValidationException("Invalid worker ID format.");
|
|
}
|
|
|
|
var result = complitedWorkStorageContract.GetList(workerId: workerId);
|
|
if (result is null)
|
|
{
|
|
logger.LogError("No completed works found for worker ID: {workerId}", workerId);
|
|
throw new NullListException();
|
|
}
|
|
|
|
logger.LogInformation("Fetched {Count} worker completed works for workerId: {workerId}", result.Count, workerId);
|
|
return result;
|
|
}
|
|
|
|
public void InsertWorkerComplitedWork(WorkerComplitedWorkDataModel workerComplitedWork)
|
|
{
|
|
logger.LogInformation("Inserting new worker completed work: {workerComplitedWork}", workerComplitedWork);
|
|
|
|
if (workerComplitedWork is null)
|
|
{
|
|
logger.LogError("Worker completed work data is null.");
|
|
throw new ArgumentNullException(nameof(workerComplitedWork));
|
|
}
|
|
|
|
if (!Guid.TryParse(workerComplitedWork.WorkerId, out _))
|
|
throw new ValidationException("Invalid worker ID format.");
|
|
|
|
if (!Guid.TryParse(workerComplitedWork.ComplitedWorkId, out _))
|
|
throw new ValidationException("Invalid completed work ID format.");
|
|
|
|
if (workerComplitedWork.NumberOfWorkingHours <= 0)
|
|
throw new ValidationException("Number of working hours must be greater than zero.");
|
|
|
|
complitedWorkStorageContract.AddElement(workerComplitedWork);
|
|
_logger.LogInformation("Worker completed work inserted successfully: {workerComplitedWork}", workerComplitedWork);
|
|
}
|
|
|
|
public void UpdateWorkerComplitedWork(WorkerComplitedWorkDataModel workerComplitedWork)
|
|
{
|
|
logger.LogInformation("Updating worker completed work: {workerComplitedWork}", workerComplitedWork);
|
|
|
|
if (workerComplitedWork is null)
|
|
{
|
|
logger.LogError("Worker completed work data is null.");
|
|
throw new ArgumentNullException(nameof(workerComplitedWork));
|
|
}
|
|
|
|
if (!Guid.TryParse(workerComplitedWork.WorkerId, out _))
|
|
throw new ValidationException("Invalid worker ID format.");
|
|
|
|
if (!Guid.TryParse(workerComplitedWork.ComplitedWorkId, out _))
|
|
throw new ValidationException("Invalid completed work ID format.");
|
|
|
|
if (workerComplitedWork.NumberOfWorkingHours <= 0)
|
|
throw new ValidationException("Number of working hours must be greater than zero.");
|
|
|
|
complitedWorkStorageContract.UpdElement(workerComplitedWork);
|
|
logger.LogInformation("Worker completed work updated successfully: {workerComplitedWork}", workerComplitedWork);
|
|
}
|
|
|
|
public void DeleteWorkerComplitedWork(string workerComplitedWorkId)
|
|
{
|
|
logger.LogInformation("Deleting worker completed work: {workerComplitedWorkId}", workerComplitedWorkId);
|
|
|
|
if (string.IsNullOrWhiteSpace(workerComplitedWorkId))
|
|
{
|
|
logger.LogError("Worker completed work ID is null or empty.");
|
|
throw new ArgumentNullException(nameof(workerComplitedWorkId));
|
|
}
|
|
|
|
if (!Guid.TryParse(workerComplitedWorkId, out _))
|
|
{
|
|
logger.LogError("Invalid worker completed work ID format: {workerComplitedWorkId}", workerComplitedWorkId);
|
|
throw new ValidationException("Invalid worker completed work ID format.");
|
|
}
|
|
|
|
complitedWorkStorageContract.DelElement(workerComplitedWorkId);
|
|
logger.LogInformation("Worker completed work deleted successfully: {workerComplitedWorkId}", workerComplitedWorkId);
|
|
}
|
|
|
|
}
|