Ejemplo n.º 1
0
        static void Main()
        {
            IMenuParser menuParser = new BillundPizzaMenuParser();
            IMenu       menu       = menuParser.ParseMenu();
            int         wrong      = 0;

            Console.WriteLine("***** Billund Pizzaria Menu *****");

            Console.WriteLine();
            foreach (IMenuItem item in menu.Items)
            {
                Console.WriteLine(item);
            }

            Console.WriteLine();
            Console.WriteLine($"Number of items: {menu.Items.Count}");
            var categories = menu.Items.Select(x => x.Category).Distinct().ToList();

            foreach (IMenuCategory category in categories)
            {
                Console.WriteLine($"{category.Name} - {menu.Items.Count(x => x.Category == category)}");
            }
            Console.WriteLine();
            Console.WriteLine("Done");
            Console.WriteLine($"Number of wrong parsings: {wrong}");
            Console.ReadLine();
        }
Ejemplo n.º 2
0
        public async Task <ActionResult <ScrapingResponse> > Scrape(ScrapingRequest request)
        {
            ScrapingResponse response = new ScrapingResponse {
                Success = true
            };

            try
            {
                // Scrape server
                IMenuParser menuParser = new BillundPizzaMenuParser();
                IMenu       menu       = menuParser.ParseMenu();
                ICollection <MenuItemModel> scraped = menu.Items.Select(x => new MenuItemModel(x)).ToList();

                // Load all existing entries
                ICollection <MenuItemModel> existing = await this._context.MenuItemModel.ToListAsync();

                ICollection <MenuCategoryModel> categories = await this._context.MenuCategoryModel.ToListAsync();

                // Update existing records with new info
                foreach (MenuItemModel menuItem in scraped)
                {
                    MenuItemModel existingItem = existing.FirstOrDefault(x => x.Number == menuItem.Number && x.Name == menuItem.Name &&
                                                                         x.Category.Name == menuItem.Category.Name && x.Description == menuItem.Description);
                    if (existingItem != null)
                    {
                        // Entry found, remove from cached collection
                        existing.Remove(existingItem);

                        //  Only update database if data is different
                        if (existingItem.Update(menuItem))
                        {
                            this._context.Entry(existingItem).State = EntityState.Modified;
                            response.NumberUpdated++;
                        }
                    }
                    else if (request.AddNew)
                    {
                        // Add new items from scrape
                        MenuCategoryModel category = categories.FirstOrDefault(x => x.Name == menuItem.Category.Name);
                        if (category == null)
                        {
                            category = new MenuCategoryModel {
                                Name = menuItem.Category.Name
                            };
                            categories.Add(category);
                            this._context.Add(category);
                        }

                        menuItem.Category = category;
                        this._context.Add(menuItem);
                        response.NumberCreated++;
                    }
                }

                // If requested, remove all existing entries not accounted for
                if (request.RemoveMissing)
                {
                    foreach (MenuItemModel existingItem in existing)
                    {
                        this._context.Remove(existingItem);
                        response.NumberRemoved++;
                    }
                }

                // Save changes
                await this._context.SaveChangesAsync();
            }
            catch
            {
                response.Success = false;
            }

            return(response);
        }