Beispiel #1
0
        /// <summary>
        /// Compare parsing list with list from DB.
        /// If these are diff, will update DB list
        /// </summary>
        public void UpdateCompanies()
        {
            _logger.Information("{@Items} start: {function}", new[] { LoggingDirectories.Drom.ToString() }, MethodBase.GetCurrentMethod().Name);

            var topDropMenuPosition = Console.GetCursorPosition().Top;
            var taskCarCompanies    = ParseCompaniesAsync();

            ConsoleExtension.ShowLoading(EnumExtensions.GetEnumDescription(WaitLines.Loading), taskCarCompanies, ConsoleColor.Green);
            ConsoleExtension.ClearCurrentConsoleLine(topPosition: topDropMenuPosition);

            var taskCarCompaniesFromDb = _uow.CompanyRepository.GetAllAsync();

            ConsoleExtension.ShowLoading(EnumExtensions.GetEnumDescription(WaitLines.Synchronize), taskCarCompaniesFromDb, ConsoleColor.Yellow, ConsoleColor.Black);
            ConsoleExtension.ClearCurrentConsoleLine(topPosition: topDropMenuPosition);

            var carCompaniesFromDb = taskCarCompaniesFromDb.Result;

            foreach (var company in _companiesInfo)
            {
                if (!carCompaniesFromDb.Any(x => x.Name == company.Key))
                {
                    _uow.CompanyRepository.Add(new Company()
                    {
                        Name       = company.Key,
                        ModelUrl   = company.Value,
                        IsFromDrom = true
                    });
                }
            }

            var taskSave = _uow.Commit();

            ConsoleExtension.ShowLoading(EnumExtensions.GetEnumDescription(WaitLines.Saving), taskSave, ConsoleColor.Green);
            ConsoleExtension.ClearCurrentConsoleLine(topPosition: topDropMenuPosition);
        }
Beispiel #2
0
        /// <summary>
        /// User sets needed act number
        /// </summary>
        /// <param name="menuLevels">Dictionary with menu levels</param>
        /// <param name="topCursorPosition">Cursor Position to clear point</param>
        /// <returns>act number or quit</returns>
        private static short GetActionNumber <T>(Dictionary <T, bool> menuLevels, int topCursorPosition)
        {
            _logger.Information("{@Items} start: {function}", new[] { LoggingDirectories.Menu.ToString() }, MethodBase.GetCurrentMethod().Name);

            short?actNumber = null;

            int leftPosition = 0;
            int topPosition  = 0;

            string message = "Enter the act number: ";

            Console.Write(message);

            leftPosition = message.Length;
            topPosition  = Console.GetCursorPosition().Top;

            while (actNumber == null)
            {
                try
                {
                    actNumber = short.Parse(Console.ReadLine());

                    if (actNumber > menuLevels.Count() || actNumber < 1)
                    {
                        throw new OverflowException($"act number should been between 1 and {menuLevels.Count()}");
                    }
                }
                catch (Exception ex)
                {
                    if (ex as FormatException != null)
                    {
                        ex = ex as FormatException;
                    }
                    else if (ex as ArgumentNullException != null)
                    {
                        ex = ex as ArgumentNullException;
                    }
                    else if (ex as OverflowException != null)
                    {
                        ex = ex as OverflowException;
                    }

                    ConsoleExtension.ShowLoading(ex.Message, backgroundColor: ConsoleColor.Red, foregroundColor: ConsoleColor.Black);
                    actNumber = null;
                    ConsoleExtension.ClearCurrentConsoleLine(leftPosition, topPosition);

                    _logger.Warning("{@Items} function: {function}\nexception: {exception}",
                                    new[] { LoggingDirectories.Menu.ToString() },
                                    MethodBase.GetCurrentMethod().Name,
                                    ex.Message);
                }
            }

            ConsoleExtension.ClearCurrentConsoleLine(topPosition: topCursorPosition);
            return(actNumber ?? (short)BaseMenuLevels.Quit);
        }
Beispiel #3
0
        /// <summary>
        /// Parse and add companies information
        /// </summary>
        private static void UpdateCompaniesInfo()
        {
            _logger.Information("{@Items} start: {function}", new[] { LoggingDirectories.Menu.ToString() }, MethodBase.GetCurrentMethod().Name);

            while (_dropMenuLevels.GetValueOrDefault(DromOptions.UpdateCompaniesInfo))
            {
                _drom.UpdateCompanies();
                Console.WriteLine("done!");
                ConsoleExtension.ShowLoading(EnumExtensions.GetEnumDescription(WaitLines.PressAnyKey), backgroundColor: ConsoleColor.DarkGreen, needClearConsole: false);
                GetUserEntersKey();
                _dropMenuLevels[DromOptions.UpdateCompaniesInfo] = false;
            }
            ConsoleExtension.ClearCurrentConsoleLine(topPosition: _topDropMenuPosition);
        }
