114 lines
4.4 KiB
C#
114 lines
4.4 KiB
C#
using AutoMapper;
|
|
using SmallSoftwareContracts.AdapterContracts;
|
|
using SmallSoftwareContracts.AdapterContracts.OperationResponses;
|
|
using SmallSoftwareContracts.BusinessLogicsContracts;
|
|
using SmallSoftwareContracts.DataModels;
|
|
using SmallSoftwareContracts.Exceptions;
|
|
using SmallSoftwareContracts.ViewModels;
|
|
|
|
namespace SmallSoftwareWebApi.Adapters;
|
|
|
|
public class ReportAdapter : IReportAdapter
|
|
{
|
|
|
|
private readonly IReportContract _reportContract;
|
|
private readonly ILogger _logger;
|
|
private readonly Mapper _mapper;
|
|
public ReportAdapter(IReportContract reportContract, ILogger<SoftwareAdapter> logger)
|
|
{
|
|
_reportContract = reportContract;
|
|
_logger = logger;
|
|
var config = new MapperConfiguration(cfg =>
|
|
{
|
|
cfg.CreateMap<HistoryOfSoftwareDataModel, HistoryOfSoftwareViewModel>();
|
|
cfg.CreateMap<RequestDataModel, RequestViewModel>();
|
|
cfg.CreateMap<InstallationRequestDataModel, InstallationRequestViewModel>();
|
|
});
|
|
_mapper = new Mapper(config);
|
|
|
|
}
|
|
public async Task<ReportOperationResponse> GetDataSoftwaresByManufacturerAsync(CancellationToken ct)
|
|
{
|
|
try
|
|
{
|
|
return ReportOperationResponse.OK([.. (await _reportContract
|
|
.GetDataSoftwaresByHistoryAsync(ct)).Select(x => _mapper.Map<HistoryOfSoftwareViewModel>(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> CreateDocumentSoftwaresByManufacturerAsync(CancellationToken ct)
|
|
{
|
|
try
|
|
{
|
|
var stream = await _reportContract.CreateDocumentSoftwaresByHistoryAsync(ct);
|
|
stream.Position = 0;
|
|
return ReportOperationResponse.OK(stream, "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> GetDataRequestByPeriodAsync(DateTime dateStart, DateTime dateFinish, CancellationToken ct)
|
|
{
|
|
try
|
|
{
|
|
return ReportOperationResponse.OK((await _reportContract.GetDataRequestByPeriodAsync(dateStart, dateFinish, ct)).Select(x =>
|
|
_mapper.Map<RequestViewModel>(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);
|
|
}
|
|
|
|
}
|
|
}
|