public async Task <IActionResult> Create(CreateCocktailViewModel cocktailVm)
        {
            var ingr = await _ingredientServices.GetAllIngredientsNames();

            if (ModelState.IsValid)
            {
                //TODO -refactor!
                // validation if there is no Picture!
                try
                {
                    //var imageSizeInKb = cocktailVm.CocktailImage.Length / 1024;
                    //var type = cocktailVm.CocktailImage.ContentType;
                    var cocktailDto = cocktailVm.MapToCocktailDTO();
                    await _cocktailServices.AddCocktail(cocktailDto);

                    string id = this.User.FindFirstValue(ClaimTypes.NameIdentifier);
                    await _notificationServices.CocktailCreateNotificationToAdminAsync(id, cocktailDto.Name);

                    _toast.AddSuccessToastMessage($"You successfully added cocktail {cocktailDto.Name}!");
                    return(RedirectToAction("ListCocktails"));
                }
                catch (Exception ex)
                {
                    _toast.AddErrorToastMessage(ex.Message);
                    ViewBag.ErrorTitle = "";
                    return(View("Error"));
                }
            }
            cocktailVm.IngredientsNames.Add(new SelectListItem("Choose an igredient", ""));
            cocktailVm.IngredientsNames.AddRange(ingr.Select(i => new SelectListItem(i, i)));
            return(View(cocktailVm));
        }
コード例 #2
0
        public async Task <IActionResult> Create(CreateCocktailViewModel model)
        {
            if (model.CocktailViewModel.Name == null ||
                model.CocktailViewModel.ShortDescription == null ||
                model.CocktailViewModel.LongDescription == null ||
                model.CocktailViewModel.Ingredients == null)
            {
                return(RedirectToAction("List", "Cocktails", new { area = "" }));
            }

            if (ModelState.IsValid)
            {
                if (model.Photo != null)
                {
                    string uploadsFolder = Path.Combine(_webHostEnvironment.WebRootPath, "storage\\images\\cocktails");
                    model.CocktailViewModel.ImageUrl = Guid.NewGuid().ToString() + " " + model.Photo.FileName;
                    string filePath = Path.Combine(uploadsFolder, model.CocktailViewModel.ImageUrl.ToString());
                    model.Photo.CopyTo(new FileStream(filePath, FileMode.Create));
                }
                var cocktailDto = this._cocktailVmMapper.MapDTO(model.CocktailViewModel);
                await this._cocktailService.CreateCocktailAsync(cocktailDto);

                return(RedirectToAction("List", "Cocktails", new { area = "" }));
            }

            return(RedirectToAction("List", "Cocktails", new { area = "" }));
        }
コード例 #3
0
        public async Task <IActionResult> Create(CreateCocktailViewModel viewModel)
        {
            var dto = this.createCocktailMapper.MapFromViewModel(viewModel);

            await this.cocktailService.CreateCocktail(dto);

            return(Ok());
        }
コード例 #4
0
        public async Task <IActionResult> Edit(CreateCocktailViewModel viewModel)
        {
            var cocktailDTO = this.createCocktailMapper.MapFromViewModel(viewModel);
            await cocktailService.EditCocktailAsync(cocktailDTO);

            //TODO remove ok
            return(Ok());
        }
コード例 #5
0
        public async Task <IViewComponentResult> InvokeAsync()
        {
            var allIngredients = await ingredientService.GetIngredientsAsync();

            var model = new CreateCocktailViewModel
            {
                AllAvailableIngredients = allIngredients
                                          .Select(b => new SelectListItem(b.Name, b.Name))
                                          .ToList()
            };

            return(View(model));
        }
コード例 #6
0
        public async Task <IActionResult> Create()
        {
            var allIngredients = await _ingredientService.GetAllIngredientsAsync();

            var model = new CreateCocktailViewModel
            {
                AllIngredients = allIngredients
                                 .Select(i => new SelectListItem(i.Name, i.Name))
                                 .ToList()
            };

            return(View(model));
        }
