204 lines
8.2 KiB
C#
204 lines
8.2 KiB
C#
using AutoMapper;
|
|
using DaisiesContracts.AdapterContracts;
|
|
using DaisiesContracts.AdapterContracts.OperationResponses;
|
|
using DaisiesContracts.BuisnessLogicContracts;
|
|
using DaisiesContracts.DataModels;
|
|
using DaisiesContracts.Exceptions;
|
|
using DaisiesContracts.ViewModels;
|
|
|
|
namespace DaisiesWebApi.Adapters;
|
|
|
|
public class ReportAdapter : IReportAdapter
|
|
{
|
|
|
|
private readonly IReportContract _reportContract;
|
|
private readonly ILogger _logger;
|
|
private readonly Mapper _mapper;
|
|
public ReportAdapter(IReportContract reportContract, ILogger logger)
|
|
{
|
|
_reportContract = reportContract;
|
|
_logger = logger;
|
|
var config = new MapperConfiguration(cfg =>
|
|
{
|
|
cfg.CreateMap<ProductProductHistoryDataModel, ProductProductHistoryViewModel>();
|
|
|
|
cfg.CreateMap<SaleDataModel, SaleViewModel>(); ;
|
|
cfg.CreateMap<SaleProductDataModel, SaleProductViewModel>();
|
|
cfg.CreateMap<ClientDiscountByPeriodDataModel, ClientDiscountByPeriodViewModel>();
|
|
cfg.CreateMap<ClientDiscountDataModel, ClientDiscountByPeriodDataModel>();
|
|
});
|
|
_mapper = new Mapper(config);
|
|
}
|
|
public async Task<ReportOperationResponse> CreateDocumentProductPricesByProductAsync(CancellationToken ct)
|
|
{
|
|
try
|
|
{
|
|
return SendStream(await _reportContract.CreateDocumentProductPricesByProductAsync(ct), "Producthistories.xslx");
|
|
}
|
|
catch (InvalidOperationException ex)
|
|
{
|
|
_logger.LogError(ex, "InvalidOperationException");
|
|
return ReportOperationResponse.InternalServerError($"Error while working with data storage: {ex.InnerException!.Message}");
|
|
}
|
|
catch (StorageException ex)
|
|
{
|
|
_logger.LogError(ex, "StorageException");
|
|
return ReportOperationResponse.InternalServerError($"Error while working with data storage: {ex.InnerException!.Message}");
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_logger.LogError(ex, "Exception");
|
|
return
|
|
ReportOperationResponse.InternalServerError(ex.Message);
|
|
}
|
|
}
|
|
|
|
public async Task<ReportOperationResponse> CreateDocumentSalesByPeriodAsync(DateTime dateStart, DateTime dateFinish, CancellationToken ct)
|
|
{
|
|
try
|
|
{
|
|
return SendStream(await _reportContract.CreateDocumentSalesByPeriodAsync(dateStart, dateFinish, ct),
|
|
"sales.xslx");
|
|
}
|
|
catch (IncorrectDatesException ex)
|
|
{
|
|
_logger.LogError(ex, "IncorrectDatesException");
|
|
return ReportOperationResponse.BadRequest($"Incorrect dates: {ex.Message}");
|
|
}
|
|
catch (InvalidOperationException ex)
|
|
{
|
|
_logger.LogError(ex, "InvalidOperationException");
|
|
return ReportOperationResponse.InternalServerError($"Error while working with data storage: {ex.InnerException!.Message}");
|
|
}
|
|
catch (StorageException ex)
|
|
{
|
|
_logger.LogError(ex, "StorageException");
|
|
return ReportOperationResponse.InternalServerError($"Error while working with data storage: {ex.InnerException!.Message}");
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_logger.LogError(ex, "Exception");
|
|
return
|
|
ReportOperationResponse.InternalServerError(ex.Message);
|
|
}
|
|
}
|
|
public async Task<ReportOperationResponse> GetDataProductPricesByProductAsync(CancellationToken ct)
|
|
{
|
|
try
|
|
{
|
|
return ReportOperationResponse.OK([.. (await _reportContract.GetDataProductPricesByProductAsync(ct)).Select(x => _mapper.Map<ProductProductHistoryViewModel>(x))]);
|
|
}
|
|
catch (InvalidOperationException ex)
|
|
{
|
|
_logger.LogError(ex, "InvalidOperationException");
|
|
return ReportOperationResponse.InternalServerError($"Error while working with data storage: {ex.InnerException!.Message} ");
|
|
}
|
|
catch (StorageException ex)
|
|
{
|
|
_logger.LogError(ex, "StorageException");
|
|
return ReportOperationResponse.InternalServerError($"Error while working with data storage: {ex.InnerException!.Message}");
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_logger.LogError(ex, "Exception");
|
|
return
|
|
ReportOperationResponse.InternalServerError(ex.Message);
|
|
}
|
|
}
|
|
|
|
public async Task<ReportOperationResponse> GetDataSaleByPeriodAsync(DateTime dateStart, DateTime dateFinish, CancellationToken ct)
|
|
{
|
|
try
|
|
{
|
|
return ReportOperationResponse.OK((await _reportContract.GetDataSaleByPeriodAsync(dateStart, dateFinish, ct)).Select(x =>
|
|
_mapper.Map<SaleViewModel>(x)).ToList());
|
|
}
|
|
catch (IncorrectDatesException ex)
|
|
{
|
|
_logger.LogError(ex, "IncorrectDatesException");
|
|
return ReportOperationResponse.BadRequest($"Incorrect dates: {ex.Message}");
|
|
}
|
|
catch (InvalidOperationException ex)
|
|
{
|
|
_logger.LogError(ex, "InvalidOperationException");
|
|
return ReportOperationResponse.InternalServerError($"Error while working with data storage: {ex.InnerException!.Message}");
|
|
}
|
|
catch (StorageException ex)
|
|
{
|
|
_logger.LogError(ex, "StorageException");
|
|
return ReportOperationResponse.InternalServerError($"Error while working with data storage: {ex.InnerException!.Message}");
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_logger.LogError(ex, "Exception");
|
|
return
|
|
ReportOperationResponse.InternalServerError(ex.Message);
|
|
}
|
|
}
|
|
|
|
private static ReportOperationResponse SendStream(Stream stream, string fileName)
|
|
{
|
|
stream.Position = 0;
|
|
return ReportOperationResponse.OK(stream, fileName);
|
|
}
|
|
|
|
public async Task<ReportOperationResponse> GetDataClientDiscountByPeriodAsync(DateTime dateStart, DateTime dateFinish, CancellationToken ct)
|
|
{
|
|
try
|
|
{
|
|
return ReportOperationResponse.OK((await _reportContract.GetDataDiscountByPeriodAsync(dateStart, dateFinish, ct))
|
|
.Select(x => _mapper.Map<ClientDiscountByPeriodViewModel>(x)).ToList());
|
|
}
|
|
catch (IncorrectDatesException ex)
|
|
{
|
|
_logger.LogError(ex, "IncorrectDatesException");
|
|
return ReportOperationResponse.BadRequest($"Incorrect dates: {ex.Message}");
|
|
}
|
|
catch (InvalidOperationException ex)
|
|
{
|
|
_logger.LogError(ex, "InvalidOperationException");
|
|
return ReportOperationResponse.InternalServerError($"Error while working with data storage: {ex.InnerException!.Message}");
|
|
}
|
|
catch (StorageException ex)
|
|
{
|
|
_logger.LogError(ex, "StorageException");
|
|
return ReportOperationResponse.InternalServerError($"Error while working with data storage: {ex.InnerException!.Message}");
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_logger.LogError(ex, "Exception");
|
|
return
|
|
ReportOperationResponse.InternalServerError(ex.Message);
|
|
}
|
|
}
|
|
|
|
public async Task<ReportOperationResponse> CreateDocumentClientDiscountByPeriodAsync(DateTime dateStart, DateTime dateFinish, CancellationToken ct)
|
|
{
|
|
try
|
|
{
|
|
return SendStream(await _reportContract.CreateDocumentDiscountByPeriodAsync(dateStart, dateFinish, ct), "discount.pdf");
|
|
}
|
|
catch (IncorrectDatesException ex)
|
|
{
|
|
_logger.LogError(ex, "IncorrectDatesException");
|
|
return ReportOperationResponse.BadRequest($"Incorrect dates: {ex.Message} ");
|
|
}
|
|
catch (InvalidOperationException ex)
|
|
{
|
|
_logger.LogError(ex, "InvalidOperationException");
|
|
return ReportOperationResponse.InternalServerError($"Error while working with data storage: {ex.InnerException!.Message}");
|
|
}
|
|
catch (StorageException ex)
|
|
{
|
|
_logger.LogError(ex, "StorageException");
|
|
return ReportOperationResponse.InternalServerError($"Error while working with data storage: {ex.InnerException!.Message}");
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_logger.LogError(ex, "Exception");
|
|
return
|
|
ReportOperationResponse.InternalServerError(ex.Message);
|
|
}
|
|
}
|
|
}
|