forked from slavaxom9k/PIBD-23_Fomichev_V.S._MagicCarpet
256 lines
10 KiB
C#
256 lines
10 KiB
C#
using AutoMapper;
|
|
using DocumentFormat.OpenXml.Office2010.Excel;
|
|
using MagicCarpetContracts.AdapterContracts;
|
|
using MagicCarpetContracts.AdapterContracts.OperationResponses;
|
|
using MagicCarpetContracts.BindingModels;
|
|
using MagicCarpetContracts.BuisnessLogicContracts;
|
|
using MagicCarpetContracts.DataModels;
|
|
using MagicCarpetContracts.Exceptions;
|
|
using MagicCarpetContracts.Mapper;
|
|
using MagicCarpetContracts.Resources;
|
|
using MagicCarpetContracts.ViewModels;
|
|
using Microsoft.Extensions.Localization;
|
|
|
|
namespace MagicCarpetWebApi.Adapters;
|
|
|
|
internal class SaleAdapter(ISaleBusinessLogicContract saleBusinessLogicContract, IStringLocalizer<Messages> localizer, ILogger<SaleAdapter> logger) : ISaleAdapter
|
|
{
|
|
private readonly ISaleBusinessLogicContract _saleBusinessLogicContract = saleBusinessLogicContract;
|
|
private readonly IStringLocalizer<Messages> _localizer = localizer;
|
|
private readonly ILogger _logger = logger;
|
|
private SaleViewModel MapSaleViewModel(SaleDataModel source) => CustomMapper.MapObject<SaleViewModel>(source);
|
|
|
|
public SaleOperationResponse GetList(DateTime fromDate, DateTime toDate)
|
|
{
|
|
try
|
|
{
|
|
var data = _saleBusinessLogicContract.GetAllSalesByPeriod(
|
|
fromDate.ToUniversalTime(),
|
|
toDate.ToUniversalTime());
|
|
|
|
return SaleOperationResponse.OK(
|
|
data.Select(x => MapSaleViewModel(x)).ToList());
|
|
}
|
|
catch (IncorrectDatesException ex)
|
|
{
|
|
_logger.LogError(ex, "IncorrectDatesException");
|
|
return SaleOperationResponse.BadRequest(string.Format(_localizer["AdapterMessageIncorrectDatesException"], ex.Message));
|
|
}
|
|
catch (StorageException ex)
|
|
{
|
|
_logger.LogError(ex, "StorageException");
|
|
return SaleOperationResponse.InternalServerError(string.Format(_localizer["AdapterMessageStorageException"], 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
|
|
{
|
|
var data = _saleBusinessLogicContract.GetAllSalesByEmployeeByPeriod(
|
|
id,
|
|
fromDate.ToUniversalTime(),
|
|
toDate.ToUniversalTime());
|
|
|
|
return SaleOperationResponse.OK(
|
|
data.Select(x => MapSaleViewModel(x)).ToList());
|
|
}
|
|
catch (IncorrectDatesException ex)
|
|
{
|
|
_logger.LogError(ex, "IncorrectDatesException");
|
|
return SaleOperationResponse.BadRequest(string.Format(_localizer["AdapterMessageIncorrectDatesException"], ex.Message));
|
|
}
|
|
catch (ValidationException ex)
|
|
{
|
|
_logger.LogError(ex, "ValidationException");
|
|
return SaleOperationResponse.BadRequest(string.Format(_localizer["AdapterMessageValidationException"], ex.Message));
|
|
}
|
|
catch (StorageException ex)
|
|
{
|
|
_logger.LogError(ex, "StorageException");
|
|
return SaleOperationResponse.InternalServerError(string.Format(_localizer["AdapterMessageStorageException"], 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
|
|
{
|
|
var data = _saleBusinessLogicContract.GetAllSalesByClientByPeriod(
|
|
id,
|
|
fromDate.ToUniversalTime(),
|
|
toDate.ToUniversalTime());
|
|
|
|
return SaleOperationResponse.OK(
|
|
data.Select(x => MapSaleViewModel(x)).ToList());
|
|
}
|
|
catch (IncorrectDatesException ex)
|
|
{
|
|
_logger.LogError(ex, "IncorrectDatesException");
|
|
return SaleOperationResponse.BadRequest(string.Format(_localizer["AdapterMessageIncorrectDatesException"], ex.Message));
|
|
}
|
|
catch (ValidationException ex)
|
|
{
|
|
_logger.LogError(ex, "ValidationException");
|
|
return SaleOperationResponse.BadRequest(string.Format(_localizer["AdapterMessageValidationException"], ex.Message));
|
|
}
|
|
catch (StorageException ex)
|
|
{
|
|
_logger.LogError(ex, "StorageException");
|
|
return SaleOperationResponse.InternalServerError(string.Format(_localizer["AdapterMessageValidationException"], ex.Message));
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_logger.LogError(ex, "Exception");
|
|
return SaleOperationResponse.InternalServerError(ex.Message);
|
|
}
|
|
}
|
|
|
|
public SaleOperationResponse GetTourList(string id, DateTime fromDate, DateTime toDate)
|
|
{
|
|
try
|
|
{
|
|
var data = _saleBusinessLogicContract.GetAllSalesByTourByPeriod(
|
|
id,
|
|
fromDate.ToUniversalTime(),
|
|
toDate.ToUniversalTime());
|
|
|
|
return SaleOperationResponse.OK(
|
|
data.Select(x => MapSaleViewModel(x)).ToList());
|
|
}
|
|
catch (IncorrectDatesException ex)
|
|
{
|
|
_logger.LogError(ex, "IncorrectDatesException");
|
|
return SaleOperationResponse.BadRequest(string.Format(_localizer["AdapterMessageIncorrectDatesException"], ex.Message));
|
|
}
|
|
catch (ValidationException ex)
|
|
{
|
|
_logger.LogError(ex, "ValidationException");
|
|
return SaleOperationResponse.BadRequest(string.Format(_localizer["AdapterMessageValidationException"], ex.Message));
|
|
}
|
|
catch (StorageException ex)
|
|
{
|
|
_logger.LogError(ex, "StorageException");
|
|
return SaleOperationResponse.InternalServerError(string.Format(_localizer["AdapterMessageStorageException"], ex.InnerException!.Message));
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_logger.LogError(ex, "Exception");
|
|
return SaleOperationResponse.InternalServerError(ex.Message);
|
|
}
|
|
}
|
|
|
|
public SaleOperationResponse GetElement(string id)
|
|
{
|
|
try
|
|
{
|
|
var data = _saleBusinessLogicContract.GetSaleByData(id);
|
|
return SaleOperationResponse.OK(MapSaleViewModel(data));
|
|
}
|
|
catch (ArgumentNullException ex)
|
|
{
|
|
_logger.LogError(ex, "ArgumentNullException");
|
|
return SaleOperationResponse.BadRequest(_localizer["AdapterMessageEmptyDate"]);
|
|
}
|
|
catch (ValidationException ex)
|
|
{
|
|
_logger.LogError(ex, "ValidationException");
|
|
return SaleOperationResponse.BadRequest(string.Format(_localizer["AdapterMessageValidationException"], ex.Message));
|
|
}
|
|
catch (ElementNotFoundException ex)
|
|
{
|
|
_logger.LogError(ex, "ElementNotFoundException");
|
|
return SaleOperationResponse.NotFound(string.Format(_localizer["AdapterMessageElementNotFoundException"], id));
|
|
}
|
|
catch (StorageException ex)
|
|
{
|
|
_logger.LogError(ex, "StorageException");
|
|
return SaleOperationResponse.InternalServerError(string.Format(_localizer["AdapterMessageStorageException"], ex.InnerException!.Message));
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_logger.LogError(ex, "Exception");
|
|
return SaleOperationResponse.InternalServerError(ex.Message);
|
|
}
|
|
}
|
|
|
|
public SaleOperationResponse MakeSale(SaleBindingModel saleModel)
|
|
{
|
|
try
|
|
{
|
|
var dataModel = CustomMapper.MapObject<SaleDataModel>(saleModel);
|
|
_saleBusinessLogicContract.InsertSale(dataModel);
|
|
return SaleOperationResponse.NoContent();
|
|
}
|
|
catch (ArgumentNullException ex)
|
|
{
|
|
_logger.LogError(ex, "ArgumentNullException");
|
|
return SaleOperationResponse.BadRequest(_localizer["AdapterMessageEmptyDate"]);
|
|
}
|
|
catch (ValidationException ex)
|
|
{
|
|
_logger.LogError(ex, "ValidationException");
|
|
return SaleOperationResponse.BadRequest(string.Format(_localizer["AdapterMessageValidationException"], ex.Message));
|
|
}
|
|
catch (StorageException ex)
|
|
{
|
|
_logger.LogError(ex, "StorageException");
|
|
return SaleOperationResponse.InternalServerError(string.Format(_localizer["AdapterMessageStorageException"], 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(_localizer["AdapterMessageEmptyDate"]);
|
|
}
|
|
catch (ValidationException ex)
|
|
{
|
|
_logger.LogError(ex, "ValidationException");
|
|
return SaleOperationResponse.BadRequest(string.Format(_localizer["AdapterMessageValidationException"], ex.Message));
|
|
}
|
|
catch (ElementNotFoundException ex)
|
|
{
|
|
_logger.LogError(ex, "ElementNotFoundException");
|
|
return SaleOperationResponse.BadRequest(string.Format(_localizer["AdapterMessageElementNotFoundException"], id));
|
|
}
|
|
catch (ElementDeletedException ex)
|
|
{
|
|
_logger.LogError(ex, "ElementDeletedException");
|
|
return SaleOperationResponse.BadRequest(string.Format(_localizer["AdapterMessageElementDeletedException"], id));
|
|
}
|
|
catch (StorageException ex)
|
|
{
|
|
_logger.LogError(ex, "StorageException");
|
|
return SaleOperationResponse.InternalServerError(string.Format(_localizer["AdapterMessageStorageException"], ex.InnerException!.Message));
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_logger.LogError(ex, "Exception");
|
|
return SaleOperationResponse.InternalServerError(ex.Message);
|
|
}
|
|
}
|
|
}
|