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 logger) { _saleBusinessLogicContract = saleBusinessLogicContract; _logger = logger; var config = new MapperConfiguration(cfg => { cfg.CreateMap(); cfg.CreateMap(); cfg.CreateMap(); cfg.CreateMap(); }); _mapper = new Mapper(config); } public SaleOperationResponse GetList(DateTime fromDate, DateTime toDate) { try { return SaleOperationResponse.OK([.. _saleBusinessLogicContract.GetAllSalesByPeriod(fromDate, toDate).Select(x => _mapper.Map(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(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(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 GetTourList(string id, DateTime fromDate, DateTime toDate) { try { return SaleOperationResponse.OK([.. _saleBusinessLogicContract.GetAllSalesByTourByPeriod(id, fromDate, toDate).Select(x => _mapper.Map(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(_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(saleModel); _saleBusinessLogicContract.InsertSale(_mapper.Map(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); } } }