91 lines
3.5 KiB
C#
91 lines
3.5 KiB
C#
using Microsoft.Extensions.Configuration;
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
using Microsoft.Extensions.Logging;
|
|
using NfProcessorApp.Infrastructure;
|
|
using NfProcessorApp.Services;
|
|
using NfProcessorApp.Handlers;
|
|
using NfProcessorApp.Domain.Repositories;
|
|
|
|
namespace NfProcessorApp
|
|
{
|
|
public class Application
|
|
{
|
|
private readonly IFileScanner _scanner;
|
|
private readonly IFileHandlerFactory _handlerFactory;
|
|
private readonly IDatabaseUpdater _dbUpdater;
|
|
private readonly ILogger<Application> _logger;
|
|
private readonly IConfiguration _config;
|
|
|
|
#pragma warning disable IDE0290 // Usar construtor primário
|
|
public Application(
|
|
#pragma warning restore IDE0290 // Usar construtor primário
|
|
IFileScanner scanner,
|
|
IFileHandlerFactory handlerFactory,
|
|
IDatabaseUpdater dbUpdater,
|
|
ILogger<Application> logger,
|
|
IConfiguration config)
|
|
{
|
|
_scanner = scanner;
|
|
_handlerFactory = handlerFactory;
|
|
_dbUpdater = dbUpdater;
|
|
_logger = logger;
|
|
_config = config;
|
|
}
|
|
|
|
public static Application Build()
|
|
{
|
|
var config = new ConfigurationBuilder()
|
|
.AddJsonFile("appsettings.json", optional: false)
|
|
.Build();
|
|
|
|
var services = new ServiceCollection();
|
|
services.AddSingleton<IConfiguration>(config);
|
|
services.AddLogging(builder => builder.AddConsole());
|
|
services.AddTransient<IFileScanner, FileScanner>();
|
|
services.AddTransient<IFileHandlerFactory, FileHandlerFactory>();
|
|
services.AddTransient<ITERepository, AccessRepository>();
|
|
services.AddTransient<IDatabaseUpdater, AccessRepository>();
|
|
services.AddTransient<IValidadorNF, NFValidator>();
|
|
services.AddTransient<IFileHandler, XmlFileHandler>();
|
|
services.AddTransient<IFileHandler, PdfFileHandler>();
|
|
services.AddTransient<Application>();
|
|
services.AddTransient<XmlFileHandler>();
|
|
services.AddTransient<PdfFileHandler>();
|
|
|
|
var provider = services.BuildServiceProvider();
|
|
return provider.GetRequiredService<Application>();
|
|
}
|
|
|
|
public void Run()
|
|
{
|
|
var folder = _config["Settings:InputFolder"] ?? throw new InvalidOperationException("Settings:InputFolder não configurado.");
|
|
|
|
#pragma warning disable IDE0301 // Simplificar a inicialização de coleção
|
|
var extensions = _config.GetSection("Settings:Extensions").Get<string[]>() ?? Array.Empty<string>();
|
|
#pragma warning restore IDE0301 // Simplificar a inicialização de coleção
|
|
var files = _scanner.ListFiles(folder, extensions);
|
|
|
|
foreach (var file in files)
|
|
{
|
|
try
|
|
{
|
|
var handler = _handlerFactory.CreateHandler(file);
|
|
var result = handler.Process(file);
|
|
if (result.IsValid)
|
|
{
|
|
_dbUpdater.Update(result);
|
|
_logger.LogInformation("Updated NF {NumeroNF} from file {file}", result.NumeroNF, file);
|
|
}
|
|
else
|
|
{
|
|
_logger.LogWarning("Invalid NF in file {file}", file);
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_logger.LogError(ex, "Error processing file {file}", file);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
} |