Files
PIbd-22_Buslaev.R.V_TwoFrom…/TwoFromTheCasketContracts/TwoFromTheCasketBusinessLogic/Implementation/WorkerBusinessLogicContract.cs

166 lines
5.8 KiB
C#

using TwoFromTheCasketContracts.BusinessLogicsContracts;
using TwoFromTheCasketContracts.DataModels;
using TwoFromTheCasketContracts.Exceptions;
using TwoFromTheCasketContracts.StorageContracts;
using Microsoft.Extensions.Logging;
namespace TwoFromTheCasketBusinessLogic.Implementation;
internal class WorkerBusinessLogicContract(IWorkerStorageContract _workerStorageContract, ILogger _logger) : IWorkerBusinessLogicContract
{
private IWorkerStorageContract workerStorageContract = _workerStorageContract;
private ILogger logger = _logger;
public List<WorkerDataModel> GetAllWorkers(bool onlyActive = true)
{
logger.LogInformation("Retrieving all workers (onlyActive: {onlyActive})", onlyActive);
var workers = workerStorageContract.GetList(onlyActive, null, null, null, null, null)
?? throw new NullListException();
return workers;
}
public List<WorkerDataModel> GetWorkersByBirthDate(DateTime fromDate, DateTime toDate, bool onlyActive = true)
{
logger.LogInformation("Retrieving workers born between {fromDate} and {toDate}", fromDate, toDate);
if (fromDate >= toDate)
throw new IncorrectDatesException(fromDate, toDate);
var workers = workerStorageContract.GetList(onlyActive, null, fromDate, toDate, null, null)
?? throw new NullListException();
return workers;
}
public List<WorkerDataModel> GetWorkersByEmploymentDate(DateTime fromDate, DateTime toDate, bool onlyActive = true)
{
logger.LogInformation("Retrieving workers employed between {fromDate} and {toDate}", fromDate, toDate);
if (fromDate >= toDate)
throw new IncorrectDatesException(fromDate, toDate);
var workers = workerStorageContract.GetList(onlyActive, null, null, null, fromDate, toDate)
?? throw new NullListException();
return workers;
}
public List<WorkerDataModel> GetWorkersBySpecialization(string specializationId, bool onlyActive = true)
{
logger.LogInformation("Retrieving workers by specialization ID: {specializationId}", specializationId);
if (string.IsNullOrWhiteSpace(specializationId))
throw new ArgumentNullException(nameof(specializationId));
if (!Guid.TryParse(specializationId, out _))
throw new ValidationException("Specialization ID is not a valid GUID.");
var workers = workerStorageContract.GetList(onlyActive, specializationId, null, null, null, null)
?? throw new NullListException();
return workers;
}
public WorkerDataModel GetWorkerByData(string data)
{
logger.LogInformation("Retrieving worker by data: {data}", data);
if (string.IsNullOrWhiteSpace(data))
throw new ArgumentNullException(nameof(data));
WorkerDataModel? worker;
if (Guid.TryParse(data, out _))
{
worker = workerStorageContract.GetElementById(data);
}
else
{
worker = workerStorageContract.GetElementByFIO(data);
}
return worker ?? throw new ElementNotFoundException(data);
}
public void InsertWorker(WorkerDataModel workerDataModel)
{
if (workerDataModel == null)
throw new ArgumentNullException(nameof(workerDataModel));
logger.LogInformation("Inserting new worker: {workerId}", workerDataModel.Id);
if (!Guid.TryParse(workerDataModel.Id, out _))
throw new ValidationException("Worker ID is not a valid GUID.");
try
{
workerStorageContract.AddElement(workerDataModel);
logger.LogDebug("Worker {workerId} successfully inserted.", workerDataModel.Id);
}
catch (ElementExistsException ex)
{
logger.LogError(ex, "Worker with ID {workerId} already exists.", workerDataModel.Id);
throw;
}
catch (StorageException ex)
{
logger.LogError(ex, "Storage error occurred while inserting worker {workerId}.", workerDataModel.Id);
throw;
}
}
public void UpdateWorker(WorkerDataModel workerDataModel)
{
if (workerDataModel == null)
throw new ArgumentNullException(nameof(workerDataModel));
logger.LogInformation("Updating worker: {workerId}", workerDataModel.Id);
if (!Guid.TryParse(workerDataModel.Id, out _))
throw new ValidationException("Worker ID is not a valid GUID.");
try
{
workerStorageContract.UpdElement(workerDataModel);
logger.LogDebug("Worker {workerId} successfully updated.", workerDataModel.Id);
}
catch (ElementNotFoundException ex)
{
logger.LogError(ex, "Worker with ID {workerId} not found.", workerDataModel.Id);
throw;
}
catch (StorageException ex)
{
logger.LogError(ex, "Storage error occurred while updating worker {workerId}.", workerDataModel.Id);
throw;
}
}
public void DeleteWorker(string id)
{
logger.LogInformation("Deleting worker: {workerId}", id);
if (string.IsNullOrWhiteSpace(id))
throw new ArgumentNullException(nameof(id));
if (!Guid.TryParse(id, out _))
throw new ValidationException("Worker ID is not a valid GUID.");
try
{
workerStorageContract.DelElement(id);
logger.LogDebug("Worker {workerId} successfully deleted.", id);
}
catch (ElementNotFoundException ex)
{
logger.LogError(ex, "Worker with ID {workerId} not found.", id);
throw;
}
catch (StorageException ex)
{
logger.LogError(ex, "Storage error occurred while deleting worker {workerId}.", id);
throw;
}
}
}