Ejemplo n.º 1
0
        public async Task <IActionResult> Edit(EditCocktailViewModel model, IFormFile file)
        {
            if (!String.IsNullOrWhiteSpace(model.MainIngString))
            {
                model.MainIngredients = model.MainIngString.Split(',').ToList();
            }
            else
            {
                return(View("EditCocktail", model));
            }

            if (!this.ModelState.IsValid || model.MainIngredients.All(p => String.IsNullOrWhiteSpace(p)))
            {
                return(View("EditCocktail", model));
            }

            byte[] cocktailPhoto = null;
            if (file != null)
            {
                using (var stream = new MemoryStream())
                {
                    await file.CopyToAsync(stream);

                    cocktailPhoto = stream.ToArray();
                }
            }
            await cocktailService.UpdateCocktailAsync(model.Id, model.Name, model.Description, model.MainIngredients.ToArray(), model.IngString == null?null : model.IngString.Split(','), cocktailPhoto);

            return(RedirectToAction("Manage"));
        }
Ejemplo n.º 2
0
        public CocktailDTO MapToDTOFromVM(EditCocktailViewModel editCocktailVM)
        {
            if (editCocktailVM == null)
            {
                return(null);
            }
            var cocktailDTO = new CocktailDTO
            {
                Id          = editCocktailVM.Id,
                Name        = editCocktailVM.Name,
                Ingredients = editCocktailVM.ContainedIngredients.Select(i => new IngredientDTO
                {
                    Id = i
                }).ToList(),
                Bars = editCocktailVM.ContainedBars.Select(b => new BarDTO
                {
                    Id = b
                }).ToList(),
            };

            if (editCocktailVM.File != null)
            {
                cocktailDTO.ImageData = editCocktailVM.ImageData;
            }

            return(cocktailDTO);
        }
Ejemplo n.º 3
0
        public async Task <IActionResult> Edit(string cocktailId)
        {
            var id       = int.Parse(cocktailId);
            var cocktail = await cocktailService.FindCocktailByIdAsync(id);

            var ingredients = await iService.GetIngredientsByCocktailAsync(id);

            var vm = new EditCocktailViewModel(cocktail, ingredients);

            return(View("EditCocktail", vm));
        }
