278 lines
8.9 KiB
C#
278 lines
8.9 KiB
C#
using AutoMapper;
|
|
using CatHasPawsContratcs.AdapterContracts;
|
|
using CatHasPawsContratcs.AdapterContracts.OperationResponses;
|
|
using CatHasPawsContratcs.BindingModels;
|
|
using CatHasPawsContratcs.BusinessLogicsContracts;
|
|
using CatHasPawsContratcs.DataModels;
|
|
using CatHasPawsContratcs.Exceptions;
|
|
using CatHasPawsContratcs.ViewModels;
|
|
|
|
namespace CatHasPawsWebApi.Adapters;
|
|
|
|
public class WorkerAdapter : IWorkerAdapter
|
|
{
|
|
private readonly IWorkerBusinessLogicContract _buyerBusinessLogicContract;
|
|
|
|
private readonly ILogger _logger;
|
|
|
|
private readonly Mapper _mapper;
|
|
|
|
public WorkerAdapter(IWorkerBusinessLogicContract workerBusinessLogicContract, ILogger<WorkerAdapter> logger)
|
|
{
|
|
_buyerBusinessLogicContract = workerBusinessLogicContract;
|
|
_logger = logger;
|
|
var config = new MapperConfiguration(cfg =>
|
|
{
|
|
cfg.CreateMap<WorkerBindingModel, WorkerDataModel>();
|
|
cfg.CreateMap<WorkerDataModel, WorkerViewModel>();
|
|
});
|
|
_mapper = new Mapper(config);
|
|
}
|
|
|
|
public WorkerOperationResponse GetList(bool includeDeleted)
|
|
{
|
|
try
|
|
{
|
|
return WorkerOperationResponse.OK([.. _buyerBusinessLogicContract.GetAllWorkers(!includeDeleted).Select(x => _mapper.Map<WorkerViewModel>(x))]);
|
|
}
|
|
catch (NullListException)
|
|
{
|
|
_logger.LogError("NullListException");
|
|
return WorkerOperationResponse.NotFound("The list is not initialized");
|
|
}
|
|
catch (StorageException ex)
|
|
{
|
|
_logger.LogError(ex, "StorageException");
|
|
return WorkerOperationResponse.InternalServerError($"Error while working with data storage: {ex.InnerException!.Message}");
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_logger.LogError(ex, "Exception");
|
|
return WorkerOperationResponse.InternalServerError(ex.Message);
|
|
}
|
|
}
|
|
|
|
public WorkerOperationResponse GetPostList(string id, bool includeDeleted)
|
|
{
|
|
try
|
|
{
|
|
return WorkerOperationResponse.OK([.. _buyerBusinessLogicContract.GetAllWorkersByPost(id, !includeDeleted).Select(x => _mapper.Map<WorkerViewModel>(x))]);
|
|
}
|
|
catch (ValidationException ex)
|
|
{
|
|
_logger.LogError(ex, "ValidationException");
|
|
return WorkerOperationResponse.BadRequest($"Incorrect data transmitted: {ex.Message}");
|
|
}
|
|
catch (NullListException)
|
|
{
|
|
_logger.LogError("NullListException");
|
|
return WorkerOperationResponse.NotFound("The list is not initialized");
|
|
}
|
|
catch (StorageException ex)
|
|
{
|
|
_logger.LogError(ex, "StorageException");
|
|
return WorkerOperationResponse.InternalServerError($"Error while working with data storage: {ex.InnerException!.Message}");
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_logger.LogError(ex, "Exception");
|
|
return WorkerOperationResponse.InternalServerError(ex.Message);
|
|
}
|
|
}
|
|
|
|
public WorkerOperationResponse GetListByBirthDate(DateTime fromDate, DateTime toDate, bool includeDeleted)
|
|
{
|
|
try
|
|
{
|
|
return WorkerOperationResponse.OK([.. _buyerBusinessLogicContract.GetAllWorkersByBirthDate(fromDate.ToUniversalTime(), toDate.ToUniversalTime(), !includeDeleted).Select(x => _mapper.Map<WorkerViewModel>(x))]);
|
|
}
|
|
catch (IncorrectDatesException ex)
|
|
{
|
|
_logger.LogError(ex, "IncorrectDatesException");
|
|
return WorkerOperationResponse.BadRequest($"Incorrect dates: {ex.Message}");
|
|
}
|
|
catch (NullListException)
|
|
{
|
|
_logger.LogError("NullListException");
|
|
return WorkerOperationResponse.NotFound("The list is not initialized");
|
|
}
|
|
catch (StorageException ex)
|
|
{
|
|
_logger.LogError(ex, "StorageException");
|
|
return WorkerOperationResponse.InternalServerError($"Error while working with data storage: {ex.InnerException!.Message}");
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_logger.LogError(ex, "Exception");
|
|
return WorkerOperationResponse.InternalServerError(ex.Message);
|
|
}
|
|
}
|
|
|
|
public WorkerOperationResponse GetListByEmploymentDate(DateTime fromDate, DateTime toDate, bool includeDeleted)
|
|
{
|
|
try
|
|
{
|
|
return WorkerOperationResponse.OK([.. _buyerBusinessLogicContract.GetAllWorkersByEmploymentDate(fromDate.ToUniversalTime(), toDate.ToUniversalTime(), !includeDeleted).Select(x => _mapper.Map<WorkerViewModel>(x))]);
|
|
}
|
|
catch (IncorrectDatesException ex)
|
|
{
|
|
_logger.LogError(ex, "IncorrectDatesException");
|
|
return WorkerOperationResponse.BadRequest($"Incorrect dates: {ex.Message}");
|
|
}
|
|
catch (NullListException)
|
|
{
|
|
_logger.LogError("NullListException");
|
|
return WorkerOperationResponse.NotFound("The list is not initialized");
|
|
}
|
|
catch (StorageException ex)
|
|
{
|
|
_logger.LogError(ex, "StorageException");
|
|
return WorkerOperationResponse.InternalServerError($"Error while working with data storage: {ex.InnerException!.Message}");
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_logger.LogError(ex, "Exception");
|
|
return WorkerOperationResponse.InternalServerError(ex.Message);
|
|
}
|
|
}
|
|
|
|
public WorkerOperationResponse GetElement(string data)
|
|
{
|
|
try
|
|
{
|
|
return WorkerOperationResponse.OK(_mapper.Map<WorkerViewModel>(_buyerBusinessLogicContract.GetWorkerByData(data)));
|
|
}
|
|
catch (ArgumentNullException ex)
|
|
{
|
|
_logger.LogError(ex, "ArgumentNullException");
|
|
return WorkerOperationResponse.BadRequest("Data is empty");
|
|
}
|
|
catch (ValidationException ex)
|
|
{
|
|
_logger.LogError(ex, "ValidationException");
|
|
return WorkerOperationResponse.BadRequest($"Incorrect data transmitted: {ex.Message}");
|
|
}
|
|
catch (ElementNotFoundException ex)
|
|
{
|
|
_logger.LogError(ex, "ElementNotFoundException");
|
|
return WorkerOperationResponse.NotFound($"Not found element by data {data}");
|
|
}
|
|
catch (StorageException ex)
|
|
{
|
|
_logger.LogError(ex, "StorageException");
|
|
return WorkerOperationResponse.InternalServerError($"Error while working with data storage: {ex.InnerException!.Message}");
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_logger.LogError(ex, "Exception");
|
|
return WorkerOperationResponse.InternalServerError(ex.Message);
|
|
}
|
|
}
|
|
|
|
public WorkerOperationResponse RegisterWorker(WorkerBindingModel workerModel)
|
|
{
|
|
try
|
|
{
|
|
_buyerBusinessLogicContract.InsertWorker(_mapper.Map<WorkerDataModel>(workerModel));
|
|
return WorkerOperationResponse.NoContent();
|
|
}
|
|
catch (ArgumentNullException ex)
|
|
{
|
|
_logger.LogError(ex, "ArgumentNullException");
|
|
return WorkerOperationResponse.BadRequest("Data is empty");
|
|
}
|
|
catch (ValidationException ex)
|
|
{
|
|
_logger.LogError(ex, "ValidationException");
|
|
return WorkerOperationResponse.BadRequest($"Incorrect data transmitted: {ex.Message}");
|
|
}
|
|
catch (ElementExistsException ex)
|
|
{
|
|
_logger.LogError(ex, "ElementExistsException");
|
|
return WorkerOperationResponse.BadRequest(ex.Message);
|
|
}
|
|
catch (StorageException ex)
|
|
{
|
|
_logger.LogError(ex, "StorageException");
|
|
return WorkerOperationResponse.BadRequest($"Error while working with data storage: {ex.InnerException!.Message}");
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_logger.LogError(ex, "Exception");
|
|
return WorkerOperationResponse.InternalServerError(ex.Message);
|
|
}
|
|
}
|
|
|
|
public WorkerOperationResponse ChangeWorkerInfo(WorkerBindingModel workerModel)
|
|
{
|
|
try
|
|
{
|
|
_buyerBusinessLogicContract.UpdateWorker(_mapper.Map<WorkerDataModel>(workerModel));
|
|
return WorkerOperationResponse.NoContent();
|
|
}
|
|
catch (ArgumentNullException ex)
|
|
{
|
|
_logger.LogError(ex, "ArgumentNullException");
|
|
return WorkerOperationResponse.BadRequest("Data is empty");
|
|
}
|
|
catch (ValidationException ex)
|
|
{
|
|
_logger.LogError(ex, "ValidationException");
|
|
return WorkerOperationResponse.BadRequest($"Incorrect data transmitted: {ex.Message}");
|
|
}
|
|
catch (ElementNotFoundException ex)
|
|
{
|
|
_logger.LogError(ex, "ElementNotFoundException");
|
|
return WorkerOperationResponse.BadRequest($"Not found element by Id {workerModel.Id}");
|
|
}
|
|
catch (ElementExistsException ex)
|
|
{
|
|
_logger.LogError(ex, "ElementExistsException");
|
|
return WorkerOperationResponse.BadRequest(ex.Message);
|
|
}
|
|
catch (StorageException ex)
|
|
{
|
|
_logger.LogError(ex, "StorageException");
|
|
return WorkerOperationResponse.BadRequest($"Error while working with data storage: {ex.InnerException!.Message}");
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_logger.LogError(ex, "Exception");
|
|
return WorkerOperationResponse.InternalServerError(ex.Message);
|
|
}
|
|
}
|
|
|
|
public WorkerOperationResponse RemoveWorker(string id)
|
|
{
|
|
try
|
|
{
|
|
_buyerBusinessLogicContract.DeleteWorker(id);
|
|
return WorkerOperationResponse.NoContent();
|
|
}
|
|
catch (ArgumentNullException ex)
|
|
{
|
|
_logger.LogError(ex, "ArgumentNullException");
|
|
return WorkerOperationResponse.BadRequest("Id is empty");
|
|
}
|
|
catch (ValidationException ex)
|
|
{
|
|
_logger.LogError(ex, "ValidationException");
|
|
return WorkerOperationResponse.BadRequest($"Incorrect data transmitted: {ex.Message}");
|
|
}
|
|
catch (ElementNotFoundException ex)
|
|
{
|
|
_logger.LogError(ex, "ElementNotFoundException");
|
|
return WorkerOperationResponse.BadRequest($"Not found element by id: {id}");
|
|
}
|
|
catch (StorageException ex)
|
|
{
|
|
_logger.LogError(ex, "StorageException");
|
|
return WorkerOperationResponse.BadRequest($"Error while working with data storage: {ex.InnerException!.Message}");
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_logger.LogError(ex, "Exception");
|
|
return WorkerOperationResponse.InternalServerError(ex.Message);
|
|
}
|
|
}
|
|
} |