コード例 #7
0
        public async Task <IActionResult> Create()
        {
            var createCocktail = new CreateCocktailViewModel();

            createCocktail.Ingredients = new List <string>();
            var listOfIngredients = await _ingredientManager.GetAllIngredientsAsync();

            foreach (var item in listOfIngredients)
            {
                createCocktail.Ingredients.Add(item.Name);
            }
            return(View(createCocktail));
        }
コード例 #8
0
        public async Task <IActionResult> Create()
        {
            var allIngredients = (await ingredientService.GetAllIngredients()).Select(i => new SelectListItem(i.Name, i.Id)).ToList();
            var unitTypes      = (await ingredientService.GetAllIngredients()).Select(i => new SelectListItem(i.UnitType, i.Id)).ToList();
            var a         = (await ingredientService.GetAllIngredients()).Select(i => this.ingredientMapper.MapFromDTO(i)).ToList();
            var viewModel = new CreateCocktailViewModel
            {
                SpecificIngredients = new List <AddIngredientToCocktailViewModel>(),
                AllIngredients      = allIngredients,
                AllUnitTypes        = unitTypes,
                AllAllIngredients   = a
            };

            return(View(viewModel));
        }
コード例 #9
0
        public static CocktailDto MapToCocktailDTO(this CreateCocktailViewModel cocktailVM)
        {
            var cocktailDto = new CocktailDto();

            cocktailDto.Id            = cocktailVM.Id;
            cocktailDto.Name          = cocktailVM.Name;
            cocktailDto.Image         = cocktailVM.Image;
            cocktailDto.CocktailImage = cocktailVM.CocktailImage;
            cocktailDto.Ingredients   = cocktailVM.Ingredients
                                        .Where(i => i.Ingredient != null)
                                        .Select(
                i => i.MapCockTailComponentVMToDTo()
                ).ToList();
            cocktailDto.Recipe = cocktailVM.Recipe;
            return(cocktailDto);
        }
        public async Task <IActionResult> Create()
        {
            try
            {
                //review this block
                var ingr = await _ingredientServices.GetAllIngredientsNames();

                var createCocktailVM = new CreateCocktailViewModel();
                createCocktailVM.IngredientsNames.Add(new SelectListItem("Choose an igredient", ""));
                createCocktailVM.IngredientsNames.AddRange(ingr.Select(i => new SelectListItem(i, i)));
                return(View(createCocktailVM));
            }
            catch (Exception ex)
            {
                _toast.AddErrorToastMessage(ex.Message);
                ViewBag.ErrorTitle = "";
                return(View("Error"));
            }
        }
コード例 #11
0
        public async Task <IActionResult> Create(CreateCocktailViewModel cocktailToCreate)
        {
            if (string.IsNullOrWhiteSpace(cocktailToCreate.Cocktail?.Name) &&
                string.IsNullOrWhiteSpace(cocktailToCreate.Cocktail?.Picture) &&
                !cocktailToCreate.Cocktail.Ingredients.Any())
            {
                ModelState.AddModelError(string.Empty, "Invalid bar parameters!");
                return(RedirectToAction("Create"));
            }
            try
            {
                var cocktailToAdd = cocktailToCreate.Cocktail.ToDTO();
                await _cocktailManager.CreateCocktail(cocktailToAdd, cocktailToCreate.Cocktail.Ingredients);

                return(RedirectToAction("Index", "Home"));
            }
            catch (Exception ex)
            {
                return(RedirectToAction("ErrorAction", "Error", new { errorCode = "500", errorMessage = ex.Message }));
            }
        }