Ejemplo n.º 4
0
        public static EditCocktailViewModel MapToEditViewModel(this CocktailDetailsDTO cocktail)
        {
            var vm = new EditCocktailViewModel
            {
                Id                  = cocktail.Id,
                Name                = cocktail.Name,
                ImagePath           = cocktail.ImagePath,
                IngredientsQuantity = cocktail.Ingredients.Select(ci => ci.MapToViewModel()).ToList(),
            };

            return(vm);
        }
        public async Task <IActionResult> EditQuanities(int id, EditCocktailViewModel cocktailToEdit)
        {
            if (ModelState.IsValid)
            {
                try
                {
                    var ingredientsQuantityDTO = cocktailToEdit.IngredientsQuantity.Select(i => i.MapToDTO()).ToList();
                    await cocktailServices.EditIngredientsAsync(id, ingredientsQuantityDTO, cocktailToEdit.IngredientsToRemove);

                    return(RedirectToAction("Details", new { id = cocktailToEdit.Id }));
                }
                catch (InvalidOperationException)
                {
                    return(BadRequest());
                }
            }
            ModelState.AddModelError("", "Something went wrong...");
            return(View(cocktailToEdit));
        }
        public async Task <IActionResult> Edit(int id)
        {
            try
            {
                var ingredients = await this.ingredientService.GetAllIngredientsAsync();

                var ingredientVMs = ingredients.Select(ingredient => ingredientDTOMapper.MapToVMFromDTO(ingredient));
                var bars          = await this.barService.GetAllBarsAsync();

                var barVMs = bars.Select(bar => barDTOMApper.MapToVMFromDTO(bar));

                var cocktailDTO = await this.cocktailService.GetCocktailAsync(id);;

                if (cocktailDTO == null)
                {
                    this.toaster.AddWarningToastMessage(ToastrConsts.NotFound);
                    return(RedirectToAction(nameof(Index)));
                }

                var editCocktailVM = new EditCocktailViewModel
                {
                    Id                   = cocktailDTO.Id,
                    Name                 = cocktailDTO.Name,
                    ContainedBars        = cocktailDTO.Bars.Select(b => b.Id).ToList(),
                    ContainedIngredients = cocktailDTO.Ingredients.Select(i => i.Id).ToList(),
                };

                ViewData["Ingredients"] = new MultiSelectList(ingredientVMs, "Id", "Name");
                ViewData["Bars"]        = new MultiSelectList(barVMs, "Id", "Name");

                return(View(editCocktailVM));
            }
            catch (Exception)
            {
                this.toaster.AddWarningToastMessage(ToastrConsts.GenericError);
                return(RedirectToAction(nameof(Edit), new { id }));
            }
        }
        public async Task <IActionResult> EditIngredients(int id, [FromQuery] EditCocktailViewModel cocktailToEdit)
        {
            if (cocktailToEdit.CocktilNewIngredients.Count() == 0 && cocktailToEdit.IngredientsToRemove.Count() == 0)
            {
                cocktailToEdit = (await cocktailServices.GetDetailedDTOAsync(id)).MapToEditViewModel();

                cocktailToEdit.ListCurrentIngredients = cocktailToEdit.IngredientsQuantity
                                                        .Select(i => i.Value + i.Unit + " " + i.Name)
                                                        .ToList();

                var ingredients = await cocktailServices.GetAllNotIncludedIngredientsDTOAsync(id);

                cocktailToEdit.AllNotIncludedIngredients = ingredients
                                                           .Select(b => new SelectListItem($"{b.Name}, {b.Unit}", $"{b.Name}, {b.Unit}")).ToList();

                cocktailToEdit.CurrentCocktailIngredients = cocktailToEdit.IngredientsQuantity
                                                            .Select(i => new SelectListItem($"{i.Name}, {i.Unit}", i.Name)).ToList();

                return(View(cocktailToEdit));
            }
            foreach (var ingr in cocktailToEdit.CocktilNewIngredients)
            {
                cocktailToEdit.IngredientsQuantity.Add(new CocktailIngredientViewModel {
                    Name = ingr, Value = 0
                });
            }

            if (cocktailToEdit.IngredientsToRemove != null)
            {
                foreach (var ingr in cocktailToEdit.IngredientsToRemove)
                {
                    var iq = cocktailToEdit.IngredientsQuantity.FirstOrDefault(c => c.Name == ingr);
                    cocktailToEdit.IngredientsQuantity.Remove(iq);
                }
            }

            return(View(cocktailToEdit));
        }
        public async Task <IActionResult> Edit(string id)
        {
            try
            {
                var cocktailToEdit = await _cocktailManager.GetCocktailForEdit(id);

                var cocktailToEditVM = new EditCocktailViewModel();
                cocktailToEditVM.Cocktail = cocktailToEdit.ToEditVM();
                var listOfIngredients = await _ingredientManager.GetIngredientsAsync();

                foreach (var ingredient in listOfIngredients)
                {
                    if (!cocktailToEditVM.Cocktail.CocktailIngredients.Any(ci => ci.IngredientId == ingredient.Id))
                    {
                        cocktailToEditVM.IngreddientsThatCanAdd.Add(ingredient.ToVM());
                    }
                }
                return(View(cocktailToEditVM));
            }
            catch (Exception ex)
            {
                return(RedirectToAction("ErrorAction", "Error", new { errorCode = "404", errorMessage = ex.Message }));
            }
        }
        public async Task <IActionResult> Edit(int id, EditCocktailViewModel newEditCocktailVM)
        {
            var sanitizer = new HtmlSanitizer();

            newEditCocktailVM.Name = sanitizer.Sanitize(newEditCocktailVM.Name);

            if (id != newEditCocktailVM.Id)
            {
                this.toaster.AddWarningToastMessage(ToastrConsts.NotFound);
                return(RedirectToAction(nameof(Index)));
            }

            if (ModelState.IsValid)
            {
                try
                {
                    if (newEditCocktailVM.File != null)
                    {
                        MemoryStream ms = new MemoryStream();
                        await newEditCocktailVM.File.CopyToAsync(ms);

                        newEditCocktailVM.ImageData = ms.ToArray();

                        ms.Close();
                        ms.Dispose();
                    }

                    var cocktailDTO = cocktailDTOMapper.MapToDTOFromVM(newEditCocktailVM);

                    var validationResult = this.cocktailService.ValidateCocktail(cocktailDTO);

                    if (!validationResult.HasProperInputData)
                    {
                        this.toaster.AddWarningToastMessage(ToastrConsts.NullModel);
                        return(RedirectToAction(nameof(Edit), new { id }));
                    }
                    if (!validationResult.HasProperNameLength)
                    {
                        this.toaster.AddWarningToastMessage(ToastrConsts.WrongNameLength);
                        return(RedirectToAction(nameof(Edit), new { id }));
                    }
                    if (!validationResult.HasValidName)
                    {
                        this.toaster.AddWarningToastMessage(ToastrConsts.NameNotValid);
                        return(RedirectToAction(nameof(Edit), new { id }));
                    }

                    await this.cocktailService.UpdateCocktailAsync(id, cocktailDTO);

                    this.toaster.AddSuccessToastMessage(ToastrConsts.Success);
                    return(RedirectToAction(nameof(Details), new { id }));
                }
                catch (Exception)
                {
                    this.toaster.AddWarningToastMessage(ToastrConsts.GenericError);
                    return(RedirectToAction(nameof(Edit), new { id }));
                }
            }
            this.toaster.AddWarningToastMessage(ToastrConsts.IncorrectInput);
            return(RedirectToAction(nameof(Index)));
        }