Пример #1
0
 public MenuResult UpdateMenu(Guid? menuId, Guid[] recipesAdd, Guid[] recipesRemove, MenuMove[] recipesMove, bool clear, string newName = null)
 {
     throw new NotImplementedException();
 }
Пример #2
0
 /// <summary>
 /// Updates a specified menu owned by the current user.
 /// </summary>
 /// <param name="menuId">The Menu ID to update, or null to update the Favorites menu.</param>
 /// <param name="recipesAdd">A list of recipe IDs to add to the menu.  Duplicates will be ignored.</param>
 /// <param name="recipesRemove">A list of recipe IDs to remove from the menu.</param>
 /// <param name="recipesMove">A list of items to move from this menu to another menu.</param>
 /// <param name="clear">If true, all recipes will be removed from this menu.</param>
 /// <param name="newName">An optional new name for this menu.  Note, the favorites menu cannot be renamed.</param>
 /// <returns></returns>
 public virtual MenuResult UpdateMenu(
     Guid? menuId,
     Guid[] recipesAdd,
     Guid[] recipesRemove,
     MenuMove[] recipesMove,
     bool clear,
     string newName = null)
 {
     return this.Adapter.UpdateMenu(this.Identity, menuId, recipesAdd, recipesRemove, recipesMove, clear, newName);
 }
Пример #3
0
        public MenuResult UpdateMenu(AuthIdentity identity, Guid? menuId, Guid[] recipesAdd, Guid[] recipesRemove, MenuMove[] recipesMove, bool clear, string newName = null)
        {
            var ret = new MenuResult();
             ret.MenuUpdated = true; // TODO: Verify actual changes were made before setting MenuUpdated to true

             using (var session = GetSession())
             {
            using (var transaction = session.BeginTransaction())
            {
               Models.Menus dbMenu = null;
               IList<Favorites> dbRecipes = null;
               if (menuId.HasValue)
               {
                  dbMenu = session.QueryOver<Models.Menus>()
                     .Fetch(prop => prop.Recipes).Eager
                     .Where(p => p.MenuId == menuId)
                     .SingleOrDefault();

                  if (dbMenu == null)
                     throw new MenuNotFoundException();

                  if (dbMenu.UserId != identity.UserId) // User does not have access to modify this menu
                     throw new UserDoesNotOwnMenuException();

                  if (!String.IsNullOrWhiteSpace(newName) && dbMenu != null) // Rename menu
                     dbMenu.Title = newName.Trim();

                  dbRecipes = dbMenu.Recipes;
               }
               else
               {
                  dbRecipes = session.QueryOver<Favorites>()
                     .Where(p => p.UserId == identity.UserId)
                     .Where(p => p.Menu == null)
                     .List();
               }

               if (recipesAdd.Any()) // Add recipes to menu
               {
                  var existing = (from r in dbRecipes select r.Recipe.RecipeId);
                  recipesAdd = recipesAdd.Except(existing).ToArray(); //Remove dupes

                  foreach (var rid in recipesAdd)
                  {
                     var fav = new Favorites
                     {
                        UserId = identity.UserId,
                        Recipe = new Models.Recipes() {RecipeId = rid},
                        Menu = dbMenu
                     };

                     session.Save(fav);
                  }
               }

               if (recipesRemove.Any()) // Remove recipes from menu
               {
                  var toDelete = (from r in dbRecipes where recipesRemove.Contains(r.Recipe.RecipeId) select r);
                  toDelete.ForEach(session.Delete);
               }

               if (clear) // Remove every recipe from menu
               {
                  dbRecipes.ForEach(session.Delete);
               }

               if (recipesMove.Any()) // Move items to another menu
               {
                  foreach (var moveAction in recipesMove)
                  {
                     Models.Menus dbTarget = null;
                     if (moveAction.TargetMenu.HasValue)
                     {
                        dbTarget = session.QueryOver<Models.Menus>()
                           .Where(p => p.MenuId == moveAction.TargetMenu.Value)
                           .Where(p => p.UserId == identity.UserId)
                           .SingleOrDefault();

                        if (dbTarget == null)
                           throw new MenuNotFoundException(moveAction.TargetMenu.Value);
                     }

                     var rToMove = (moveAction.MoveAll
                        ? dbRecipes
                        : dbRecipes.Where(r => moveAction.RecipesToMove.Contains(r.Recipe.RecipeId)));

                     rToMove.ForEach(a => a.Menu = dbTarget);
                  }
               }

               transaction.Commit();
            }
             }

             return ret;
        }
