public async Task <IActionResult> Edit(int id) { var drink = await this.drinks.ByIdAsync(id); if (drink == null) { return(NotFound()); } var category = await this.categories.ByIdAsync(drink.CategoryId); var model = new EditDrinkViewModel { Id = id, Name = drink.Name, Description = drink.Description, FullDescription = drink.FullDescription, Price = drink.Price, ImageUrl = drink.ImageUrl, ImageThumbnailUrl = drink.ImageThumbnailUrl, IsPreferred = drink.IsPreferred, IsInStock = drink.IsInStock, Category = category.Name }; return(View(model)); }
public async Task EditDrinkAsyncWorksCorrectly() { await this.PopulateDB(); await this.AddDrinksToDB(); var newIngredient = new Ingredient() { Name = "test3", Id = 3, }; this.DbContext.Ingredients.Add(newIngredient); await this.DbContext.SaveChangesAsync(); var ingredient = this.DbContext.Ingredients.FirstOrDefault(x => x.Id == 1); var drinkTypeId = 2; var ingredientId = 1; var packagingTypeId = 2; var image = this.GetFile("test123"); var expectedName = "edit"; var expectedIntValue = 33; var editDrink = new EditDrinkViewModel() { Id = "test1", Name = expectedName, Price = expectedIntValue, Weight = expectedIntValue, AdditionalInfo = expectedName, DrinkTypeId = drinkTypeId, IngredientsId = new List <int>() { ingredientId, newIngredient.Id }, NewImage = image, AlchoholByVolume = expectedIntValue, PackagingTypeId = packagingTypeId, }; await this.DrinkService.EditDrinkAsync(editDrink, AppDomain.CurrentDomain.BaseDirectory); var actualIngredient = this.DbContext.Ingredients.FirstOrDefault(x => x.Id == ingredientId); var actual = this.DbContext.Drinks.FirstOrDefault(x => x.Id == "test1"); Assert.Equal(editDrink.Name, actual.Name); Assert.Equal(expectedIntValue, actual.Price); Assert.Equal(expectedIntValue, actual.Weight); Assert.Equal(expectedIntValue, actual.AlchoholByVolume); Assert.Equal(packagingTypeId, actual.PackagingTypeId); Assert.Equal(expectedName, actual.AdditionalInfo); Assert.Equal(drinkTypeId, actual.DrinkTypeId); Assert.Equal(new List <Ingredient>() { actualIngredient, newIngredient }, actual.Ingredients); Assert.Equal(ImageExtension.jpeg, actual.Image.Extension); }
public async Task <IActionResult> Edit(EditDrinkViewModel model) { if (!ModelState.IsValid) { return(View(model)); } var drink = await this.drinks.ByIdAsync(model.Id); if (drink == null) { return(NotFound()); } var category = await this.categories.ByNameAsync(model.Category); if (category == null) { TempData.AddErrorMessage($"Invalid category: {model.Category}"); return(View(model)); } await this.drinks.EditAsync( model.Id, model.Name, model.Description, model.FullDescription, model.Price, model.ImageUrl, model.ImageThumbnailUrl, model.IsPreferred, model.IsInStock, category.Id); TempData.AddSuccessMessage($"Successfully edited drink: {model.Name}"); return(this.RedirectToCustomAction(nameof(HomeController.Index), nameof(HomeController))); }
public async Task <IActionResult> EditDrink(EditDrinkViewModel editDrink) { await this.drinkService.EditDrinkAsync(editDrink, this.webHostEnvironment.WebRootPath); return(this.RedirectToAction("Index", "Menu")); }