Files
Check/MagicCarpetProject/MagicCarpetWebApi/Adapters/EmployeeAdapter.cs
2025-03-25 18:04:21 +04:00

279 lines
11 KiB
C#

using AutoMapper;
using MagicCarpetContracts.AdapterContracts;
using MagicCarpetContracts.AdapterContracts.OperationResponses;
using MagicCarpetContracts.BindingModels;
using MagicCarpetContracts.BuisnessLogicContracts;
using MagicCarpetContracts.DataModels;
using MagicCarpetContracts.Exceptions;
using MagicCarpetContracts.ViewModels;
namespace MagicCarpetWebApi.Adapters;
public class EmployeeAdapter : IEmployeeAdapter
{
private readonly IEmployeeBusinessLogicContract _employeeBusinessLogicContract;
private readonly ILogger _logger;
private readonly Mapper _mapper;
public EmployeeAdapter(IEmployeeBusinessLogicContract employeeBusinessLogicContract, ILogger<EmployeeAdapter> logger)
{
_employeeBusinessLogicContract = employeeBusinessLogicContract;
_logger = logger;
var config = new MapperConfiguration(cfg =>
{
cfg.CreateMap<EmployeeBindingModel, EmployeeDataModel>();
cfg.CreateMap<EmployeeDataModel, EmployeeViewModel>();
});
_mapper = new Mapper(config);
}
public EmployeeOperationResponse GetList(bool includeDeleted)
{
try
{
return EmployeeOperationResponse.OK([.. _employeeBusinessLogicContract.GetAllEmployees(!includeDeleted).Select(x => _mapper.Map<EmployeeViewModel>(x))]);
}
catch (NullListException)
{
_logger.LogError("NullListException");
return EmployeeOperationResponse.NotFound("The list is not initialized");
}
catch (StorageException ex)
{
_logger.LogError(ex, "StorageException");
return EmployeeOperationResponse.InternalServerError($"Error while working with data storage: {ex.InnerException!.Message}");
}
catch (Exception ex)
{
_logger.LogError(ex, "Exception");
return EmployeeOperationResponse.InternalServerError(ex.Message);
}
}
public EmployeeOperationResponse GetPostList(string id, bool includeDeleted)
{
try
{
return EmployeeOperationResponse.OK([.. _employeeBusinessLogicContract.GetAllEmployeesByPost(id, !includeDeleted).Select(x => _mapper.Map<EmployeeViewModel>(x))]);
}
catch (ValidationException ex)
{
_logger.LogError(ex, "ValidationException");
return EmployeeOperationResponse.BadRequest($"Incorrect data transmitted: {ex.Message}");
}
catch (NullListException)
{
_logger.LogError("NullListException");
return EmployeeOperationResponse.NotFound("The list is not initialized");
}
catch (StorageException ex)
{
_logger.LogError(ex, "StorageException");
return EmployeeOperationResponse.InternalServerError($"Error while working with data storage: {ex.InnerException!.Message}");
}
catch (Exception ex)
{
_logger.LogError(ex, "Exception");
return EmployeeOperationResponse.InternalServerError(ex.Message);
}
}
public EmployeeOperationResponse GetListByBirthDate(DateTime fromDate, DateTime toDate, bool includeDeleted)
{
try
{
return EmployeeOperationResponse.OK([.. _employeeBusinessLogicContract.GetAllEmployeesByBirthDate(fromDate, toDate, !includeDeleted).Select(x => _mapper.Map<EmployeeViewModel>(x))]);
}
catch (IncorrectDatesException ex)
{
_logger.LogError(ex, "IncorrectDatesException");
return EmployeeOperationResponse.BadRequest($"Incorrect dates: {ex.Message}");
}
catch (NullListException)
{
_logger.LogError("NullListException");
return EmployeeOperationResponse.NotFound("The list is not initialized");
}
catch (StorageException ex)
{
_logger.LogError(ex, "StorageException");
return EmployeeOperationResponse.InternalServerError($"Error while working with data storage: {ex.InnerException!.Message}");
}
catch (Exception ex)
{
_logger.LogError(ex, "Exception");
return EmployeeOperationResponse.InternalServerError(ex.Message);
}
}
public EmployeeOperationResponse GetListByEmploymentDate(DateTime fromDate, DateTime toDate, bool includeDeleted)
{
try
{
return EmployeeOperationResponse.OK([.. _employeeBusinessLogicContract.GetAllEmployeesByEmploymentDate(fromDate, toDate, !includeDeleted).Select(_mapper.Map<EmployeeViewModel>)]);
}
catch (IncorrectDatesException ex)
{
_logger.LogError(ex, "IncorrectDatesException");
return EmployeeOperationResponse.BadRequest($"Incorrect dates: {ex.Message}");
}
catch (NullListException)
{
_logger.LogError("NullListException");
return EmployeeOperationResponse.NotFound("The list is not initialized");
}
catch (StorageException ex)
{
_logger.LogError(ex, "StorageException");
return EmployeeOperationResponse.InternalServerError($"Error while working with data storage: {ex.InnerException!.Message}");
}
catch (Exception ex)
{
_logger.LogError(ex, "Exception");
return EmployeeOperationResponse.InternalServerError(ex.Message);
}
}
public EmployeeOperationResponse GetElement(string data)
{
try
{
return EmployeeOperationResponse.OK(_mapper.Map<EmployeeViewModel>(_employeeBusinessLogicContract.GetEmployeeByData(data)));
}
catch (ArgumentNullException ex)
{
_logger.LogError(ex, "ArgumentNullException");
return EmployeeOperationResponse.BadRequest("Data is empty");
}
catch (ValidationException ex)
{
_logger.LogError(ex, "ValidationException");
return EmployeeOperationResponse.BadRequest($"Incorrect data transmitted: {ex.Message}");
}
catch (ElementNotFoundException ex)
{
_logger.LogError(ex, "ElementNotFoundException");
return EmployeeOperationResponse.NotFound($"Not found element by data {data}");
}
catch (StorageException ex)
{
_logger.LogError(ex, "StorageException");
return EmployeeOperationResponse.InternalServerError($"Error while working with data storage: {ex.InnerException!.Message}");
}
catch (Exception ex)
{
_logger.LogError(ex, "Exception");
return EmployeeOperationResponse.InternalServerError(ex.Message);
}
}
public EmployeeOperationResponse RegisterEmployee(EmployeeBindingModel employeeModel)
{
try
{
_employeeBusinessLogicContract.InsertEmployee(_mapper.Map<EmployeeDataModel>(employeeModel));
return EmployeeOperationResponse.NoContent();
}
catch (ArgumentNullException ex)
{
_logger.LogError(ex, "ArgumentNullException");
return EmployeeOperationResponse.BadRequest("Data is empty");
}
catch (ValidationException ex)
{
_logger.LogError(ex, "ValidationException");
return EmployeeOperationResponse.BadRequest($"Incorrect data transmitted: {ex.Message}");
}
catch (ElementExistsException ex)
{
_logger.LogError(ex, "ElementExistsException");
return EmployeeOperationResponse.BadRequest(ex.Message);
}
catch (StorageException ex)
{
_logger.LogError(ex, "StorageException");
return EmployeeOperationResponse.BadRequest($"Error while working with data storage: {ex.InnerException!.Message}");
}
catch (Exception ex)
{
_logger.LogError(ex, "Exception");
return EmployeeOperationResponse.InternalServerError(ex.Message);
}
}
public EmployeeOperationResponse ChangeEmployeeInfo(EmployeeBindingModel employeeModel)
{
try
{
_employeeBusinessLogicContract.UpdateEmployee(_mapper.Map<EmployeeDataModel>(employeeModel));
return EmployeeOperationResponse.NoContent();
}
catch (ArgumentNullException ex)
{
_logger.LogError(ex, "ArgumentNullException");
return EmployeeOperationResponse.BadRequest("Data is empty");
}
catch (ValidationException ex)
{
_logger.LogError(ex, "ValidationException");
return EmployeeOperationResponse.BadRequest($"Incorrect data transmitted: {ex.Message}");
}
catch (ElementNotFoundException ex)
{
_logger.LogError(ex, "ElementNotFoundException");
return EmployeeOperationResponse.BadRequest($"Not found element by Id {employeeModel.Id}");
}
catch (ElementExistsException ex)
{
_logger.LogError(ex, "ElementExistsException");
return EmployeeOperationResponse.BadRequest(ex.Message);
}
catch (StorageException ex)
{
_logger.LogError(ex, "StorageException");
return EmployeeOperationResponse.BadRequest($"Error while working with data storage: {ex.InnerException!.Message}");
}
catch (Exception ex)
{
_logger.LogError(ex, "Exception");
return EmployeeOperationResponse.InternalServerError(ex.Message);
}
}
public EmployeeOperationResponse RemoveEmployee(string id)
{
try
{
_employeeBusinessLogicContract.DeleteEmployee(id);
return EmployeeOperationResponse.NoContent();
}
catch (ArgumentNullException ex)
{
_logger.LogError(ex, "ArgumentNullException");
return EmployeeOperationResponse.BadRequest("Id is empty");
}
catch (ValidationException ex)
{
_logger.LogError(ex, "ValidationException");
return EmployeeOperationResponse.BadRequest($"Incorrect data transmitted: {ex.Message}");
}
catch (ElementNotFoundException ex)
{
_logger.LogError(ex, "ElementNotFoundException");
return EmployeeOperationResponse.BadRequest($"Not found element by id: {id}");
}
catch (StorageException ex)
{
_logger.LogError(ex, "StorageException");
return EmployeeOperationResponse.BadRequest($"Error while working with data storage: {ex.InnerException!.Message}");
}
catch (Exception ex)
{
_logger.LogError(ex, "Exception");
return EmployeeOperationResponse.InternalServerError(ex.Message);
}
}
}