public PartialViewResult RemoveIngredient(Guid id) { HttpCookie cookie; ICollection <IngredientCreateViewModel> ingredients; if (CookieHelper.CookieExists(currentRecipeCookie, out cookie)) { ingredients = CookieHelper.ReadCookie <ICollection <IngredientCreateViewModel> >(cookie, createdRecipeIngredients); if (ingredients == null) { CookieHelper.DeleteCookie(currentRecipeCookie); } else { IngredientCreateViewModel ingredient = ingredients.First(i => i.ProductId == id); if (ingredient != null) { ingredients.Remove(ingredient); CookieHelper.UpdateCookie <ICollection <IngredientCreateViewModel> >(cookie, createdRecipeIngredients, ingredients); } } } else { ingredients = new List <IngredientCreateViewModel>(); } return(PartialView("_CreatedRecipeIngredients", ingredients)); }
public async Task <IActionResult> addIngredient(int RecipeId) { var vm = new IngredientCreateViewModel(await _context.RecipeItems .Select(r => new SelectListItem { Value = r.Unit, Text = r.Unit }).Distinct().ToListAsync()); return(View(vm)); }
public async Task Create_ValidInput_ReturnCreatedResource() { var validInput = new IngredientCreateViewModel { Name = "Apple" }; var result = await ingredientService.Create(validInput); Assert.That(result, Is.Not.Null); Assert.That(result.Name, Is.EqualTo(validInput.Name)); }
public void CreateIngredient(IngredientCreateViewModel ingredientCreateViewModel) { var ingredients = this.ListAllIngredients(); var index = ingredients.Max(x => x.Id); var newIngredient = new Ingredient(); newIngredient.Id = index + 1; newIngredient.Name = ingredientCreateViewModel.Name; newIngredient.ExpirationDate = ingredientCreateViewModel.ExpirationDate; newIngredient.Quantity = ingredientCreateViewModel.Quantity; ingredients.Add(newIngredient); this._rwHelper.SerializeIngredients(ingredients); }
public PartialViewResult AddIngredient(Guid id) { HttpCookie cookie; var product = ProductManager.FindById(id); IngredientCreateViewModel ingredient = new IngredientCreateViewModel { ProductId = id, Quantity = 0, ProductName = product.Name, BaseProtein = product.Protein, BaseCarbohydrate = product.Carbohydrate, BaseFat = product.Fat }; ICollection <IngredientCreateViewModel> ingredients; if (CookieHelper.CookieExists(currentRecipeCookie, out cookie)) { ingredients = CookieHelper.ReadCookie <ICollection <IngredientCreateViewModel> >(cookie, createdRecipeIngredients); if (ingredients == null) { CookieHelper.DeleteCookie(currentRecipeCookie); ingredients = new List <IngredientCreateViewModel> { ingredient }; CookieHelper.CreateCookie <ICollection <IngredientCreateViewModel> >( currentRecipeCookie, createdRecipeIngredients, ingredients, new TimeSpan(0, 30, 0)); } else { if (!ingredients.Any(i => i.ProductId == id)) { ingredients.Add(ingredient); CookieHelper.UpdateCookie <ICollection <IngredientCreateViewModel> >(cookie, createdRecipeIngredients, ingredients); } } } else { ingredients = new List <IngredientCreateViewModel> { ingredient }; CookieHelper.CreateCookie <ICollection <IngredientCreateViewModel> >( currentRecipeCookie, createdRecipeIngredients, ingredients, new TimeSpan(0, 30, 0)); } return(PartialView("_CreatedRecipeIngredients", ingredients)); }
public async Task <IActionResult> addIngredient(IngredientCreateViewModel ingredientViewModel, [FromRoute] int RecipeId) { //var addedRecipeItems = new List<RecipeItem>(); if (ModelState.IsValid) { var ingredient = new Ingredient(); var recipeItem = new RecipeItem(); var ingredientExist = _context.Ingredients.Where(x => x.Name == ingredientViewModel.Name).Select(x => x.Name).FirstOrDefault(); if (ingredientExist == null) { ingredient.Name = ingredientViewModel.Name; _context.Add(ingredient); recipeItem.Name = ingredientViewModel.Name; recipeItem.Ingredient = ingredient; } else { var ingredientId = _context.Ingredients.Where(x => x.Name == ingredientViewModel.Name).Select(x => x.Id).First(); recipeItem.IngredientId = ingredientId; recipeItem.Name = ingredientViewModel.Name; } recipeItem.Quantity = ingredientViewModel.Quantity; recipeItem.Unit = ingredientViewModel.Unit; recipeItem.RecipeId = RecipeId; //ingredientViewModel.AddedRecipeItems = addedRecipeItems; //addedRecipeItems.Add(recipeItem); _context.Add(recipeItem); await _context.SaveChangesAsync(); ModelState.Clear(); } ingredientViewModel.AddUnits(await _context.RecipeItems .Select(p => new SelectListItem { Value = p.Unit, Text = p.Unit }).ToListAsync()); return(RedirectToAction(nameof(addIngredient), new { RecipeId = RecipeId })); }
public async Task <Ingredient> Create(IngredientCreateViewModel ingredientCreateViewModel) { var newIngredient = mapper.Map <Ingredient>(ingredientCreateViewModel); var existingIngredient = await applicationDbContext.Ingredients.FirstOrDefaultAsync(ingredient => ingredient.Name == newIngredient.Name); if (existingIngredient != null) { throw new ResourceAlreadyExistsException("An ingredient by this name already exists."); } await applicationDbContext.Ingredients.AddAsync(newIngredient); await applicationDbContext.SaveChangesAsync(); return(newIngredient); }
public async Task Create_InputWithExistingName_ThrowException() { var existingIngredient = new Ingredient { Name = "Carrot" }; await testDbContext.Ingredients.AddAsync(existingIngredient); await testDbContext.SaveChangesAsync(); var inputWithExistingName = new IngredientCreateViewModel { Name = existingIngredient.Name }; Assert.ThrowsAsync <ResourceAlreadyExistsException>(async() => await ingredientService.Create(inputWithExistingName)); }
public async Task <IActionResult> Create([FromBody] IngredientCreateViewModel ingredientCreateViewModel) { try { return(Ok(await ingredientService.Create(ingredientCreateViewModel))); } catch (ResourceAlreadyExistsException exception) { logger.Error("{Message}", exception.Message); return(BadRequest(exception.Message)); } catch (Exception exception) { logger.Error("{Message}", exception.Message); return(Problem()); } }
public async Task <IActionResult> Create(IngredientCreateViewModel viewModel) { if (ModelState.IsValid) { var result = await this._ingredientService.CheckByNameAsync(viewModel.Name); if (result) { ModelState.AddModelError(string.Empty, "Ingredient is exist."); return(View(viewModel)); } try { var ingredient = this._mapper.Map <Ingredient>(viewModel); var created = await this._ingredientService.CreateAsync(ingredient); if (!created) { this._logger.LogError($"Cannot create ingredient"); return(View("Error", new ErrorViewModel() { ErrorTitle = "Create Ingredient", ErrorMessage = "Cannot create ingredient" })); } return(RedirectToAction(nameof(Details), new { id = ingredient.Id })); } catch (DbUpdateException ex) { this._logger.LogError(ex.Message); return(View("Error", new ErrorViewModel() { ErrorTitle = "Create Ingredient", ErrorMessage = ex.Message })); } } return(View(viewModel)); }
public IActionResult Create() { IngredientCreateViewModel ingredientCreateViewModel = new IngredientCreateViewModel(); try { ingredientCreateViewModel.Name = Request.Form["Name"].ToString(); ingredientCreateViewModel.ExpirationDate = Request.Form["ExpirationDate"].ToString(); if (ingredientCreateViewModel.Name == "" || ingredientCreateViewModel.ExpirationDate == "") { throw new FormatException(); } ingredientCreateViewModel.Quantity = Convert.ToInt16(Request.Form["Quantity"].ToString()); }catch (FormatException e) { return(NotFound(e.Message)); } this._ingredientRepository.CreateIngredient(ingredientCreateViewModel); return(RedirectToAction("Index")); }
private bool TryUpdateCreatedQuantity(Guid productId, int quantity, out ICollection <IngredientCreateViewModel> ingredients) { HttpCookie recipeCookie; if (CookieHelper.CookieExists(currentRecipeCookie, out recipeCookie)) { ingredients = CookieHelper.ReadCookie <ICollection <IngredientCreateViewModel> >( recipeCookie, createdRecipeIngredients); if (ingredients == null) { // Ciasteczko jest uszkodzone. CookieHelper.DeleteCookie(currentRecipeCookie); return(false); } IngredientCreateViewModel ing = ingredients.First(i => i.ProductId == productId); if (ing != null) { ing.Quantity = quantity; } else { var product = ProductManager.FindById(productId); if (product != null) { ingredients.Add(new IngredientCreateViewModel { ProductId = productId, ProductName = product.Name, Quantity = quantity }); } else { return(false); } } CookieHelper.UpdateCookie <ICollection <IngredientCreateViewModel> >(recipeCookie, createdRecipeIngredients, ingredients); return(true); } ingredients = null; return(false); }
public IActionResult Create(IngredientCreateViewModel ingredientVieModel) { return(RedirectToAction(nameof(Index))); }
public PartialViewResult AddIngredient(Guid id) { HttpCookie cookie; var product = ProductManager.FindById(id); IngredientCreateViewModel ingredient = new IngredientCreateViewModel { ProductId = id, Quantity = 0, ProductName = product.Name, BaseProtein = product.Protein, BaseCarbohydrate = product.Carbohydrate, BaseFat = product.Fat }; ICollection<IngredientCreateViewModel> ingredients; if (CookieHelper.CookieExists(currentRecipeCookie, out cookie)) { ingredients = CookieHelper.ReadCookie<ICollection<IngredientCreateViewModel>>(cookie, createdRecipeIngredients); if (ingredients == null) { CookieHelper.DeleteCookie(currentRecipeCookie); ingredients = new List<IngredientCreateViewModel> { ingredient }; CookieHelper.CreateCookie<ICollection<IngredientCreateViewModel>>( currentRecipeCookie, createdRecipeIngredients, ingredients, new TimeSpan(0, 30, 0)); } else { if (!ingredients.Any(i => i.ProductId == id)) { ingredients.Add(ingredient); CookieHelper.UpdateCookie<ICollection<IngredientCreateViewModel>>(cookie, createdRecipeIngredients, ingredients); } } } else { ingredients = new List<IngredientCreateViewModel> { ingredient }; CookieHelper.CreateCookie<ICollection<IngredientCreateViewModel>>( currentRecipeCookie, createdRecipeIngredients, ingredients, new TimeSpan(0, 30, 0)); } return PartialView("_CreatedRecipeIngredients", ingredients); }