예제 #1
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;
        }
예제 #2
0
        /// <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;
        }