コード例 #12
0
        public async Task <IActionResult> Create(CreateCocktailViewModel model)
        {
            if (ModelState.IsValid)
            {
                if (model.CocktailViewModel.File == null)
                {
                    this.toastNotification.AddWarningToastMessage("Please upload a valid image");
                    return(RedirectToAction("Index", "Cocktails", new { area = "" }));
                }

                var cocktailUploadsFolder = System.IO.Path.Combine(Directory.GetCurrentDirectory(), "wwwroot\\assets\\img\\cocktails");
                this.fileServiceProvider.CreateFolder(cocktailUploadsFolder);
                var    newFileName  = $"{Guid.NewGuid()}_{model.CocktailViewModel.File.FileName}";
                string fullFilePath = Path.Combine(cocktailUploadsFolder, newFileName);
                string cocktailDBImageLocationPath = $"/assets/img/cocktails/{newFileName}";
                model.CocktailViewModel.ImagePath = cocktailDBImageLocationPath;

                using (var stream = new FileStream(fullFilePath, FileMode.Create))
                {
                    using (var memoryStream = new MemoryStream())
                    {
                        await model.CocktailViewModel.File.CopyToAsync(memoryStream);

                        var x = memoryStream.ToArray();
                        stream.Write(x, 0, x.Length);
                    }
                }

                var tempCocktail = this.cocktailViewModelMapper.MapFrom(model.CocktailViewModel);
                var cocktailDto  = await this.cocktailService.CreateAsync(tempCocktail);

                this.toastNotification.AddSuccessToastMessage("Cocktail successfully created");
                return(RedirectToAction("Details", new { id = cocktailDto.Id }));
            }

            this.toastNotification.AddWarningToastMessage("Incorrect input, please try again");
            ModelState.AddModelError(string.Empty, ExceptionMessages.ModelError);
            return(RedirectToAction("Index", "Cocktails", new { area = "" }));
        }
コード例 #13
0
        public CocktailDTO MapToDTOFromVM(CreateCocktailViewModel createCocktailVM)
        {
            if (createCocktailVM == null)
            {
                return(null);
            }
            var cocktailDTO = new CocktailDTO
            {
                Name        = createCocktailVM.Name,
                Ingredients = createCocktailVM.ContainedIngredients.Select(i => new IngredientDTO
                {
                    Id = i
                }).ToList(),
            };

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

            return(cocktailDTO);
        }
コード例 #14
0
        public async Task <IActionResult> Create(CreateCocktailViewModel createCocktailViewModel)
        {
            var sanitizer = new HtmlSanitizer();

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

            if (createCocktailViewModel.File == null)
            {
                this.toaster.AddWarningToastMessage(ToastrConsts.NoPicture);
                return(RedirectToAction(nameof(Create)));
            }

            if (ModelState.IsValid)
            {
                try
                {
                    if (createCocktailViewModel.File.Length > 0)
                    {
                        MemoryStream ms = new MemoryStream();
                        await createCocktailViewModel.File.CopyToAsync(ms);

                        createCocktailViewModel.ImageData = ms.ToArray();

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

                    var cocktailDTO = this.cocktailDTOMapper.MapToDTOFromVM(createCocktailViewModel);

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

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

                    bool isUnique = this.cocktailService.CocktailIsUnique(cocktailDTO);

                    if (!isUnique)
                    {
                        this.toaster.AddWarningToastMessage(ToastrConsts.NotUnique);
                        return(RedirectToAction(nameof(Create)));
                    }

                    var currentUserId = int.Parse(userManager.GetUserId(HttpContext.User));
                    cocktailDTO.CreatorId = currentUserId;

                    await this.cocktailService.CreateCocktailAsync(cocktailDTO);

                    this.toaster.AddSuccessToastMessage(ToastrConsts.Success);
                    return(RedirectToAction(nameof(Index)));
                }
                catch (Exception)
                {
                    this.toaster.AddWarningToastMessage(ToastrConsts.GenericError);
                    return(RedirectToAction(nameof(Create)));
                }
            }
            this.toaster.AddWarningToastMessage(ToastrConsts.IncorrectInput);
            return(RedirectToAction(nameof(Index)));
        }