Beispiel #4
0
        /// <summary>
        /// Shows info's section menu
        /// </summary>
        private static void ShowInfoSection()
        {
            _logger.Information("start: {function}", MethodBase.GetCurrentMethod().Name);

            while (_baseMenuLevels.GetValueOrDefault(BaseMenuLevels.Info))
            {
                Console.WriteLine("this app was developed for educational purposes.\n"
                                  + "it parses sites and saves the data to a local database.\n"
                                  + "all data is taken from open sources.\n");

                ConsoleExtension.ShowLoading(EnumExtensions.GetEnumDescription(WaitLines.PressAnyKey), backgroundColor: ConsoleColor.DarkGreen);
                GetUserEntersKey();
                _baseMenuLevels[BaseMenuLevels.Info] = false;
            }

            ConsoleExtension.ClearCurrentConsoleLine(topPosition: _topMenuPosition);
        }
Beispiel #5
0
        /// <summary>
        /// logic to show information (companies info or models info)
        /// </summary>
        /// <typeparam name="T">Company/CarModels</typeparam>
        /// <param name="callBack">func to show info to console</param>
        /// <param name="getCollectionFunction">async func to get some collections</param>
        private static void ShowInfo <T>(Action <IQueryable <T> > callBack, Func <Task <IQueryable <T> > > getCollectionFunction)
        {
            _logger.Information("{@Items} start: {function}", new[] { LoggingDirectories.Menu.ToString() }, MethodBase.GetCurrentMethod().Name);

            while (_dropMenuLevels.GetValueOrDefault(DromOptions.ShowCompaniesInfo))
            {
                ConsoleExtension.ClearCurrentConsoleLine(topPosition: _topDropMenuPosition);

                var taskAllCompanies = getCollectionFunction();

                ConsoleExtension.ShowLoading(EnumExtensions.GetEnumDescription(WaitLines.Loading), taskAllCompanies, ConsoleColor.Green);
                ConsoleExtension.ClearCurrentConsoleLine(topPosition: _topDropMenuPosition);

                callBack(taskAllCompanies.Result);

                _dropMenuLevels[DromOptions.ShowCompaniesInfo] = false;
            }

            ConsoleExtension.ClearCurrentConsoleLine(topPosition: _topDropMenuPosition);
        }
Beispiel #6
0
        /// <summary>
        /// logic to show companies info
        /// shows company info
        /// </summary>
        /// <param name="companies">list of companies</param>
        private static void ShowCompanies(IQueryable <Company> companies)
        {
            _logger.Information("{@Items} start: {function}", new[] { LoggingDirectories.Menu.ToString() }, MethodBase.GetCurrentMethod().Name);

            if (!companies.Any())
            {
                Console.WriteLine("There is no any companies");
                return;
            }

            int       i = 1;
            int       additionalI;
            int       page           = 0;
            const int pageSize       = 5;
            int       companiesCount = companies.Count();
            int       maxPage        = companiesCount / pageSize == 0
                        ? companiesCount / pageSize
                        : companiesCount / pageSize + 1;

            var                    bindNumbersAndCompaniesId = new Dictionary <char, Guid>();
            List <Company>         companies5 = new List <Company>();
            Task <List <Company> > companies5Task;
            ConsoleKey             key = ConsoleKey.Z;

            while (key != ConsoleKey.Q)
            {
                companies5Task = Task.Run(() => GetPage <Company>(companies, page, pageSize));

                ConsoleExtension.ShowLoading(EnumExtensions.GetEnumDescription(WaitLines.Loading),
                                             companies5Task,
                                             ConsoleColor.Green,
                                             ConsoleColor.Black);

                ConsoleExtension.ClearCurrentConsoleLine(topPosition: _topDropMenuPosition);

                companies5.AddRange(companies5Task.Result);


                additionalI = 1;
                foreach (var company in companies5)
                {
                    Console.WriteLine($"[{i} : {additionalI}]");
                    Console.WriteLine($"id:        {company.Id}");
                    Console.WriteLine($"company:   {company.Name}");
                    Console.WriteLine($"model url: {company.ModelUrl}\n");
                    bindNumbersAndCompaniesId.Add(additionalI.ToString().First(), company.Id);
                    i++;
                    additionalI++;
                }

                Console.WriteLine($"Press:\n"
                                  + $"- 'N' to show next {pageSize} companies\n"
                                  + $"- 'P' to show previous {pageSize} companies\n"
                                  + $"- Press number beetwen {1} and {pageSize}\n"
                                  + $"  to view the company car models\n"
                                  + $"- 'Q' Exit\n");

                while (key != ConsoleKey.N &&
                       key != ConsoleKey.P &&
                       key != ConsoleKey.Q &&
                       !bindNumbersAndCompaniesId.Keys.Any(k => k == (int)key))
                {
                    key = Console.ReadKey(true).Key;
                }

                if (key == ConsoleKey.N &&
                    (page + 1 < maxPage))
                {
                    page += 1;
                }
                else if (key == ConsoleKey.P &&
                         (page - 1 >= 0))
                {
                    page -= 1;
                }
                else if (bindNumbersAndCompaniesId.Keys.Any(k => k == (int)key))
                {
                    ShowModelsInfo(bindNumbersAndCompaniesId.Where(k => k.Key == (int)key).Select(g => g.Value).First());
                }

                i = page * pageSize + 1;

                if (key != ConsoleKey.Q)
                {
                    key = ConsoleKey.Z;
                    bindNumbersAndCompaniesId.Clear();
                    companies5.Clear();
                }
                ConsoleExtension.ClearCurrentConsoleLine(topPosition: _topDropMenuPosition);
            }
        }
Beispiel #7
0
        /// <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);
            }
        }
Beispiel #8
0
        /// <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);
        }