Ejemplo n.º 1
0
        public ShoppingListResult Commit()
        {
            // Build ShoppingListUpdateCommand array with each update
            var updates = new List <ShoppingListUpdateCommand>();

            if (addQueue.Any())
            {
                // Grab new raw entries
                var newRaws = addQueue.SelectMany(i => i.ToParse).Where(i => !String.IsNullOrWhiteSpace(i))
                              .Select(i => new ShoppingListUpdateCommand
                {
                    Command = ShoppingListUpdateType.AddItem,
                    NewRaw  = i
                });

                // Grab new Recipes
                var newRecipes = addQueue.SelectMany(i => i.Recipes).Where(i => i != null)
                                 .Select(i => new ShoppingListUpdateCommand
                {
                    Command   = ShoppingListUpdateType.AddItem,
                    NewRecipe = i
                });

                // Grab new IngredientUsages
                var newUsages = addQueue.SelectMany(i => i.Usages).Where(i => i != null)
                                .Select(i => new ShoppingListUpdateCommand
                {
                    Command  = ShoppingListUpdateType.AddItem,
                    NewUsage = i
                });

                // Grab new Ingredients
                var newIngredients = addQueue.SelectMany(i => i.Ingredients).Where(i => i != null)
                                     .Select(i => new ShoppingListUpdateCommand
                {
                    Command       = ShoppingListUpdateType.AddItem,
                    NewIngredient = i
                });

                updates.AddRange(newRaws);
                updates.AddRange(newRecipes);
                updates.AddRange(newUsages);
                updates.AddRange(newIngredients);
            }

            if (removeQueue.Any())
            {
                updates.AddRange(removeQueue.Where(i => i.Id.HasValue).Select(i => new ShoppingListUpdateCommand
                {
                    Command    = ShoppingListUpdateType.RemoveItem,
                    RemoveItem = i.Id
                }));
            }

            if (updateQueue.Any())
            {
                updates.AddRange(updateQueue.Where(i => i.Item.Id.HasValue).Select(i => new ShoppingListUpdateCommand
                {
                    Command    = ShoppingListUpdateType.ModifyItem,
                    ModifyItem = new ShoppingListModification(i.Item.Id.Value, i.NewAmount, i.CrossedOut)
                }));
            }

            return(context.UpdateShoppingList(list, updates.ToArray(), newName));
        }