Beispiel #1
0
        public Menu GetTodaysMenu()
        {
            var todaysDate = dateProvider.GetDate();
            var savedMenu  = menuDao.GetMenuByDate(todaysDate);

            if (savedMenu != null && savedMenu.Meals != null && savedMenu.Meals.Any())
            {
                return(savedMenu);
            }

            logger.LogInformation("Menu is not saved. Scraping web site");
            var scrapedMenu = webScraper.ScrapeForMenu();

            if (scrapedMenu.Date.Date != todaysDate.Date)
            {
                logger.LogInformation("Scraped menu doesn't have same date as today's date");
                return(new Menu {
                    Date = todaysDate, Meals = new List <Meal>()
                });
            }

            if (scrapedMenu != null && scrapedMenu.Meals != null && scrapedMenu.Meals.Any())
            {
                scrapedMenu = menuDao.Save(scrapedMenu);
            }

            return(scrapedMenu);
        }
Beispiel #2
0
        public void AddOperation(double amount)
        {
            var balance = this.GetBalance() + amount;

            operations.Add(new Operation {
                Amount = amount, Date = dateProvider.GetDate(), Balance = balance
            });
        }
Beispiel #3
0
        /// <summary>
        /// Parses date from heading. Expected format is: NAZEV_DNE CISLO_DNE.NAZEV_MESICE ROK
        /// </summary>
        /// <param name="headingDate"></param>
        /// <returns></returns>
        public DateTime ParseHeadingDate(string headingDate)
        {
            var tokens  = LoadTokensFromString(headingDate);
            var dayName = ""; // not used at this time

            if (tokens[0].IsWord())
            {
                dayName = tokens[0].representation;
            }

            int index = 0;

            while (index < tokens.Count && tokens[index].IsWord())
            {
                index++;
            }

            if (index == tokens.Count)
            {
                var msg = $"Heading date '{headingDate}' doesn't contain number of day.";
                logger.LogError(msg);
                throw new ArgumentException(msg);
            }

            var day = tokens[index].GetNumber();

            index++;
            if (index == tokens.Count)
            {
                var msg = $"Heading date '{headingDate}' doesn't contain month.";
                logger.LogError(msg);
                throw new ArgumentException(msg);
            }

            int month;

            if (tokens[index].IsNumber())
            {
                month = tokens[index].GetNumber();
            }
            else
            {
                var monthRepresentation = tokens[index].representation;
                try {
                    month = monthConvertor.convertMonth(monthRepresentation);
                } catch (KeyNotFoundException) {
                    var defaultMonth = defaultDateProvider.GetDate().Month;
                    logger.LogWarning($"Month '{monthRepresentation}' could not be converted. Heading: '{headingDate}'. Using default month: {defaultMonth}");
                    month = defaultMonth;
                }
            }
            index++;

            while (index < tokens.Count && tokens[index].IsWord())
            {
                index++;
            }
            int year;

            if (index == tokens.Count)
            {
                var defaultYear = defaultDateProvider.GetDate().Year;
                var msg         = $"Heading date '{headingDate}' doesn't contain year. Using default year: {defaultYear}";
                logger.LogWarning(msg);
                year = defaultYear;
            }
            else
            {
                year = tokens[index].GetNumber();
            }


            return(new DateTime(year, month, day));
        }