forked from slavaxom9k/PIBD-23_Fomichev_V.S._MagicCarpet
195 lines
8.4 KiB
C#
195 lines
8.4 KiB
C#
using AutoMapper;
|
|
using MagicCarpetContracts.AdapterContracts;
|
|
using MagicCarpetContracts.AdapterContracts.OperationResponses;
|
|
using MagicCarpetContracts.BusinessLogicContracts;
|
|
using MagicCarpetContracts.DataModels;
|
|
using MagicCarpetContracts.Exceptions;
|
|
using MagicCarpetContracts.Mapper;
|
|
using MagicCarpetContracts.Resources;
|
|
using MagicCarpetContracts.ViewModels;
|
|
using Microsoft.Extensions.Localization;
|
|
|
|
namespace MagicCarpetWebApi.Adapters;
|
|
|
|
internal class ReportAdapter(IReportContract reportContract, IStringLocalizer<Messages> localizer, ILogger logger) : IReportAdapter
|
|
{
|
|
private readonly IReportContract _reportContract = reportContract;
|
|
|
|
private readonly IStringLocalizer<Messages> _localizer = localizer;
|
|
|
|
private readonly ILogger _logger = logger;
|
|
|
|
public async Task<ReportOperationResponse> CreateDocumentToursHistoryAsync(CancellationToken ct)
|
|
{
|
|
try
|
|
{
|
|
return SendStream(await _reportContract.CreateDocumentToursHistoryAsync(ct), "histories.xslx");
|
|
}
|
|
catch (InvalidOperationException ex)
|
|
{
|
|
_logger.LogError(ex, "InvalidOperationException");
|
|
return ReportOperationResponse.InternalServerError(string.Format(_localizer["AdapterMessageInvalidOperationException"], ex.InnerException!.Message));
|
|
}
|
|
catch (StorageException ex)
|
|
{
|
|
_logger.LogError(ex, "StorageException");
|
|
return ReportOperationResponse.InternalServerError(string.Format(_localizer["AdapterMessageStorageException"], 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(string.Format(_localizer["AdapterMessageIncorrectDatesException"], ex.Message));
|
|
}
|
|
catch (InvalidOperationException ex)
|
|
{
|
|
_logger.LogError(ex, "InvalidOperationException");
|
|
return ReportOperationResponse.InternalServerError(string.Format(_localizer["AdapterMessageInvalidOperationException"], ex.InnerException!.Message));
|
|
}
|
|
catch (StorageException ex)
|
|
{
|
|
_logger.LogError(ex, "StorageException");
|
|
return ReportOperationResponse.InternalServerError(string.Format(_localizer["AdapterMessageStorageException"], ex.InnerException!.Message));
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_logger.LogError(ex, "Exception");
|
|
return
|
|
ReportOperationResponse.InternalServerError(ex.Message);
|
|
}
|
|
}
|
|
|
|
public async Task<ReportOperationResponse> GetDataToursHistoryAsync(CancellationToken ct)
|
|
{
|
|
try
|
|
{
|
|
return ReportOperationResponse.OK([.. (await _reportContract.GetDataToursHistoryAsync(ct)).Select(x => CustomMapper.MapObject<TourAndTourHistoryViewModel>(x))]);
|
|
}
|
|
catch (InvalidOperationException ex)
|
|
{
|
|
_logger.LogError(ex, "InvalidOperationException");
|
|
return ReportOperationResponse.InternalServerError(string.Format(_localizer["AdapterMessageInvalidOperationException"], ex.InnerException!.Message));
|
|
}
|
|
catch (StorageException ex)
|
|
{
|
|
_logger.LogError(ex, "StorageException");
|
|
return ReportOperationResponse.InternalServerError(string.Format(_localizer["AdapterMessageStorageException"], ex.InnerException!.Message));
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_logger.LogError(ex, "Exception");
|
|
return
|
|
ReportOperationResponse.InternalServerError(ex.Message);
|
|
}
|
|
}
|
|
|
|
public async Task<ReportOperationResponse> GetDataBySalesByPeriodAsync(DateTime dateStart, DateTime dateFinish, CancellationToken ct)
|
|
{
|
|
try
|
|
{
|
|
return ReportOperationResponse.OK((await _reportContract.GetDataBySalesAsync(dateStart.ToUniversalTime(), dateFinish.ToUniversalTime(), ct)).Select(x =>
|
|
CustomMapper.MapObject<SaleViewModel>(x)).ToList());
|
|
}
|
|
catch (IncorrectDatesException ex)
|
|
{
|
|
_logger.LogError(ex, "IncorrectDatesException");
|
|
return ReportOperationResponse.BadRequest(string.Format(_localizer["AdapterMessageIncorrectDatesException"], ex.Message));
|
|
}
|
|
catch (InvalidOperationException ex)
|
|
{
|
|
_logger.LogError(ex, "InvalidOperationException");
|
|
return ReportOperationResponse.InternalServerError(string.Format(_localizer["AdapterMessageInvalidOperationException"], ex.InnerException!.Message));
|
|
}
|
|
catch (StorageException ex)
|
|
{
|
|
_logger.LogError(ex, "StorageException");
|
|
return ReportOperationResponse.InternalServerError(string.Format(_localizer["AdapterMessageStorageException"], 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> GetDataSalaryByPeriodAsync(DateTime dateStart, DateTime dateFinish, CancellationToken ct)
|
|
{
|
|
try
|
|
{
|
|
return ReportOperationResponse.OK((await _reportContract.GetDataSalaryByPeriodAsync(dateStart.ToUniversalTime(), dateFinish.ToUniversalTime(), ct))
|
|
.Select(x => CustomMapper.MapObject<EmployeeSalaryByPeriodViewModel>(x)).ToList());
|
|
}
|
|
catch (IncorrectDatesException ex)
|
|
{
|
|
_logger.LogError(ex, "IncorrectDatesException");
|
|
return ReportOperationResponse.BadRequest(string.Format(_localizer["AdapterMessageIncorrectDatesException"], ex.Message));
|
|
}
|
|
catch (InvalidOperationException ex)
|
|
{
|
|
_logger.LogError(ex, "InvalidOperationException");
|
|
return ReportOperationResponse.InternalServerError(string.Format(_localizer["AdapterMessageInvalidOperationException"], ex.InnerException!.Message));
|
|
}
|
|
catch (StorageException ex)
|
|
{
|
|
_logger.LogError(ex, "StorageException");
|
|
return ReportOperationResponse.InternalServerError(string.Format(_localizer["AdapterMessageStorageException"], 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(string.Format(_localizer["AdapterMessageIncorrectDatesException"], ex.Message));
|
|
}
|
|
catch (InvalidOperationException ex)
|
|
{
|
|
_logger.LogError(ex, "InvalidOperationException");
|
|
return ReportOperationResponse.InternalServerError(string.Format(_localizer["AdapterMessageInvalidOperationException"], ex.InnerException!.Message));
|
|
}
|
|
catch (StorageException ex)
|
|
{
|
|
_logger.LogError(ex, "StorageException");
|
|
return ReportOperationResponse.InternalServerError(string.Format(_localizer["AdapterMessageStorageException"], ex.InnerException!.Message));
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_logger.LogError(ex, "Exception");
|
|
return
|
|
ReportOperationResponse.InternalServerError(ex.Message);
|
|
}
|
|
}
|
|
}
|