Files
Check/MagicCarpetProject/MagicCarpetWebApi/Adapters/AgencyAdapter.cs
2025-04-13 14:32:05 +04:00

191 lines
7.8 KiB
C#

using AutoMapper;
using MagicCarpetContracts.AdapterContracts;
using MagicCarpetContracts.AdapterContracts.OperationResponses;
using MagicCarpetContracts.BindingModels;
using MagicCarpetContracts.BusinessLogicContracts;
using MagicCarpetContracts.DataModels;
using MagicCarpetContracts.Exceptions;
using MagicCarpetContracts.ViewModels;
namespace MagicCarpetWebApi.Adapters;
public class AgencyAdapter : IAgencyAdapter
{
private readonly IAgencyBusinessLogicContract _warehouseBusinessLogicContract;
private readonly ILogger<AgencyAdapter> _logger;
private readonly IMapper _mapper;
public AgencyAdapter(IAgencyBusinessLogicContract warehouseBusinessLogicContract, ILogger<AgencyAdapter> logger)
{
_warehouseBusinessLogicContract = warehouseBusinessLogicContract;
_logger = logger;
var config = new MapperConfiguration(cfg =>
{
cfg.CreateMap<AgencyBindingModel, AgencyDataModel>();
cfg.CreateMap<AgencyDataModel, AgencyViewModel>();
cfg.CreateMap<TourAgencyBindingModel, TourAgencyDataModel>();
cfg.CreateMap<TourAgencyDataModel, TourAgencyViewModel>();
});
_mapper = new Mapper(config);
}
public AgencyOperationResponse GetAllComponents()
{
try
{
Console.WriteLine(_warehouseBusinessLogicContract.GetAllComponents());
return AgencyOperationResponse.OK([.. _warehouseBusinessLogicContract.GetAllComponents().Select(x => _mapper.Map<AgencyViewModel>(x))]);
}
catch (NullListException)
{
_logger.LogError("NullListException: The list of warehouse is null");
return AgencyOperationResponse.NotFound("The list of warehouse is not initialized");
}
catch (StorageException ex)
{
_logger.LogError(ex, "StorageException: Error while working with data storage");
return AgencyOperationResponse.InternalServerError($"Error while working with data storage: {ex.InnerException?.Message}");
}
catch (Exception ex)
{
_logger.LogError(ex, "Exception: An unexpected error occurred");
return AgencyOperationResponse.InternalServerError(ex.Message);
}
}
public AgencyOperationResponse GetComponentByData(string data)
{
try
{
return AgencyOperationResponse.OK(_mapper.Map<AgencyViewModel>(_warehouseBusinessLogicContract.GetComponentByData(data)));
}
catch (ArgumentNullException ex)
{
_logger.LogError(ex, "ArgumentNullException: Data is null or empty");
return AgencyOperationResponse.BadRequest("Data is empty");
}
catch (ElementNotFoundException ex)
{
_logger.LogError(ex, "ElementNotFoundException: warehouse not found");
return AgencyOperationResponse.NotFound($"warehouse with data '{data}' not found");
}
catch (StorageException ex)
{
_logger.LogError(ex, "StorageException: Error while working with data storage");
return AgencyOperationResponse.InternalServerError($"Error while working with data storage: {ex.InnerException?.Message}");
}
catch (Exception ex)
{
_logger.LogError(ex, "Exception: An unexpected error occurred");
return AgencyOperationResponse.InternalServerError(ex.Message);
}
}
public AgencyOperationResponse InsertComponent(AgencyBindingModel warehouseDataModel)
{
try
{
_warehouseBusinessLogicContract.InsertComponent(_mapper.Map<AgencyDataModel>(warehouseDataModel));
return AgencyOperationResponse.NoContent();
}
catch (ArgumentNullException ex)
{
_logger.LogError(ex, "ArgumentNullException: warehouse data is null");
return AgencyOperationResponse.BadRequest("furniture data is empty");
}
catch (ValidationException ex)
{
_logger.LogError(ex, "ValidationException: Invalid warehouse data");
return AgencyOperationResponse.BadRequest($"Invalid warehouse data: {ex.Message}");
}
catch (ElementExistsException ex)
{
_logger.LogError(ex, "ElementExistsException: warehouse already exists");
return AgencyOperationResponse.BadRequest(ex.Message);
}
catch (StorageException ex)
{
_logger.LogError(ex, "StorageException: Error while working with data storage");
return AgencyOperationResponse.BadRequest($"Error while working with data storage: {ex.InnerException?.Message}");
}
catch (Exception ex)
{
_logger.LogError(ex, "Exception: An unexpected error occurred");
return AgencyOperationResponse.InternalServerError(ex.Message);
}
}
public AgencyOperationResponse UpdateComponent(AgencyBindingModel warehouseDataModel)
{
try
{
_warehouseBusinessLogicContract.UpdateComponent(_mapper.Map<AgencyDataModel>(warehouseDataModel));
return AgencyOperationResponse.NoContent();
}
catch (ArgumentNullException ex)
{
_logger.LogError(ex, "ArgumentNullException: warehouse data is null");
return AgencyOperationResponse.BadRequest("warehouse data is empty");
}
catch (ValidationException ex)
{
_logger.LogError(ex, "ValidationException: Invalid warehouse data");
return AgencyOperationResponse.BadRequest($"Invalid warehouse data: {ex.Message}");
}
catch (ElementNotFoundException ex)
{
_logger.LogError(ex, "ElementNotFoundException: warehouse not found");
return AgencyOperationResponse.BadRequest($"warehouse with id '{warehouseDataModel.Id}' not found");
}
catch (ElementExistsException ex)
{
_logger.LogError(ex, "ElementExistsException: warehouse already exists");
return AgencyOperationResponse.BadRequest(ex.Message);
}
catch (StorageException ex)
{
_logger.LogError(ex, "StorageException: Error while working with data storage");
return AgencyOperationResponse.BadRequest($"Error while working with data storage: {ex.InnerException?.Message}");
}
catch (Exception ex)
{
_logger.LogError(ex, "Exception: An unexpected error occurred");
return AgencyOperationResponse.InternalServerError(ex.Message);
}
}
public AgencyOperationResponse DeleteComponent(string id)
{
try
{
_warehouseBusinessLogicContract.DeleteComponent(id);
return AgencyOperationResponse.NoContent();
}
catch (ArgumentNullException ex)
{
_logger.LogError(ex, "ArgumentNullException: Id is null or empty");
return AgencyOperationResponse.BadRequest("Id is empty");
}
catch (ValidationException ex)
{
_logger.LogError(ex, "ValidationException: Invalid id");
return AgencyOperationResponse.BadRequest($"Invalid id: {ex.Message}");
}
catch (ElementNotFoundException ex)
{
_logger.LogError(ex, "ElementNotFoundException: warehouse not found");
return AgencyOperationResponse.BadRequest($"furniture with id '{id}' not found");
}
catch (StorageException ex)
{
_logger.LogError(ex, "StorageException: Error while working with data storage");
return AgencyOperationResponse.BadRequest($"Error while working with data storage: {ex.InnerException?.Message}");
}
catch (Exception ex)
{
_logger.LogError(ex, "Exception: An unexpected error occurred");
return AgencyOperationResponse.InternalServerError(ex.Message);
}
}
}