/// <summary> /// logic to show models info /// </summary> /// <param name="companyId">company id</param> private static void ShowModelsInfo(Guid companyId) { _logger.Information("{@Items} start: {function}", new[] { LoggingDirectories.Menu.ToString() }, MethodBase.GetCurrentMethod().Name); var carModelsTask = _drom.GetModelsByCompanyId(companyId); ConsoleExtension.ShowLoading(EnumExtensions.GetEnumDescription(WaitLines.Synchronize), carModelsTask, ConsoleColor.Yellow, ConsoleColor.Black); ConsoleExtension.ClearCurrentConsoleLine(topPosition: _topDropMenuPosition); var carModels = carModelsTask.Result; int i = 1; int page = 0; const int pageSize = 5; int companiesCount = carModels.Count(); int maxPage = companiesCount / pageSize == 0 ? companiesCount / pageSize : companiesCount / pageSize + 1; List <CarModel> carModels5 = new List <CarModel>(); Task <List <CarModel> > carModels5Task; ConsoleKey key = ConsoleKey.Z; while (key != ConsoleKey.Q) { carModels5Task = Task.Run(() => GetPage <CarModel>(carModels, page, pageSize)); ConsoleExtension.ShowLoading(EnumExtensions.GetEnumDescription(WaitLines.Loading), carModels5Task, ConsoleColor.Green, ConsoleColor.Black); ConsoleExtension.ClearCurrentConsoleLine(topPosition: _topDropMenuPosition); carModels5.AddRange(carModels5Task.Result); foreach (var model in carModels5) { Console.WriteLine($"[{i}]"); Console.WriteLine($"id: {model.Id}"); Console.WriteLine($"Name: {model.Name}"); i++; } Console.WriteLine($"Press:\n" + $"- 'N' to show next {pageSize} models\n" + $"- 'P' to show previous {pageSize} models\n" + $"- 'Q' Exit\n"); while (key != ConsoleKey.N && key != ConsoleKey.P && key != ConsoleKey.Q) { key = Console.ReadKey(true).Key; } if (key == ConsoleKey.N && (page + 1 < maxPage)) { page += 1; } else if (key == ConsoleKey.P && (page - 1 >= 0)) { page -= 1; } i = page * pageSize + 1; if (key != ConsoleKey.Q) { key = ConsoleKey.Z; carModels5.Clear(); } ConsoleExtension.ClearCurrentConsoleLine(topPosition: _topDropMenuPosition); } }
public void Show() { Console.WriteLine("Example 5\n\n"); Console.WriteLine("Example shows \"QuerySingle\" method to select data from Address table:\n\n"); try { Console.WriteLine("First, answer the question\n"); bool result = false;; do { Console.WriteLine("The question is: What will happen in case if we use QuerySingle method on more than 1 records?"); Console.WriteLine("Press 1 if it will return null, press 2 if it will return an exception and press 3 if it will return any single of found records"); var dapperQuestionAnswer = Console.ReadKey().KeyChar; result = new QuestionAnswerHandler(EQuestionType.QuerySingleQuestion) .HandleAnswer(dapperQuestionAnswer); Console.WriteLine(); }while (!result); _initializationRepository.Initialize(); Console.WriteLine("QuerySingle method execution, press any key to begin:"); Console.ReadKey(); Console.WriteLine("\nSingle object if found more than 1 records:"); try { var singleIfMany = _repository.GetSingleAddress(); ConsoleExtension.WriteObject(singleIfMany); } catch (Exception exception) { Console.WriteLine($"QuerySingle method has thrown an exception: \n\n{exception.Message}"); } Console.WriteLine("\nSecond case, press any key to proceed:"); Console.ReadKey(); Console.WriteLine("\n"); Console.WriteLine("Clearing db table"); _repository.ClearAllAddressData(); Console.WriteLine("Db table cleared"); _repository.AddSingleFakeAddress(); Console.WriteLine("Added single fake address to Address table"); Console.WriteLine("\nSingle object if single record found:"); try { var single = _repository.GetSingleAddress(); ConsoleExtension.WriteObject(single); } catch (Exception exception) { Console.WriteLine($"QuerySingle method has thrown an exception: \n\n{exception.Message}"); } Console.WriteLine("\nThird case, press any key to proceed:"); Console.ReadKey(); Console.WriteLine("\n"); Console.WriteLine("Clearing db table"); _repository.ClearAllAddressData(); Console.WriteLine("Db table cleared"); Console.WriteLine("\nSingle object if no records found:"); try { var singleIfEmpty = _repository.GetSingleAddress(); ConsoleExtension.WriteObject(singleIfEmpty); } catch (Exception exception) { Console.WriteLine($"QuerySingle method has thrown an exception: \n\n{exception.Message}"); } Console.WriteLine("\n\nReinitializing db tables"); _initializationRepository.Initialize(); Console.WriteLine("Db tables reinitialized"); } catch (Exception exception) { Console.WriteLine(ConnectionStore.ConnectionErrorProvider(exception.Message)); } GetBackChooser.GetBack(); }
public static void Execute() { ConsoleExtension.WriteSeparator("Shareholders example - before"); int totalNumberOfShares = 1000; Console.WriteLine($"Total number of shares is: {totalNumberOfShares}!"); var james = new Person { Name = "James" }; var mary = new Person { Name = "Mary" }; var emily = new Person { Name = "Emily" }; var sophia = new Person { Name = "Sophia" }; var johan = new Person { Name = "Johan" }; var nikola = new Person { Name = "Nikola" }; var frenk = new Person { Name = "Frenk" }; var bigCorporation = new Corporation { Name = "Big corporation", Shareholders = { james, mary, emily, sophia, johan }, }; var independentShareholders = new List <Person> { nikola, frenk }; var corporations = new List <Corporation> { bigCorporation }; var totalToSplitBy = independentShareholders.Count + corporations.Count; var sharesForEachParty = totalNumberOfShares / totalToSplitBy; var remainingShares = totalNumberOfShares % totalToSplitBy; // Divide shares between independent shareholders foreach (var shareholder in independentShareholders) { shareholder.NumberOfShares = sharesForEachParty + remainingShares; remainingShares = 0; shareholder.PrintReport(); } // Divide shares between shareholders within corporation foreach (var corporation in corporations) { var sharesForEachShareholderInCorporation = sharesForEachParty / corporation.Shareholders.Count; var remainingSharesForCorporation = sharesForEachParty % corporation.Shareholders.Count; foreach (var shareholder in corporation.Shareholders) { shareholder.NumberOfShares = sharesForEachShareholderInCorporation + remainingSharesForCorporation; remainingSharesForCorporation = 0; shareholder.PrintReport(); } } }
public static void DownloadMissingFiles(InstallationDataModel version) { _client = new TcpClient(_ip, _port); if (version.MissingFiles.Count == 0) { version = ChecksumTool.RecheckVersion(version); RequestVersionMissingFiles(ref version); } DownloadProgressEventArgs args = new DownloadProgressEventArgs(); PatchDataModel model = new PatchDataModel() { RequestType = PatchNetworkRequest.DownloadFile, InstalledVersion = new InstallationDataModel { VersionName = version.VersionName, VersionBranch = version.VersionBranch } }; args.Version = version.VersionBranch; args.TotalSize = version.TotalSize; //foreach (var item in version.MissingFiles) { // args.TotalSize += item.Size; //} _downloadingFiles = true; //While there's still files missing and there's still an active connection while (version.MissingFiles.Count > 0 && ConnectionHandler.Connected(_client)) { //Raise download progress event args.NextFileName = version.MissingFiles[0].FilePath; OnDownloadProgress(args); //Request file model.File = new FileModel() { FilePath = version.MissingFiles[0].FilePath }; ConnectionHandler.SendObject(model, _client); //Wait for response while (!_client.GetStream().DataAvailable) { Thread.Sleep(16); } //Handle incoming file // await ConnectionHandler.ReadFileAsync(_client, version.MissingFiles[0].FilePath, InstallPath + '/' + version.VersionName); ConnectionHandler.ReadFile(_client, version.MissingFiles[0].FilePath, version.InstallPath); Console.WriteLine(ConsoleExtension.AddTimestamp(version.MissingFiles[0].FilePath + " downloaded")); args.DownloadedTotal += version.MissingFiles[0].Size; lock (version) version.MissingFiles.RemoveAt(0); } _downloadingFiles = false; Console.WriteLine(ConsoleExtension.AddTimestamp("All missing files received!")); version = ChecksumTool.RecheckVersion(version); RequestVerifyVersion(ref version); _client.GetStream().Close(); _client.Close(); DownloadDone?.Invoke(version); }
/// <summary> /// Update car models info /// </summary> public void UpdateModels(int topCursorPosition) { _logger.Information("{@Items} start: {function}", new[] { LoggingDirectories.Drom.ToString() }, MethodBase.GetCurrentMethod().Name); var companiesTask = _uow.CompanyRepository.GetAllModelsURLFromDrom(); ConsoleExtension.ShowLoading( EnumExtensions.GetEnumDescription(WaitLines.Loading), companiesTask, ConsoleColor.Green, ConsoleColor.Black); ConsoleExtension.ClearCurrentConsoleLine(topPosition: topCursorPosition); var companies = companiesTask.Result.ToList(); var companiesCount = companies.Count(); if (!companies.Any()) { throw new ArgumentNullException("there is no models urls"); } List <Tuple <string, Guid> > carModels = new List <Tuple <string, Guid> >(); int i = 1; var mainParseTask = Task.Run(() => { foreach (var company in companies) { var htmlDoc = GetHtmlDocument(company.ModelUrl); carModels.AddRange(ParseModelsInfo(htmlDoc, company.Id)); carModels.AddRange(ParseNoScriptInfoForModels(htmlDoc, company.Id)); i++; } }); while (!mainParseTask.IsCompleted) { ConsoleExtension.ShowLoading( (EnumExtensions.GetEnumDescription(WaitLines.Parsing) + "..." + $"{(i * 100 / companiesCount)}%"), backgroundColor: ConsoleColor.Gray, foregroundColor: ConsoleColor.Black); ConsoleExtension.ClearCurrentConsoleLine(topPosition: topCursorPosition); } var carModelsFromDBTask = _uow.CarModelRepository.GetCarModelsFromDromAsync(); ConsoleExtension.ShowLoading( (EnumExtensions.GetEnumDescription(WaitLines.Synchronize)), carModelsFromDBTask, ConsoleColor.Yellow, ConsoleColor.Black); ConsoleExtension.ClearCurrentConsoleLine(topPosition: topCursorPosition); var carModelsFromDB = carModelsFromDBTask.Result; var synchronize = Task.Run(() => { foreach (var carModel in carModels) { var tempCarModel = carModelsFromDB.Where(c => c.Name == carModel.Item1).FirstOrDefault(); if (tempCarModel == null) { var newCarModel = new CarModel() { Name = carModel.Item1, CompanyId = carModel.Item2, }; try { _uow.CarModelRepository.Add(newCarModel); _uow.Commit().Wait(); } catch (Exception ex) { _logger.Error("{@Items} function: {function}\nexception: {exception}", new[] { LoggingDirectories.Drom.ToString() }, MethodBase.GetCurrentMethod().Name, ex.Message); } } } }); ConsoleExtension.ShowLoading( (EnumExtensions.GetEnumDescription(WaitLines.Synchronize)), synchronize, ConsoleColor.Yellow, ConsoleColor.Black); ConsoleExtension.ClearCurrentConsoleLine(topPosition: topCursorPosition); }