예제 #1
0
        public ActionResult New(NewRecipeViewModel vm)
        {
            if (ModelState.IsValid)
            {
                Recipe recipe = new Recipe
                {
                    CategoryId       = vm.CategoryId,
                    Title            = vm.Title,
                    AuthorId         = User.Identity.GetUserId(),
                    Directions       = vm.Directions,
                    Slug             = UrlService.URLFriendly(vm.Slug),
                    CreationTime     = DateTime.Now,
                    ModificationTime = DateTime.Now,
                    PhotoPath        = this.SaveImage(vm.FeaturedImage)
                };
                var selectedIngredients = new List <Ingredient>();
                foreach (var item in vm.IngredientId)
                {
                    selectedIngredients.Add(db.Ingredients.FirstOrDefault(x => x.Id == item));
                }
                recipe.Ingredients = selectedIngredients;
                db.Recipes.Add(recipe);
                db.SaveChanges();
                TempData["SuccessMessage"] = "Post has been created successfully.";

                return(RedirectToAction("Index"));
            }

            ViewBag.CategoryId = new SelectList(db.Categories.OrderBy(x => x.CategoryName).ToList(), "Id", "CategoryName");
            return(View());
        }
        public ActionResult Save(Recipe recipe)
        {
            if (!ModelState.IsValid)
            {
                var model = new NewRecipeViewModel()
                {
                    recipe = recipe, types = _context.RecipesTypes.ToList()
                };
                return(View("New", model));
            }

            if (recipe.ID == 0)
            {
                _context.Recipes.Add(recipe);
            }
            else
            {
                var tempRecipe = _context.Recipes.SingleOrDefault(r => r.ID == recipe.ID);
                tempRecipe.Title        = recipe.Title;
                tempRecipe.Description  = recipe.Description;
                tempRecipe.RecipeTypeId = recipe.RecipeTypeId;
            }

            _context.SaveChanges();
            return(RedirectToAction("List"));
        }
        public NewRecipePage()
        {
            InitializeComponent();
            BindingContext = new NewRecipeViewModel();

            MessagingCenter.Subscribe <NewRecipeViewModel>(this, MessageStrings.PopOffCurrentModal, async(sender) =>
            {
                await Navigation.PopModalAsync();
            });
        }
        public ActionResult New()
        {
            var types          = _context.RecipesTypes.ToList();
            var recipeWithType = new NewRecipeViewModel()
            {
                types = types
            };

            return(View(recipeWithType));
        }
예제 #5
0
        public NewRecipeView()
        {
            InitializeComponent();
            WindowStartupLocation = WindowStartupLocation.CenterScreen;
            var viewModel = new NewRecipeViewModel();

            this.DataContext = viewModel;
            if (viewModel.CloseAction == null)
            {
                viewModel.CloseAction = new Action(this.Close);
            }
        }
        public ActionResult Edit(int id)
        {
            var recipe = _context.Recipes.SingleOrDefault(r => r.ID == id);

            if (recipe == null)
            {
                return(HttpNotFound());
            }

            var model = new NewRecipeViewModel {
                recipe = recipe, types = _context.RecipesTypes.ToList()
            };


            _context.SaveChanges();
            return(View("New", model));
        }
예제 #7
0
        /// <summary>
        /// Creates a new Recipe with a list of Products from Shoppinglist (if there are any)
        /// </summary>
        /// <returns>ViewResult.</returns>
        public ViewResult New()
        {
            var recipe = _service.GetTempRecipe("tempRecipe");

            if (recipe == null)
            {
                recipe = new Recipe();
                _service.SetTempRecipe("tempRecipe", recipe);
            }

            var viewModel = new NewRecipeViewModel
            {
                Recipe     = recipe,
                NewProduct = new Product()
            };

            return(View(viewModel));
        }
 public ApiResponse <RecipeViewModel> AddRecipe([FromBody] NewRecipeViewModel model)
 {
     try
     {
         return(ApiResponse <RecipeViewModel> .CreateResponse(true, "", _recipeService.AddRecipes(new Services.Message.AddRecipeRequest()
         {
             model = model
         }).recipe));
     }
     catch (BusinessRuleException ex)
     {
         return(ApiResponse <RecipeViewModel> .CreateResponse(false, ex.Message, null, rules : ex.brokenRules, code : HttpStatusCode.BadRequest));
     }
     catch (Exception ex)
     {
         Console.Write(ex);
         return(ApiResponse <RecipeViewModel> .CreateResponse(false, "An unexpected error occured.", null, code : HttpStatusCode.InternalServerError));
     }
 }
