Пример #1
0
        public IActionResult Create(IngredientListInputModel model)
        {
            var dbRecipe = _db.Recipes.FirstOrDefault(r => r.Id == model.RecipeId);

            if (dbRecipe == null)
            {
                return(View("New"));
            }

            var shoppingList = new ShoppingList
            {
                Name      = dbRecipe.Name,
                UserId    = HttpContext.Session.GetInt32("_Userid").Value,
                CreatedAt = DateTime.Now
            };

            _db.Lists.Add(shoppingList);
            _db.SaveChanges();

            var recipeIngredientIds = _db.RecipeIngredients
                                      .Where(r => r.RecipeId == model.RecipeId)
                                      .Select(i => i.IngredientId)
                                      .ToArray();


            foreach (var recipeIngredientId in recipeIngredientIds)
            {
                var ingredient = _db.Ingredients
                                 .Where(i => i.Id == recipeIngredientId)
                                 .Select(i => i.Name)
                                 .Single();

                var listItem = new ListItem
                {
                    ListId   = shoppingList.Id,
                    ItemName = ingredient
                };

                _db.ListItems.Add(listItem);
            }

            _db.SaveChanges();
            return(RedirectToAction("View", new { listId = shoppingList.Id }));
        }
Пример #2
0
        public IActionResult Process(DataInput dataInput)
        {
            var uId = HttpContext.Session.GetInt32("_Userid");

            if (uId == -1)
            {
                return(RedirectToAction("Login", "Account"));
            }

            var slim = JsonConvert.DeserializeObject <ShoppingListInputModel>(dataInput.Data);

            var shoppingList = new ShoppingList
            {
                Name      = slim.Name,
                UserId    = uId.Value,
                CreatedAt = DateTime.Now
            };

            _db.Lists.Add(shoppingList);
            _db.SaveChanges();

            foreach (var item in slim.Items)
            {
                var listItem = new ListItem
                {
                    ListId   = shoppingList.Id,
                    ItemName = item
                };

                _db.ListItems.Add(listItem);
            }

            _db.SaveChanges();

            return(RedirectToAction("lists"));
        }