196 lines
7.4 KiB
C#
196 lines
7.4 KiB
C#
using AutoMapper;
|
|
using SmallSoftwareContracts.AdapterContracts;
|
|
using SmallSoftwareContracts.AdapterContracts.OperationResponses;
|
|
using SmallSoftwareContracts.BindingModels;
|
|
using SmallSoftwareContracts.BusinessLogicsContracts;
|
|
using SmallSoftwareContracts.DataModels;
|
|
using SmallSoftwareContracts.Exceptions;
|
|
using SmallSoftwareContracts.ViewModels;
|
|
|
|
namespace SmallSoftwareWebApi.Adapters;
|
|
|
|
public class ManufacturerAdapter : IManufacturerAdapter
|
|
{
|
|
private readonly IManufacturerBusinessLogicContract _manufacturerBusinessLogicContract;
|
|
private readonly ILogger _logger;
|
|
private readonly Mapper _mapper;
|
|
public ManufacturerAdapter(IManufacturerBusinessLogicContract manufacturerBusinessLogicContract, ILogger<ManufacturerAdapter> logger)
|
|
{
|
|
_manufacturerBusinessLogicContract = manufacturerBusinessLogicContract;
|
|
_logger = logger;
|
|
var config = new MapperConfiguration(cfg =>
|
|
{
|
|
cfg.CreateMap<ManufacturerBindingModel, ManufacturerDataModel>();
|
|
cfg.CreateMap<ManufacturerDataModel, ManufacturerViewModel>();
|
|
});
|
|
_mapper = new Mapper(config);
|
|
}
|
|
public ManufacturerOperationResponse GetList()
|
|
{
|
|
try
|
|
{
|
|
return ManufacturerOperationResponse.OK([..
|
|
_manufacturerBusinessLogicContract.GetAllManufacturers().Select(x =>
|
|
_mapper.Map<ManufacturerViewModel>(x))]);
|
|
}
|
|
catch (NullListException)
|
|
{
|
|
_logger.LogError("NullListException");
|
|
return ManufacturerOperationResponse.NotFound("The list is not initialized");
|
|
}
|
|
catch (StorageException ex)
|
|
{
|
|
_logger.LogError(ex, "StorageException");
|
|
return
|
|
ManufacturerOperationResponse.InternalServerError($"Error while working with data storage: { ex.InnerException!.Message} ");
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_logger.LogError(ex, "Exception");
|
|
return
|
|
ManufacturerOperationResponse.InternalServerError(ex.Message);
|
|
}
|
|
}
|
|
public ManufacturerOperationResponse GetElement(string data)
|
|
{
|
|
try
|
|
{
|
|
return
|
|
ManufacturerOperationResponse.OK(_mapper.Map<ManufacturerViewModel>(_manufacturerBusinessLogicContract.GetManufacturerByData(data)));
|
|
}
|
|
catch (ArgumentNullException ex)
|
|
{
|
|
_logger.LogError(ex, "ArgumentNullException");
|
|
return ManufacturerOperationResponse.BadRequest("Data is empty");
|
|
}
|
|
catch (ElementNotFoundException ex)
|
|
{
|
|
_logger.LogError(ex, "ElementNotFoundException");
|
|
return ManufacturerOperationResponse.NotFound($"Not found element by data { data} ");
|
|
}
|
|
catch (StorageException ex)
|
|
{
|
|
_logger.LogError(ex, "StorageException");
|
|
return
|
|
ManufacturerOperationResponse.InternalServerError($"Error while working with data storage: { ex.InnerException!.Message} ");
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_logger.LogError(ex, "Exception");
|
|
return
|
|
ManufacturerOperationResponse.InternalServerError(ex.Message);
|
|
}
|
|
}
|
|
public ManufacturerOperationResponse
|
|
RegisterManufacturer(ManufacturerBindingModel manufacturerModel)
|
|
{
|
|
try
|
|
{
|
|
_manufacturerBusinessLogicContract.InsertManufacturer(_mapper.Map < ManufacturerDataModel > (manufacturerModel));
|
|
return ManufacturerOperationResponse.NoContent();
|
|
}
|
|
catch (ArgumentNullException ex)
|
|
{
|
|
_logger.LogError(ex, "ArgumentNullException");
|
|
return ManufacturerOperationResponse.BadRequest("Data is empty");
|
|
}
|
|
catch (ValidationException ex)
|
|
{
|
|
_logger.LogError(ex, "ValidationException");
|
|
return ManufacturerOperationResponse.BadRequest($"Incorrect data transmitted: { ex.Message} ");
|
|
}
|
|
catch (ElementExistsException ex)
|
|
{
|
|
_logger.LogError(ex, "ElementExistsException");
|
|
return ManufacturerOperationResponse.BadRequest(ex.Message);
|
|
}
|
|
catch (StorageException ex)
|
|
{
|
|
_logger.LogError(ex, "StorageException");
|
|
return ManufacturerOperationResponse.BadRequest($"Error while working with data storage: { ex.InnerException!.Message} ");
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_logger.LogError(ex, "Exception");
|
|
return
|
|
ManufacturerOperationResponse.InternalServerError(ex.Message);
|
|
}
|
|
}
|
|
public ManufacturerOperationResponse
|
|
ChangeManufacturerInfo(ManufacturerBindingModel manufacturerModel)
|
|
{
|
|
try
|
|
{
|
|
_manufacturerBusinessLogicContract.UpdateManufacturer(_mapper.Map < ManufacturerDataModel > (manufacturerModel));
|
|
return ManufacturerOperationResponse.NoContent();
|
|
}
|
|
catch (ArgumentNullException ex)
|
|
{
|
|
_logger.LogError(ex, "ArgumentNullException");
|
|
return ManufacturerOperationResponse.BadRequest("Data is empty");
|
|
}
|
|
catch (ValidationException ex)
|
|
{
|
|
_logger.LogError(ex, "ValidationException");
|
|
return ManufacturerOperationResponse.BadRequest($"Incorrect data transmitted: { ex.Message}");
|
|
}
|
|
catch (ElementNotFoundException ex)
|
|
{
|
|
_logger.LogError(ex, "ElementNotFoundException");
|
|
return ManufacturerOperationResponse.BadRequest($"Not found element by Id { manufacturerModel.Id} ");
|
|
}
|
|
catch (ElementExistsException ex)
|
|
{
|
|
_logger.LogError(ex, "ElementExistsException");
|
|
return ManufacturerOperationResponse.BadRequest(ex.Message);
|
|
}
|
|
catch (StorageException ex)
|
|
{
|
|
_logger.LogError(ex, "StorageException");
|
|
return ManufacturerOperationResponse
|
|
.BadRequest($"Error while working with data storage: { ex.InnerException!.Message}");
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_logger.LogError(ex, "Exception");
|
|
return
|
|
ManufacturerOperationResponse.InternalServerError(ex.Message);
|
|
}
|
|
}
|
|
public ManufacturerOperationResponse RemoveManufacturer(string id)
|
|
{
|
|
try
|
|
{
|
|
_manufacturerBusinessLogicContract.DeleteManufacturer(id);
|
|
return ManufacturerOperationResponse.NoContent();
|
|
}
|
|
catch (ArgumentNullException ex)
|
|
{
|
|
_logger.LogError(ex, "ArgumentNullException");
|
|
return ManufacturerOperationResponse.BadRequest("Id is empty");
|
|
}
|
|
catch (ValidationException ex)
|
|
{
|
|
_logger.LogError(ex, "ValidationException");
|
|
return ManufacturerOperationResponse.BadRequest($"Incorrect data transmitted: { ex.Message} ");
|
|
}
|
|
catch (ElementNotFoundException ex)
|
|
{
|
|
_logger.LogError(ex, "ElementNotFoundException");
|
|
return ManufacturerOperationResponse.BadRequest($"Not found element by id: { id} ");
|
|
}
|
|
catch (StorageException ex)
|
|
{
|
|
_logger.LogError(ex, "StorageException");
|
|
return ManufacturerOperationResponse
|
|
.BadRequest($"Error while working with data storage: { ex.InnerException!.Message} ");
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_logger.LogError(ex, "Exception");
|
|
return
|
|
ManufacturerOperationResponse.InternalServerError(ex.Message);
|
|
}
|
|
}
|
|
}
|