예제 #9
0
        public ActionResult Add(NewRecipeViewModel viewModel)
        {
            var recipe = _service.GetTempRecipe("tempRecipe");

            recipe.Name         = viewModel.Recipe.Name;
            recipe.CookingTimeH = viewModel.Recipe.CookingTimeH;
            recipe.CookingTimeM = viewModel.Recipe.CookingTimeM;
            recipe.Portions     = viewModel.Recipe.Portions;

            var fileName = CreateFileName(viewModel.Recipe.Image.File.FileName);

            recipe.Image = CreateImage(fileName);
            fileName     = Path.Combine(Server.MapPath("~/Image/"), fileName);
            viewModel.Recipe.Image.File.SaveAs(fileName);

            _service.AddOrUpdate(recipe);
            _service.Complete();

            var newRecipe = new Recipe();

            _service.SetTempRecipe("tempRecipe", newRecipe);
            return(RedirectToAction("Index", "Recipes"));
        }
예제 #10
0
        public async Task <IActionResult> Create(NewRecipeViewModel viewModel)
        {
            var user = await GetCurrentUserAsync();

            viewModel.Recipe.UserId = user.Id;

            if (ModelState.IsValid)
            {
                viewModel.Recipe.RecipeTypeId = viewModel.SelectedRecipeType;

                _context.Add(viewModel.Recipe);
                await _context.SaveChangesAsync();

                foreach (var ingredientId in viewModel.IngredientsWithAmount.Keys)
                {
                    var amount = viewModel.IngredientsWithAmount[ingredientId];

                    if (!String.IsNullOrEmpty(amount))
                    {
                        var ri = new RecipeIngredients
                        {
                            IngredientId = ingredientId,
                            RecipeId     = viewModel.Recipe.RecipeId,
                            Amount       = amount
                        };
                        _context.Add(ri);
                        _context.SaveChanges();
                    }
                }


                return(RedirectToAction(nameof(MyRecipes)));
            }
            ViewData["RecipeTypeId"] = new SelectList(_context.RecipeType, "RecipeTypeId", "RecipeTypeId", viewModel.Recipe.RecipeTypeId);
            ViewData["UserId"]       = new SelectList(_context.ApplicationUsers, "Id", "Id", viewModel.Recipe.UserId);
            return(View(viewModel.Recipe));
        }
        public void Should_Add_A_Recipe_Wit_Items_Successfully()
        {
            var recipe = new NewRecipeViewModel();

            recipe.Title = "Integration Recipe";

            var ingredients = new List <NewRecipeItemViewModel>()
            {
                new NewRecipeItemViewModel()
                {
                    IngredientId = Guid.Parse("FB1BCEE5-B3F2-443A-B14D-511C52C959EA"),
                    Measure      = MeasureViewModel.TSP,
                    Quantity     = 0.5
                }
            };

            recipe.Ingredients = ingredients.AsEnumerable();

            var request = new AddRecipeRequest()
            {
                model = recipe
            };
            var response = _recipeService.AddRecipes(request);
        }
예제 #12
0
        // GET: Recipes/Create
        public IActionResult Create()
        {
            NewRecipeViewModel viewModel = new NewRecipeViewModel(_context.Ingredients.ToList(), _context.RecipeType.ToList());

            return(View(viewModel));
        }
예제 #13
0
        public NewRecipePage(Category category = null)
        {
            InitializeComponent();

            BindingContext = viewModel = new NewRecipeViewModel(category);
        }
예제 #14
0
 public NewRecipePage()
 {
     InitializeComponent();
     this.vm        = new NewRecipeViewModel();
     BindingContext = this.vm;
 }