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

161 lines
6.5 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 SuppliesAdapter : ISuppliesAdapter
{
private readonly ISuppliesBusinessLogicContract _suppliesBusinessLogicContract;
private readonly ILogger<SuppliesAdapter> _logger;
private readonly IMapper _mapper;
public SuppliesAdapter(ISuppliesBusinessLogicContract suppliesBusinessLogicContract,
ILogger<SuppliesAdapter> logger)
{
_suppliesBusinessLogicContract = suppliesBusinessLogicContract;
_logger = logger;
var config = new MapperConfiguration(cfg =>
{
cfg.CreateMap<SuppliesBindingModel, SuppliesDataModel>();
cfg.CreateMap<SuppliesDataModel, SuppliesViewModel>();
cfg.CreateMap<TourSuppliesBindingModel, TourSuppliesDataModel>();
cfg.CreateMap<TourSuppliesDataModel, TourSuppliesViewModel>();
});
_mapper = new Mapper(config);
}
public SuppliesOperationResponse GetAllComponents()
{
try
{
return SuppliesOperationResponse.OK([.. _suppliesBusinessLogicContract.GetAllComponents().Select(x => _mapper.Map<SuppliesViewModel>(x))]);
}
catch (NullListException)
{
_logger.LogError("NullListException: The list of supplies is null");
return SuppliesOperationResponse.NotFound("The list of supplies is not initialized");
}
catch (StorageException ex)
{
_logger.LogError(ex, "StorageException: Error while working with data storage");
return SuppliesOperationResponse.InternalServerError($"Error while working with data storage: {ex.InnerException?.Message}");
}
catch (Exception ex)
{
_logger.LogError(ex, "Exception: An unexpected error occurred");
return SuppliesOperationResponse.InternalServerError(ex.Message);
}
}
public SuppliesOperationResponse GetComponentByData(string data)
{
try
{
return SuppliesOperationResponse.OK(_mapper.Map<SuppliesViewModel>(_suppliesBusinessLogicContract.GetComponentByData(data)));
}
catch (ArgumentNullException ex)
{
_logger.LogError(ex, "ArgumentNullException: Data is null or empty");
return SuppliesOperationResponse.BadRequest("Data is empty");
}
catch (ElementNotFoundException ex)
{
_logger.LogError(ex, "ElementNotFoundException: Component not found");
return SuppliesOperationResponse.NotFound($"Component with data '{data}' not found");
}
catch (StorageException ex)
{
_logger.LogError(ex, "StorageException: Error while working with data storage");
return SuppliesOperationResponse.InternalServerError(
$"Error while working with data storage: {ex.InnerException?.Message}");
}
catch (Exception ex)
{
_logger.LogError(ex, "Exception: An unexpected error occurred");
return SuppliesOperationResponse.InternalServerError(ex.Message);
}
}
public SuppliesOperationResponse InsertComponent(SuppliesBindingModel componentDataModel)
{
try
{
_suppliesBusinessLogicContract.InsertComponent(_mapper.Map<SuppliesDataModel>(componentDataModel));
return SuppliesOperationResponse.NoContent();
}
catch (ArgumentNullException ex)
{
_logger.LogError(ex, "ArgumentNullException: Component data is null");
return SuppliesOperationResponse.BadRequest("Component data is empty");
}
catch (ValidationException ex)
{
_logger.LogError(ex, "ValidationException: Invalid component data");
return SuppliesOperationResponse.BadRequest($"Invalid component data: {ex.Message}");
}
catch (ElementExistsException ex)
{
_logger.LogError(ex, "ElementExistsException: Component already exists");
return SuppliesOperationResponse.BadRequest(ex.Message);
}
catch (StorageException ex)
{
_logger.LogError(ex, "StorageException: Error while working with data storage");
return SuppliesOperationResponse.BadRequest(
$"Error while working with data storage: {ex.InnerException?.Message}");
}
catch (Exception ex)
{
_logger.LogError(ex, "Exception: An unexpected error occurred");
return SuppliesOperationResponse.InternalServerError(ex.Message);
}
}
public SuppliesOperationResponse UpdateComponent(SuppliesBindingModel componentModel)
{
try
{
_suppliesBusinessLogicContract.UpdateComponent(_mapper.Map<SuppliesDataModel>(componentModel));
return SuppliesOperationResponse.NoContent();
}
catch (ArgumentNullException ex)
{
_logger.LogError(ex, "ArgumentNullException: Component data is null");
return SuppliesOperationResponse.BadRequest("Component data is empty");
}
catch (ValidationException ex)
{
_logger.LogError(ex, "ValidationException: Invalid component data");
return SuppliesOperationResponse.BadRequest($"Invalid component data: {ex.Message}");
}
catch (ElementNotFoundException ex)
{
_logger.LogError(ex, "ElementNotFoundException: Component not found");
return SuppliesOperationResponse.BadRequest($"Component with id '{componentModel.Id}' not found");
}
catch (ElementExistsException ex)
{
_logger.LogError(ex, "ElementExistsException: Component already exists");
return SuppliesOperationResponse.BadRequest(ex.Message);
}
catch (StorageException ex)
{
_logger.LogError(ex, "StorageException: Error while working with data storage");
return SuppliesOperationResponse.BadRequest($"Error while working with data storage: {ex.InnerException?.Message}");
}
catch (Exception ex)
{
_logger.LogError(ex, "Exception: An unexpected error occurred");
return SuppliesOperationResponse.InternalServerError(ex.Message);
}
}
}