196 lines
7.1 KiB
C#
196 lines
7.1 KiB
C#
using AutoMapper;
|
|
using CatHasPawsContratcs.AdapterContracts;
|
|
using CatHasPawsContratcs.AdapterContracts.OperationResponses;
|
|
using CatHasPawsContratcs.BusinessLogicsContracts;
|
|
using CatHasPawsContratcs.DataModels;
|
|
using CatHasPawsContratcs.Exceptions;
|
|
using CatHasPawsContratcs.ViewModels;
|
|
|
|
namespace CatHasPawsWebApi.Adapters;
|
|
|
|
public class ReportAdapter : IReportAdapter
|
|
{
|
|
private readonly IReportContract _reportContract;
|
|
|
|
private readonly ILogger _logger;
|
|
|
|
private readonly Mapper _mapper;
|
|
|
|
public ReportAdapter(IReportContract reportContract, ILogger<ProductAdapter> logger)
|
|
{
|
|
_reportContract = reportContract;
|
|
_logger = logger;
|
|
var config = new MapperConfiguration(cfg =>
|
|
{
|
|
cfg.CreateMap<ManufacturerProductDataModel, ManufacturerProductViewModel>();
|
|
cfg.CreateMap<SaleDataModel, SaleViewModel>();
|
|
cfg.CreateMap<SaleProductDataModel, SaleProductViewModel>();
|
|
cfg.CreateMap<WorkerSalaryByPeriodDataModel, WorkerSalaryByPeriodViewModel>();
|
|
});
|
|
_mapper = new Mapper(config);
|
|
}
|
|
|
|
public async Task<ReportOperationResponse> GetDataProductsByManufacturerAsync(CancellationToken ct)
|
|
{
|
|
try
|
|
{
|
|
return ReportOperationResponse.OK([.. (await _reportContract.GetDataProductsByManufacturerAsync(ct)).Select(x => _mapper.Map<ManufacturerProductViewModel>(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.ToUniversalTime(), dateFinish.ToUniversalTime(), 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);
|
|
}
|
|
}
|
|
|
|
public async Task<ReportOperationResponse> GetDataSalaryByPeriodAsync(DateTime dateStart, DateTime dateFinish, CancellationToken ct)
|
|
{
|
|
try
|
|
{
|
|
return ReportOperationResponse.OK((await _reportContract.GetDataSalaryByPeriodAsync(dateStart.ToUniversalTime(), dateFinish.ToUniversalTime(), ct)).Select(x => _mapper.Map<WorkerSalaryByPeriodViewModel>(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> CreateDocumentProductsByManufacturerAsync(CancellationToken ct)
|
|
{
|
|
try
|
|
{
|
|
return SendStream(await _reportContract.CreateDocumentProductsByManufacturerAsync(ct), "products.docx");
|
|
}
|
|
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.ToUniversalTime(), dateFinish.ToUniversalTime(), 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> CreateDocumentSalaryByPeriodAsync(DateTime dateStart, DateTime dateFinish, CancellationToken ct)
|
|
{
|
|
try
|
|
{
|
|
return SendStream(await _reportContract.CreateDocumentSalaryByPeriodAsync(dateStart.ToUniversalTime(), dateFinish.ToUniversalTime(), ct), "salary.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);
|
|
}
|
|
}
|
|
|
|
private static ReportOperationResponse SendStream(Stream stream, string fileName)
|
|
{
|
|
stream.Position = 0;
|
|
return ReportOperationResponse.OK(stream, fileName);
|
|
}
|
|
} |