Exemple #1
0
        public static Menu AddProductToMenu(Menu menu, Product product, string categoryId)
        {
            if (menu.Categories == null)
            {
                menu.Categories = new List<Category> { new Category { Id = categoryId, Products = new List<string>() } };
            }
            var category = menu.Categories.FirstOrDefault(c => c.Id == categoryId);
            if (category == null)
            {
                category = new Category { Id = categoryId, Products = new List<string>() };
                menu.Categories.Add(category);
            }

            category.Products.Add(product.Id);

            return menu;
        }
        public static Enterprise CreateFakeEnterprise(Abstract.Db.IDb Db,bool modified)
        {
            var enterprise = new Enterprise
                                 {
                                     Id = EnterpriseHelper.GetId(GeneralHelper.GetGuid()),
                                     Name = RandomString(),
                                     Categories = RandomListString(),
                                     LastUpdated = DateTime.Now,
                                     Coordinates = RandomCoordinates(),
                                     StreetRoute = RandomString(),
                                     County = "Stockholm"
                                 };
            var menu = new Menu();
            var categories = new List<Category>();

            var products = new List<Product>();

            for (var i = 0; i < _random.Next(35, 40); i++)
            {
                var category = new Category
                                      {
                                          Id = GeneralHelper.GetGuid(),
                                          Name = RandomString(),
                                          Products = new List<string>()
                                      };
                for (var j = 0; j < _random.Next(1, 2); j++)
                {
                    var product = NewProduct(enterprise.Id,modified);


                    products.Add(product);
                    category.Products.Add(product.Id);
                }
                categories.Add(category);
            }


            menu.Categories = categories;


            if(!modified)
            {
                enterprise.IsNew = true; 
            }

            enterprise.Menu = menu;
            
            Db.Enterprises.CreateEnterprise(enterprise);
            Db.Products.AddProductsToDb(products);

            if(modified)
            {
                Thread.Sleep(1000);
                var categoryCount = enterprise.Menu.Categories.Count;

                //Remove random products
                for (var i = 0; i < _random.Next(1, 4); i++)
                {
                    var randomCategory = enterprise.Menu.Categories[_random.Next(categoryCount)];
                    var productCountForCategory = randomCategory.Products.Count;
                    enterprise.Menu.Categories[_random.Next(categoryCount)].Products.RemoveAt(_random.Next(productCountForCategory));
                }

                //Create new products
                var newProducts = new List<Product>();
                for (var i = 0; i < _random.Next(1, 8);i++ )
                {
                    newProducts.Add(NewProduct(enterprise.Id,false));
                }

                //Add the new products to random categories in random places
                foreach (var newProduct in newProducts)
                {
                    var randomCategory = enterprise.Menu.Categories[_random.Next(categoryCount)];
                    var productCountForCategory = randomCategory.Products.Count;
                    randomCategory.Products.Insert(_random.Next(productCountForCategory),newProduct.Id);
                }

                Db.Products.AddProductsToDb(newProducts);
                Thread.Sleep(1000);
                Db.Enterprises.UpdateEnterprise(enterprise.Id, enterprise.Menu);
            }

            return enterprise;
        }
        public void AddProduct(Product product, string categoryId, string enterpriseId)
        {
            using (var session = _documentStore.OpenSession())
            {
                var enterprise = session.Load<Enterprise>(enterpriseId);

                if (EnterpriseHelper.ValidEditableEnterprise(enterprise, session))
                {
                    product.Enterprise = enterpriseId;

                    //Om det är en ny enterprise eller en som är ägd, spara produkten direkt
                    if (enterprise.IsNew || enterprise.OwnedByAccount)
                    {
                        enterprise.Menu = MenuHelper.AddProductToMenu(enterprise.Menu, product, categoryId);
                        _logger.Info(string.Format("New product ({0}) added to new enterprise: {1}, Code:[gPrsdfeas3]", product.Id, enterpriseId));
                    }
                    else
                    {
                        //If enterprise is existing, save/create a TEMP-menu for approvement
                        var modifiedMenuId = MenuHelper.GetModifiedMenuId(enterpriseId);
                        var menuInDb = session.Load<ModifiedMenu>(modifiedMenuId);

                        if (menuInDb == null)
                        {
                            //Copy the menu from the Enpterprise
                            var menuCopy = new Menu
                                        {
                                            Categories = new List<Category>()
                                        };
                            foreach (var c in enterprise.Menu.Categories)
                            {
                                var category = new Category { Id = c.Id, Name = c.Name, Products = new List<string>() };
                                foreach (var p in c.Products)
                                {
                                    category.Products.Add(p);
                                }
                                menuCopy.Categories.Add(category);
                            }
                            menuInDb = new ModifiedMenu { Id = modifiedMenuId, Menu = menuCopy };

                            enterprise.ModifiedMenu = modifiedMenuId;
                        }

                        MenuHelper.AddProductToMenu(menuInDb.Menu, product, categoryId);
                        session.Store(menuInDb);
                        _logger.Info(string.Format("New product {0} added to modified menu:{1} Code:[g8iopgdfe]", product.Id, modifiedMenuId));

                    }
                    session.Store(enterprise);
                    session.Store(product);
                    session.SaveChanges();

                }
                else
                {
                    var loggedInUser = !string.IsNullOrEmpty(HttpContext.Current.User.Identity.Name) ? string.Format(", logged in user: {0}", HttpContext.Current.User.Identity.Name) : string.Empty;
                    _logger.Warn(string.Format("A product({0}) was about to be added to a non-valid enterprise with id: {1}{2}, Code:[yTerdfds56]", product.Name, enterpriseId, loggedInUser));
                }

            }
        }