< Summary

Information
Class: MRA.Services.Backup.Export.ExportService
Assembly: MRA.Services
File(s): D:\a\MiguelRomerART\MiguelRomerART\MRA.Services\Backup\Export\ExportService.cs
Line coverage
30%
Covered lines: 16
Uncovered lines: 36
Coverable lines: 52
Total lines: 102
Line coverage: 30.7%
Branch coverage
0%
Covered branches: 0
Total branches: 6
Branch coverage: 0%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)100%11100%
ExportDrawings()0%4260%

File(s)

D:\a\MiguelRomerART\MiguelRomerART\MRA.Services\Backup\Export\ExportService.cs

#LineLine coverage
 1using Microsoft.Extensions.Logging;
 2using MRA.DTO.Models;
 3using MRA.Infrastructure.Settings;
 4using MRA.Services.Excel;
 5using MRA.Services.Excel.Interfaces;
 6using MRA.Services.Models.Drawings;
 7using MRA.Services.RemoteConfig;
 8using MRA.Services.Storage;
 9using OfficeOpenXml;
 10
 11namespace MRA.Services.Backup.Export;
 12
 13public class ExportService : IExportService
 14{
 15    private readonly ILogger<ExportService> _logger;
 16    private readonly IExcelService _excelService;
 17    private readonly IStorageService _storageService;
 18    private readonly IDrawingService _drawingService;
 19    private readonly IAppService _appService;
 20    private readonly AppSettings _appConfiguration;
 21
 122    public ExportService(
 123        ILogger<ExportService> logger,
 124        IExcelService excelService,
 125        IStorageService storageService,
 126        IRemoteConfigService remoteConfigService,
 127        IDrawingService drawingService,
 128        IAppService appService,
 129        AppSettings appConfiguration
 130        )
 31    {
 132        _appConfiguration = appConfiguration;
 133        _excelService = excelService;
 134        _logger = logger;
 135        _storageService = storageService;
 136        _drawingService = drawingService;
 137        _appService = appService;
 138    }
 39
 40
 41    public async Task ExportDrawings()
 42    {
 43        try
 44        {
 045            _logger.LogInformation("Iniciando Aplicación de Exportación");
 46
 47
 048            _logger.LogInformation("Leyendo documentos desde Firestore");
 49            List<DrawingModel> listDrawings;
 050            listDrawings = (await _drawingService.GetAllDrawingsAsync(onlyIfVisible: false)).ToList();
 51
 052            _logger.LogInformation("Calculando Popularidad");
 053            listDrawings = _appService.CalculatePopularityOfListDrawings(listDrawings).ToList();
 54
 055            _logger.LogInformation("Procediendo a crear Excel");
 56
 057            using (ExcelPackage excel = new ExcelPackage())
 58            {
 059                _logger.LogInformation($"Creando hoja principal \"{ExcelService.EXCEL_DRAWING_SHEET_NAME}\"");
 060                var workSheet = excel.Workbook.Worksheets.Add(ExcelService.EXCEL_DRAWING_SHEET_NAME);
 061                workSheet.View.FreezePanes(2, 2);
 62
 063                _logger.LogInformation("Obteniendo propiedades del DTO de Drawing");
 064                var drawingProperties = _excelService.GetPropertiesAttributes<DrawingModel>();
 65
 066                _logger.LogInformation("Rellenando tabla principal");
 067                _excelService.FillDrawingTable(ref workSheet, drawingProperties, listDrawings.OrderBy(x => x.Id).ToList(
 68
 069                _logger.LogInformation("Preparando hojas de diccionarios");
 070                _excelService.FillSheetsDictionary(excel, drawingProperties, workSheet);
 71
 072                _logger.LogInformation("Preparando fichero para guardar en Azure Storage");
 073                using (var memoryStream = new MemoryStream())
 74                {
 075                    var fileName = _excelService.GetFileName();
 76
 077                    await excel.SaveAsAsync(memoryStream);
 78
 079                    memoryStream.Position = 0; // Restablecer la posición del Stream al inicio
 80
 81                    try
 82                    {
 083                        var success = await _storageService.Save(memoryStream, _appConfiguration.AzureStorage.ExportLoca
 084                        if (!success)
 85                        {
 086                            _logger.LogError("File not saved to Storage");
 87                        }
 088                    }
 089                    catch (Exception ex)
 90                    {
 091                        _logger.LogError(ex, "Error al guardar el archivo '{FileName}' en Azure Storage", fileName);
 092                    }
 093                }
 094            }
 095        }
 096        catch (Exception ex)
 97        {
 098            _logger.LogError(ex, "Error durante la exportación");
 099        }
 0100        _logger.LogInformation("Fin de la Exportación en Azure Functions");
 0101    }
 102}