Пример #1
0
 public static Product AddUpdatedInfoToProduct(Product product)
 {
     product.Name = product.UpdatedVersion.Name;
     product.Description = product.UpdatedVersion.Description;
     product.Abv = product.UpdatedVersion.Abv;
     product.Prices = product.UpdatedVersion.Prices;
     product.Image = product.UpdatedVersion.Image;
     return product;
 }
Пример #2
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;
        }
Пример #3
0
        private static Product NewProduct(string enterpriseId,bool modified)
        {
            var product = new Product
            {
                Id = ProductHelper.GenerateId(),
                Enterprise = enterpriseId,
                Name = RandomString(),
                Prices = new List<ProductPrice>()
            };

            if (RandomBool())
                product.Description = RandomString();

            var productPrices = new List<ProductPrice>();
            for (var k = 0; k < _random.Next(1, 4); k++)
            {
                var productPrice = new ProductPrice { Price = RandomNumber() };
                if (RandomBool())
                    productPrice.Description = RandomString();

                productPrices.Add(productPrice);
            }

            product.Prices = productPrices;

            if (modified)
            {
                if (RandomBool())
                {
                    Mapper.CreateMap<Product, ProductUpdatedVersion>();
                    var updatedProduct = new ProductUpdatedVersion();
                    Mapper.Map(product, updatedProduct);
                    updatedProduct.Description = RandomString();
                    product.UpdatedVersion = updatedProduct;
                }
            }

            return product;
        }
Пример #4
0
        public void UpdateProduct(Product product, string enterpriseId)
        {
            using (var session = _documentStore.OpenSession())
            {
                var enterprise = session.Load<Enterprise>(enterpriseId);

                if (EnterpriseHelper.ValidEditableEnterprise(enterprise, session))
                {
                    //Om det är en ny enterprise eller en som är ägd, spara produkten direkt
                    if (enterprise.IsNew || enterprise.OwnedByAccount)
                    {
                        session.Store(product);
                    }
                    else
                    {
                        if (product.Enterprise == enterpriseId)
                        {
                            //If enterprise is existing, save an updated version of the product for approvement
                            var productInDb = session.Load<Product>(product.Id);
                            Mapper.CreateMap<Product, ProductUpdatedVersion>();
                            var updatedProduct = new ProductUpdatedVersion();
                            Mapper.Map(product, updatedProduct);
                            productInDb.UpdatedVersion = updatedProduct;
                        }
                        else
                        {
                            _logger.Warn(string.Format("Product: {0} belongs to this enterprise: {1} and was about to be updated to {2} Code:[hT882v563]", product.Id, product.Enterprise, enterpriseId));
                        }
                    }
                    _logger.Info(string.Format("Product:{0} was updated. Enterprise: {1}. Code:[poO0789b]",product.Id,enterpriseId));
                    session.SaveChanges();
                }
            }
        }
Пример #5
0
        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));
                }

            }
        }
Пример #6
0
 public static ProductViewModel ModelToViewModel(Product model)
 {
     Mapper.CreateMap<Product, ProductViewModel>();
     return Mapper.Map<Product, ProductViewModel>(model);
 }