Beispiel #1
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;
        }
Beispiel #2
0
        public MenuResult CreateMenu(AuthIdentity identity, Menu menu, params Guid[] recipeIds)
        {
            using (var session = GetSession())
             {
            menu.Title = menu.Title.Trim();
            var ret = new MenuResult();

            using (var transaction = session.BeginTransaction())
            {
               Models.Menus dbMenu;
               var dupes = session.QueryOver<Models.Menus>()
                  .Where(p => p.UserId == identity.UserId)
                  .Where(p => p.Title == menu.Title)
                  .ToRowCountQuery()
                  .RowCount();

               if (dupes > 0)
               {
                  throw new MenuAlreadyExistsException();
               }

               session.Save(dbMenu = new Models.Menus
               {
                  UserId = identity.UserId,
                  Title = menu.Title,
                  CreatedDate = DateTime.Now,
               });

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

                  session.Save(fav);
               }

               transaction.Commit();

               ret.MenuCreated = true;
               ret.NewMenuId = dbMenu.MenuId;
            }

            return ret;
             }
        }
        /// <summary>
        /// Created a new menu owned by the current user.
        /// </summary>
        /// <param name="menu">A menu to create.</param>
        /// <param name="recipeIds">Zero or more recipes to add to the newly created menu.</param>
        /// <returns>A result indicating the new menu ID.</returns>
        public MenuResult CreateMenu(Menu menu, params Guid[] recipeIds)
        {
            menu.Title = menu.Title.Trim();
             var ret = new MenuResult();

             Menus dbMenu;
             var dupes = store.Menus
            .Where(p => p.UserId == Identity.UserId)
            .Any(p => p.Title == menu.Title);

             if (dupes)
             {
            throw new MenuAlreadyExistsException();
             }

             store.Menus.Add(dbMenu = new Data.DTO.Menus
             {
            MenuId = Guid.NewGuid(),
            UserId = Identity.UserId,
            Title = menu.Title,
            CreatedDate = DateTime.Now,
             });

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

            store.Favorites.Add(fav);
             }

             ret.IsMenuCreated = true;
             ret.NewMenuId = dbMenu.MenuId;

             return ret;
        }
        /// <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;
        }