using Autofac; using Autofac.Core; using Csv; using Serilog; using Serilog.Core; using System; using System.Collections.Generic; using System.Configuration; using System.IO; using webapi.core; using webapi.Domain.Events; using webapi.Domain.SeedWork; using webapi.Infractructure; using webapi.Infractructure.Core; using webapi.Infractructure.Core.EventHandlers; using webapi.Infractructure.Repositories; using webapi.Infrastructure.Repositories; namespace ImportFlatTool { class Program { static void Main(string[] args) { if (args.Length < 3) { Console.WriteLine("Arguments should be set in following order: \"csvFileName\", \"pmsCondoName\", \"taloyhtioCondoUrl\""); Console.WriteLine("Press any key to close the console..."); Console.ReadKey(); return; } var csvFileName = args[0]; var pmsCondoName = args[1]; var taloyhtioCondoUrl = args[2]; if (string.IsNullOrEmpty(csvFileName)) { Console.WriteLine("The \"csvFileName\" could be empty"); Console.WriteLine("Press any key to close the console..."); Console.ReadKey(); return; } if (string.IsNullOrEmpty(pmsCondoName)) { Console.WriteLine("The \"pmsCondoName\" could be empty"); Console.WriteLine("Press any key to close the console..."); Console.ReadKey(); return; } if (string.IsNullOrEmpty(taloyhtioCondoUrl)) { Console.WriteLine("The \"taloyhtioCondoUrl\" could be empty"); Console.WriteLine("Press any key to close the console..."); Console.ReadKey(); return; } if (!File.Exists(csvFileName)) throw new FileNotFoundException($"File is not found {csvFileName}"); try { var builder = new ContainerBuilder(); ILogger log = Log.ForContext(); builder.RegisterType() .WithParameters(new List { new ResolvedParameter( (pi, ctx) => pi.ParameterType == typeof(ILifetimeScope), (pi, ctx) => ctx.Resolve()) }) .SingleInstance(); builder.RegisterType() .As() .WithParameters(new List { new ResolvedParameter( (pi, ctx) => pi.ParameterType == typeof(ILogger), (pi, ctx) => log), new ResolvedParameter( (pi, ctx) => pi.ParameterType == typeof(string) && pi.Name == "storageConnectionString", (pi, ctx) => ConfigurationManager.ConnectionStrings["AzureProdConnectionString"].ConnectionString), new ResolvedParameter( (pi, ctx) => pi.ParameterType == typeof(EventsHandlerFactory), (pi, ctx) => ctx.Resolve()), }) .SingleInstance(); builder.RegisterType() .As() .WithParameters(new List { new ResolvedParameter( (pi, ctx) => pi.ParameterType == typeof(ILogger), (pi, ctx) => log), new ResolvedParameter( (pi, ctx) => pi.ParameterType == typeof(string) && pi.Name == "storageConnectionString", (pi, ctx) => ConfigurationManager.ConnectionStrings["AzureProdConnectionString"].ConnectionString), new ResolvedParameter( (pi, ctx) => pi.ParameterType == typeof(EventsHandlerFactory), (pi, ctx) => ctx.Resolve()), }) .SingleInstance(); builder.RegisterType() .As>() .WithParameters(new List { new ResolvedParameter( (pi, ctx) => pi.ParameterType == typeof(ILogger), (pi, ctx) => log), new ResolvedParameter( (pi, ctx) => pi.ParameterType == typeof(IUserFlatMappingRepository), (pi, ctx) => ctx.Resolve()), new ResolvedParameter( (pi, ctx) => pi.ParameterType == typeof(IUserAuthRepository), (pi, ctx) => ctx.Resolve()), }) .SingleInstance(); ; var container = builder.Build(); var manager = new FlatManager(LogUtils._log, ConfigurationManager.ConnectionStrings["AzureProdConnectionString"].ConnectionString, ConfigurationManager.AppSettings["SPAPIUrl"], container); var csv = File.ReadAllText(csvFileName); foreach (var line in CsvReader.ReadFromText(csv, new CsvOptions() { HeaderMode = HeaderMode.HeaderAbsent })) { for (var i = 0; i < line.ColumnCount; i++) { LogUtils.LogDebug($"Importing of the flat \"{line[i]}\""); manager.ImportFlat(flatTitle: line[i], pmsCondoName, taloyhtioCondoUrl) .GetAwaiter().GetResult(); } } } catch (Exception ex) { LogUtils.LogError($"Error: {ex.Message} {ex.StackTrace} {ex.InnerException}"); Console.WriteLine("There is an error when importing."); Console.WriteLine("Press any key to close the console..."); Console.ReadKey(); return; } Console.WriteLine("All flats data has been imported."); Console.WriteLine("Press any key to close the console..."); Console.ReadKey(); } } }