forked from slavaxom9k/PIBD-23_Fomichev_V.S._MagicCarpet
189 lines
6.8 KiB
C#
189 lines
6.8 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 ClientAdapter : IClientAdapter
|
|
{
|
|
private readonly IClientBusinessLogicContract _clientBusinessLogicContract;
|
|
|
|
private readonly ILogger _logger;
|
|
|
|
private readonly Mapper _mapper;
|
|
public ClientAdapter(IClientBusinessLogicContract clientBusinessLogicContract, ILogger<ClientAdapter> logger)
|
|
{
|
|
_clientBusinessLogicContract = clientBusinessLogicContract;
|
|
_logger = logger;
|
|
var config = new MapperConfiguration(cfg =>
|
|
{
|
|
cfg.CreateMap<ClientBindingModel, ClientDataModel>();
|
|
cfg.CreateMap<ClientDataModel, ClientViewModel>();
|
|
});
|
|
_mapper = new Mapper(config);
|
|
}
|
|
|
|
public ClientOperationResponse GetList()
|
|
{
|
|
try
|
|
{
|
|
return ClientOperationResponse.OK([.. _clientBusinessLogicContract.GetAllClients().Select(x => _mapper.Map<ClientViewModel>(x))]);
|
|
}
|
|
catch (NullListException)
|
|
{
|
|
_logger.LogError("NullListException");
|
|
return ClientOperationResponse.NotFound("The list is not initialized");
|
|
}
|
|
catch (StorageException ex)
|
|
{
|
|
_logger.LogError(ex, "StorageException");
|
|
return ClientOperationResponse.InternalServerError($"Error while working with data storage: {ex.InnerException!.Message}");
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_logger.LogError(ex, "Exception");
|
|
return ClientOperationResponse.InternalServerError(ex.Message);
|
|
}
|
|
}
|
|
|
|
public ClientOperationResponse GetElement(string data)
|
|
{
|
|
try
|
|
{
|
|
return ClientOperationResponse.OK(_mapper.Map<ClientViewModel>(_clientBusinessLogicContract.GetClientByData(data)));
|
|
}
|
|
catch (ArgumentNullException ex)
|
|
{
|
|
_logger.LogError(ex, "ArgumentNullException");
|
|
return ClientOperationResponse.BadRequest("Data is empty");
|
|
}
|
|
catch (ElementNotFoundException ex)
|
|
{
|
|
_logger.LogError(ex, "ElementNotFoundException");
|
|
return ClientOperationResponse.NotFound($"Not found element by data {data}");
|
|
}
|
|
catch (StorageException ex)
|
|
{
|
|
_logger.LogError(ex, "StorageException");
|
|
return ClientOperationResponse.InternalServerError($"Error while working with data storage: {ex.InnerException!.Message}");
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_logger.LogError(ex, "Exception");
|
|
return ClientOperationResponse.InternalServerError(ex.Message);
|
|
}
|
|
}
|
|
|
|
public ClientOperationResponse RegisterClient(ClientBindingModel clientModel)
|
|
{
|
|
try
|
|
{
|
|
_clientBusinessLogicContract.InsertClient(_mapper.Map<ClientDataModel>(clientModel));
|
|
return ClientOperationResponse.NoContent();
|
|
}
|
|
catch (ArgumentNullException ex)
|
|
{
|
|
_logger.LogError(ex, "ArgumentNullException");
|
|
return ClientOperationResponse.BadRequest("Data is empty");
|
|
}
|
|
catch (ValidationException ex)
|
|
{
|
|
_logger.LogError(ex, "ValidationException");
|
|
return ClientOperationResponse.BadRequest($"Incorrect data transmitted: {ex.Message}");
|
|
}
|
|
catch (ElementExistsException ex)
|
|
{
|
|
_logger.LogError(ex, "ElementExistsException");
|
|
return ClientOperationResponse.BadRequest(ex.Message);
|
|
}
|
|
catch (StorageException ex)
|
|
{
|
|
_logger.LogError(ex, "StorageException");
|
|
return ClientOperationResponse.BadRequest($"Error while working with data storage: {ex.InnerException!.Message}");
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_logger.LogError(ex, "Exception");
|
|
return ClientOperationResponse.InternalServerError(ex.Message);
|
|
}
|
|
}
|
|
|
|
public ClientOperationResponse ChangeClientInfo(ClientBindingModel clientModel)
|
|
{
|
|
try
|
|
{
|
|
_clientBusinessLogicContract.UpdateClient(_mapper.Map<ClientDataModel>(clientModel));
|
|
return ClientOperationResponse.NoContent();
|
|
}
|
|
catch (ArgumentNullException ex)
|
|
{
|
|
_logger.LogError(ex, "ArgumentNullException");
|
|
return ClientOperationResponse.BadRequest("Data is empty");
|
|
}
|
|
catch (ValidationException ex)
|
|
{
|
|
_logger.LogError(ex, "ValidationException");
|
|
return ClientOperationResponse.BadRequest($"Incorrect data transmitted: {ex.Message}");
|
|
}
|
|
catch (ElementNotFoundException ex)
|
|
{
|
|
_logger.LogError(ex, "ElementNotFoundException");
|
|
return ClientOperationResponse.BadRequest($"Not found element by Id {clientModel.Id}");
|
|
}
|
|
catch (ElementExistsException ex)
|
|
{
|
|
_logger.LogError(ex, "ElementExistsException");
|
|
return ClientOperationResponse.BadRequest(ex.Message);
|
|
}
|
|
catch (StorageException ex)
|
|
{
|
|
_logger.LogError(ex, "StorageException");
|
|
return ClientOperationResponse.BadRequest($"Error while working with data storage: {ex.InnerException!.Message}");
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_logger.LogError(ex, "Exception");
|
|
return ClientOperationResponse.InternalServerError(ex.Message);
|
|
}
|
|
}
|
|
|
|
public ClientOperationResponse RemoveClient(string id)
|
|
{
|
|
try
|
|
{
|
|
_clientBusinessLogicContract.DeleteClient(id);
|
|
return ClientOperationResponse.NoContent();
|
|
}
|
|
catch (ArgumentNullException ex)
|
|
{
|
|
_logger.LogError(ex, "ArgumentNullException");
|
|
return ClientOperationResponse.BadRequest("Id is empty");
|
|
}
|
|
catch (ValidationException ex)
|
|
{
|
|
_logger.LogError(ex, "ValidationException");
|
|
return ClientOperationResponse.BadRequest($"Incorrect data transmitted: {ex.Message}");
|
|
}
|
|
catch (ElementNotFoundException ex)
|
|
{
|
|
_logger.LogError(ex, "ElementNotFoundException");
|
|
return ClientOperationResponse.BadRequest($"Not found element by id: {id}");
|
|
}
|
|
catch (StorageException ex)
|
|
{
|
|
_logger.LogError(ex, "StorageException");
|
|
return ClientOperationResponse.BadRequest($"Error while working with data storage: {ex.InnerException!.Message}");
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_logger.LogError(ex, "Exception");
|
|
return ClientOperationResponse.InternalServerError(ex.Message);
|
|
}
|
|
}
|
|
}
|