Пример #4
0
        /// <summary>
        /// Updates a specified menu owned by the current user.
        /// </summary>
        /// <param name="menuId">The Menu ID to update, or null to update the Favorites menu.</param>
        /// <param name="recipesAdd">A list of recipe IDs to add to the menu.  Duplicates will be ignored.</param>
        /// <param name="recipesRemove">A list of recipe IDs to remove from the menu.</param>
        /// <param name="recipesMove">A list of items to move from this menu to another menu.</param>
        /// <param name="clear">If true, all recipes will be removed from this menu.</param>
        /// <param name="newName">An optional new name for this menu.  Note, the favorites menu cannot be renamed.</param>
        /// <returns></returns>
        public MenuResult UpdateMenu(Guid? menuId, Guid[] recipesAdd, Guid[] recipesRemove, MenuMove[] recipesMove, bool clear, string newName = null)
        {
            var ret = new MenuResult();
             ret.IsMenuUpdated = true; // TODO: Verify actual changes were made before setting MenuUpdated to true

             Menus dbMenu = null;
             if (menuId.HasValue)
             {
            dbMenu = store.Menus.SingleOrDefault(p => p.MenuId == menuId);
            if (dbMenu == null)
               throw new MenuNotFoundException();

            if (dbMenu.UserId != Identity.UserId) // User does not have access to modify this menu
               throw new UserDoesNotOwnMenuException();
             }

             var dbFavorites = store.Favorites
            .Where(p => p.MenuId == menuId)
            .ToList();

             if (!string.IsNullOrWhiteSpace(newName) && dbMenu != null) // Rename menu
            dbMenu.Title = newName.Trim();

             if (recipesAdd.Any()) // Add recipes to menu
             {
            var existing = dbFavorites.Select(f => f.RecipeId);
            recipesAdd = recipesAdd.Except(existing).ToArray(); //Remove dupes

            foreach (var rid in recipesAdd)
            {
               var fav = new Favorites
               {
                  FavoriteId = Guid.NewGuid(),
                  UserId = Identity.UserId,
                  RecipeId = rid,
                  MenuId = menuId
               };

               store.Favorites.Add(fav);
            }
             }

             if (recipesRemove.Any()) // Remove recipes from menu
             {
            var toDelete = (from r in dbFavorites where recipesRemove.Contains(r.RecipeId) select r);
            toDelete.ForEach(r => store.Favorites.Remove(r));
             }

             if (clear) // Remove every recipe from menu
             {
            store.Favorites.RemoveAll(dbFavorites.Contains);
             }

             if (recipesMove.Any()) // Move items to another menu
             {
            foreach (var moveAction in recipesMove)
            {
               Menus dbTarget = null;
               if (moveAction.TargetMenu.HasValue)
               {
                  dbTarget = store.Menus
                     .Where(p => p.UserId == Identity.UserId)
                     .SingleOrDefault(p => p.MenuId == moveAction.TargetMenu);

                  if (dbTarget == null)
                     throw new MenuNotFoundException(moveAction.TargetMenu.Value);
               }

               var rToMove = (moveAction.AllMoved
                  ? dbFavorites
                  : dbFavorites.Where(r => moveAction.RecipesToMove.Contains(r.RecipeId)));

               rToMove.ForEach(a => a.MenuId = dbTarget != null ? (Guid?) dbTarget.MenuId : null);
            }
             }

             return ret;
        }
        public MenuResult UpdateMenu(
            AuthorIdentity identity, 
            Guid? menuId, 
            Guid[] recipesAdd, 
            Guid[] recipesRemove, 
            MenuMove[] recipesMove, 
            bool clear, 
            string newName = null)
        {
            var menu = new MenuResult();

            using (var session = this.GetSession())
            {
                using (var transaction = session.BeginTransaction())
                {
                    Menus databaseMenu = null;
                    IList<Favorites> databaseRecipes = null;
                    if (menuId.HasValue)
                    {
                        databaseMenu =
                            session.QueryOver<Menus>()
                                .Fetch(prop => prop.Recipes)
                                .Eager.Where(p => p.MenuId == menuId)
                                .SingleOrDefault();

                        if (databaseMenu == null)
                        {
                            throw new MenuNotFoundException();
                        }

                        CheckForUserAccess(identity, databaseMenu);

                        RenameMenu(newName, databaseMenu);

                        databaseRecipes = databaseMenu.Recipes;
                    }
                    else
                    {
                        databaseRecipes =
                            session.QueryOver<Favorites>()
                                .Where(p => p.UserId == identity.UserId)
                                .Where(p => p.Menu == null)
                                .List();
                    }

                    if (recipesAdd.Any())
                    {
                        AddRecipesToMenu(identity, recipesAdd, databaseRecipes, databaseMenu, session);
                        menu.MenuUpdated = true;
                    }

                    if (recipesRemove.Any())
                    {
                        RemoveRecipesFromMenu(recipesRemove, databaseRecipes, session);
                        menu.MenuUpdated = true;
                    }

                    if (clear)
                    {
                        RemoveAllRecipesFromMenu(databaseRecipes, session);
                        menu.MenuUpdated = true;
                    }

                    if (recipesMove.Any())
                    {
                        MoveRecipesToAnotherMenu(identity, recipesMove, session, databaseRecipes);
                        menu.MenuUpdated = true;
                    }

                    transaction.Commit();
                }
            }

            return menu;
        }
        private static void MoveRecipesToAnotherMenu(
            AuthorIdentity identity, 
            MenuMove[] recipesMove, 
            ISession session, 
            IList<Favorites> databaseRecipes)
        {
            foreach (var moveAction in recipesMove)
            {
                Menus databaseTarget = null;
                if (moveAction.TargetMenu.HasValue)
                {
                    databaseTarget =
                        session.QueryOver<Menus>()
                            .Where(p => p.MenuId == moveAction.TargetMenu.Value)
                            .Where(p => p.UserId == identity.UserId)
                            .SingleOrDefault();

                    if (databaseTarget == null)
                    {
                        throw new MenuNotFoundException(moveAction.TargetMenu.Value);
                    }
                }

                var recipesToMove = moveAction.MoveAll
                                         ? databaseRecipes
                                         : databaseRecipes.Where(r => moveAction.RecipesToMove.Contains(r.Recipe.RecipeId));

                recipesToMove.ForEach(a => a.Menu = databaseTarget);
            }
        }