forked from slavaxom9k/PIBD-23_Fomichev_V.S._MagicCarpet
263 lines
9.8 KiB
C#
263 lines
9.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 SaleAdapter : ISaleAdapter
|
|
{
|
|
private readonly ISaleBusinessLogicContract _saleBusinessLogicContract;
|
|
|
|
private readonly ILogger _logger;
|
|
|
|
private readonly Mapper _mapper;
|
|
|
|
public SaleAdapter(ISaleBusinessLogicContract saleBusinessLogicContract, ILogger<SaleAdapter> logger)
|
|
{
|
|
_saleBusinessLogicContract = saleBusinessLogicContract;
|
|
_logger = logger;
|
|
var config = new MapperConfiguration(cfg =>
|
|
{
|
|
cfg.CreateMap<SaleBindingModel, SaleDataModel>();
|
|
cfg.CreateMap<SaleDataModel, SaleViewModel>();
|
|
cfg.CreateMap<SaleTourBindingModel, SaleTourDataModel>();
|
|
cfg.CreateMap<SaleTourDataModel, SaleTourViewModel>();
|
|
});
|
|
_mapper = new Mapper(config);
|
|
}
|
|
|
|
public SaleOperationResponse GetList(DateTime fromDate, DateTime toDate)
|
|
{
|
|
try
|
|
{
|
|
return SaleOperationResponse.OK([.. _saleBusinessLogicContract.GetAllSalesByPeriod(fromDate, toDate).Select(x => _mapper.Map<SaleViewModel>(x))]);
|
|
}
|
|
catch (IncorrectDatesException ex)
|
|
{
|
|
_logger.LogError(ex, "IncorrectDatesException");
|
|
return SaleOperationResponse.BadRequest($"Incorrect dates: {ex.Message}");
|
|
}
|
|
catch (NullListException)
|
|
{
|
|
_logger.LogError("NullListException");
|
|
return SaleOperationResponse.NotFound("The list is not initialized");
|
|
}
|
|
catch (StorageException ex)
|
|
{
|
|
_logger.LogError(ex, "StorageException");
|
|
return SaleOperationResponse.InternalServerError($"Error while working with data storage: {ex.InnerException!.Message}");
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_logger.LogError(ex, "Exception");
|
|
return SaleOperationResponse.InternalServerError(ex.Message);
|
|
}
|
|
}
|
|
|
|
public SaleOperationResponse GetEmployeeList(string id, DateTime fromDate, DateTime toDate)
|
|
{
|
|
try
|
|
{
|
|
return SaleOperationResponse.OK([.. _saleBusinessLogicContract.GetAllSalesByEmployeeByPeriod(id, fromDate, toDate).Select(x => _mapper.Map<SaleViewModel>(x))]);
|
|
}
|
|
catch (IncorrectDatesException ex)
|
|
{
|
|
_logger.LogError(ex, "IncorrectDatesException");
|
|
return SaleOperationResponse.BadRequest($"Incorrect dates: {ex.Message}");
|
|
}
|
|
catch (ValidationException ex)
|
|
{
|
|
_logger.LogError(ex, "ValidationException");
|
|
return SaleOperationResponse.BadRequest($"Incorrect data transmitted: {ex.Message}");
|
|
}
|
|
catch (NullListException)
|
|
{
|
|
_logger.LogError("NullListException");
|
|
return SaleOperationResponse.NotFound("The list is not initialized");
|
|
}
|
|
catch (StorageException ex)
|
|
{
|
|
_logger.LogError(ex, "StorageException");
|
|
return SaleOperationResponse.InternalServerError($"Error while working with data storage: {ex.InnerException!.Message}");
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_logger.LogError(ex, "Exception");
|
|
return SaleOperationResponse.InternalServerError(ex.Message);
|
|
}
|
|
}
|
|
|
|
public SaleOperationResponse GetClientList(string id, DateTime fromDate, DateTime toDate)
|
|
{
|
|
try
|
|
{
|
|
return SaleOperationResponse.OK([.. _saleBusinessLogicContract.GetAllSalesByClientByPeriod(id, fromDate, toDate).Select(x => _mapper.Map<SaleViewModel>(x))]);
|
|
}
|
|
catch (IncorrectDatesException ex)
|
|
{
|
|
_logger.LogError(ex, "IncorrectDatesException");
|
|
return SaleOperationResponse.BadRequest($"Incorrect dates: {ex.Message}");
|
|
}
|
|
catch (ValidationException ex)
|
|
{
|
|
_logger.LogError(ex, "ValidationException");
|
|
return SaleOperationResponse.BadRequest($"Incorrect data transmitted: {ex.Message}");
|
|
}
|
|
catch (NullListException)
|
|
{
|
|
_logger.LogError("NullListException");
|
|
return SaleOperationResponse.NotFound("The list is not initialized");
|
|
}
|
|
catch (StorageException ex)
|
|
{
|
|
_logger.LogError(ex, "StorageException");
|
|
return SaleOperationResponse.InternalServerError($"Error while working with data storage: {ex.InnerException!.Message}");
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_logger.LogError(ex, "Exception");
|
|
return SaleOperationResponse.InternalServerError(ex.Message);
|
|
}
|
|
}
|
|
|
|
public SaleOperationResponse GetCocktailList(string id, DateTime fromDate, DateTime toDate)
|
|
{
|
|
try
|
|
{
|
|
return SaleOperationResponse.OK([.. _saleBusinessLogicContract.GetAllSalesByTourByPeriod(id, fromDate, toDate).Select(x => _mapper.Map<SaleViewModel>(x))]);
|
|
}
|
|
catch (IncorrectDatesException ex)
|
|
{
|
|
_logger.LogError(ex, "IncorrectDatesException");
|
|
return SaleOperationResponse.BadRequest($"Incorrect dates: {ex.Message}");
|
|
}
|
|
catch (ValidationException ex)
|
|
{
|
|
_logger.LogError(ex, "ValidationException");
|
|
return SaleOperationResponse.BadRequest($"Incorrect data transmitted: {ex.Message}");
|
|
}
|
|
catch (NullListException)
|
|
{
|
|
_logger.LogError("NullListException");
|
|
return SaleOperationResponse.NotFound("The list is not initialized");
|
|
}
|
|
catch (StorageException ex)
|
|
{
|
|
_logger.LogError(ex, "StorageException");
|
|
return SaleOperationResponse.InternalServerError($"Error while working with data storage: {ex.InnerException!.Message}");
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_logger.LogError(ex, "Exception");
|
|
return SaleOperationResponse.InternalServerError(ex.Message);
|
|
}
|
|
}
|
|
|
|
public SaleOperationResponse GetElement(string id)
|
|
{
|
|
try
|
|
{
|
|
return SaleOperationResponse.OK(_mapper.Map<SaleViewModel>(_saleBusinessLogicContract.GetSaleByData(id)));
|
|
}
|
|
catch (ArgumentNullException ex)
|
|
{
|
|
_logger.LogError(ex, "ArgumentNullException");
|
|
return SaleOperationResponse.BadRequest("Data is empty");
|
|
}
|
|
catch (ValidationException ex)
|
|
{
|
|
_logger.LogError(ex, "ValidationException");
|
|
return SaleOperationResponse.BadRequest($"Incorrect data transmitted: {ex.Message}");
|
|
}
|
|
catch (ElementNotFoundException ex)
|
|
{
|
|
_logger.LogError(ex, "ElementNotFoundException");
|
|
return SaleOperationResponse.NotFound($"Not found element by data {id}");
|
|
}
|
|
catch (StorageException ex)
|
|
{
|
|
_logger.LogError(ex, "StorageException");
|
|
return SaleOperationResponse.InternalServerError($"Error while working with data storage: {ex.InnerException!.Message}");
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_logger.LogError(ex, "Exception");
|
|
return SaleOperationResponse.InternalServerError(ex.Message);
|
|
}
|
|
}
|
|
|
|
public SaleOperationResponse MakeSale(SaleBindingModel saleModel)
|
|
{
|
|
try
|
|
{
|
|
var data = _mapper.Map<SaleDataModel>(saleModel);
|
|
_saleBusinessLogicContract.InsertSale(_mapper.Map<SaleDataModel>(saleModel));
|
|
return SaleOperationResponse.NoContent();
|
|
}
|
|
catch (ArgumentNullException ex)
|
|
{
|
|
_logger.LogError(ex, "ArgumentNullException");
|
|
return SaleOperationResponse.BadRequest("Data is empty");
|
|
}
|
|
catch (ValidationException ex)
|
|
{
|
|
_logger.LogError(ex, "ValidationException");
|
|
return SaleOperationResponse.BadRequest($"Incorrect data transmitted: {ex.Message}");
|
|
}
|
|
catch (StorageException ex)
|
|
{
|
|
_logger.LogError(ex, "StorageException");
|
|
return SaleOperationResponse.BadRequest($"Error while working with data storage: {ex.InnerException!.Message}");
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_logger.LogError(ex, "Exception");
|
|
return SaleOperationResponse.InternalServerError(ex.Message);
|
|
}
|
|
}
|
|
|
|
public SaleOperationResponse CancelSale(string id)
|
|
{
|
|
try
|
|
{
|
|
_saleBusinessLogicContract.CancelSale(id);
|
|
return SaleOperationResponse.NoContent();
|
|
}
|
|
catch (ArgumentNullException ex)
|
|
{
|
|
_logger.LogError(ex, "ArgumentNullException");
|
|
return SaleOperationResponse.BadRequest("Id is empty");
|
|
}
|
|
catch (ValidationException ex)
|
|
{
|
|
_logger.LogError(ex, "ValidationException");
|
|
return SaleOperationResponse.BadRequest($"Incorrect data transmitted: {ex.Message}");
|
|
}
|
|
catch (ElementNotFoundException ex)
|
|
{
|
|
_logger.LogError(ex, "ElementNotFoundException");
|
|
return SaleOperationResponse.BadRequest($"Not found element by id: {id}");
|
|
}
|
|
catch (ElementDeletedException ex)
|
|
{
|
|
_logger.LogError(ex, "ElementDeletedException");
|
|
return SaleOperationResponse.BadRequest($"Element by id: {id} was deleted");
|
|
}
|
|
catch (StorageException ex)
|
|
{
|
|
_logger.LogError(ex, "StorageException");
|
|
return SaleOperationResponse.BadRequest($"Error while working with data storage: {ex.InnerException!.Message}");
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_logger.LogError(ex, "Exception");
|
|
return SaleOperationResponse.InternalServerError(ex.Message);
|
|
}
|
|
}
|
|
}
|