예제 #1
0
        public bool UpdateEnterprise(string enterpriseId, Menu menu)
        {
            var updated = false;
            using (var session = _documentStore.OpenSession())
            {
                var enterprise = session.Load<Enterprise>(enterpriseId);

                try
                {
                    if (EnterpriseHelper.ValidEditableEnterprise(enterprise, session))
                    {
                        MenuHelper.ValidateMenu(menu, enterpriseId, session, _logger);
                        if (enterprise.IsNew || enterprise.OwnedByAccount)
                        {
                            var deletedProductsIds = GeneralHelper.CompareLists(enterprise.Menu.Categories.SelectMany(c => c.Products).ToList(), menu.Categories.SelectMany(c => c.Products).ToList());

                            if (deletedProductsIds.Count > 0)
                            {
                                var deletedProducts = session.Load<Product>(deletedProductsIds);

                                //Delete products that belongs to this enterprise
                                foreach (var deletedProduct in deletedProducts.Where(deletedProduct => deletedProduct != null && deletedProduct.Enterprise == enterpriseId))
                                {
                                    session.Delete(deletedProduct);
                                }
                            }

                            enterprise.Menu = menu;
                            enterprise.LastUpdated = DateTime.Now;
                        }
                        else
                        {
                            var modifiedMenuInDb = session.Load<ModifiedMenu>(MenuHelper.GetModifiedMenuId(enterpriseId));

                            if (modifiedMenuInDb == null)
                            {
                                var modifiedMenu = new ModifiedMenu
                                {
                                    Id = MenuHelper.GetModifiedMenuId(enterpriseId),
                                    Menu = menu
                                };
                                session.Store(modifiedMenu);

                                enterprise.LockedFromEdit = true;
                                enterprise.ModifiedMenu = modifiedMenu.Id;
                            }

                        }

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

                        updated = true;

                        _logger.Info("Updated menu settings for enterprise: " + enterprise.Id);
                    }
                }
                catch (Exception ex)
                {
                    _logger.Fatal(ex.Message, ex);
                    var failedEnterprise = new FailedEnterprise
                                               {
                                                   Enterprise = enterprise,
                                                   Menu = menu,
                                                   ErrorMessage = ex.Message
                                               };
                    session.Store(failedEnterprise);
                    session.SaveChanges();
                }
            }
            return updated;
        }
예제 #2
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));
                }

            }
        }