80 lines
2.2 KiB
C#
80 lines
2.2 KiB
C#
using Microsoft.Extensions.Configuration;
|
|
using Microsoft.Extensions.Logging;
|
|
using PartSource.Automation.Models.Configuration;
|
|
using PartSource.Automation.Models.Enums;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Data;
|
|
using System.Data.SqlClient;
|
|
using System.Text;
|
|
|
|
namespace PartSource.Automation.Services
|
|
{
|
|
public class WhiSeoService
|
|
{
|
|
private readonly FtpService _ftpService;
|
|
private readonly string _connectionString;
|
|
private readonly ILogger<WhiSeoService> _logger;
|
|
|
|
public WhiSeoService(IConfiguration configuration, ILogger<WhiSeoService> logger)
|
|
{
|
|
FtpConfiguration ftpConfiguration = configuration.GetSection("FtpServers:WhiConfiguration").Get<FtpConfiguration>();
|
|
_ftpService = new FtpService(ftpConfiguration);
|
|
|
|
_connectionString = configuration.GetConnectionString("PartSourceDatabase");
|
|
|
|
_logger = logger;
|
|
}
|
|
|
|
public void GetFiles(SeoDataType seoDataType)
|
|
{
|
|
string seoDataTypeString = seoDataType.ToString().ToLowerInvariant();
|
|
string[] files = _ftpService.ListFiles(seoDataTypeString);
|
|
|
|
foreach (string file in files)
|
|
{
|
|
if (file.EndsWith("csv.gz"))
|
|
{
|
|
try
|
|
{
|
|
_ftpService.Download($"{seoDataTypeString}/{file}");
|
|
_logger.LogInformation($"Finished downloading {file}.");
|
|
}
|
|
|
|
|
|
catch (Exception ex)
|
|
{
|
|
_logger.LogWarning($"Failed to download {file}, quitting", ex);
|
|
throw;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
public void Truncate(SeoDataType seoDataType)
|
|
{
|
|
using SqlConnection connection = new SqlConnection(_connectionString);
|
|
connection.Open();
|
|
|
|
#pragma warning disable CA2100 // Review SQL queries for security vulnerabilities
|
|
using SqlCommand command = new SqlCommand($"TRUNCATE TABLE [{seoDataType}]", connection);
|
|
command.ExecuteNonQuery();
|
|
#pragma warning restore CA2100 // Review SQL queries for security vulnerabilities
|
|
}
|
|
|
|
public void BulkCopy(SeoDataType seoDataType, DataTable dataTable)
|
|
{
|
|
using SqlConnection connection = new SqlConnection(_connectionString);
|
|
connection.Open();
|
|
|
|
using SqlBulkCopy bulk = new SqlBulkCopy(connection)
|
|
{
|
|
DestinationTableName = seoDataType.ToString(),
|
|
BulkCopyTimeout = 14400
|
|
};
|
|
|
|
bulk.WriteToServer(dataTable);
|
|
}
|
|
}
|
|
}
|