public static CocktailIngredients CocktailIngredientDTOMapToModel(this CocktailIngredientsDTO cocktailIngredientDTO)
        {
            var cocktailIngredients = new CocktailIngredients();

            cocktailIngredients.CocktailId   = cocktailIngredientDTO.CocktailId;
            cocktailIngredients.IngredientId = cocktailIngredientDTO.IngredientId;
            cocktailIngredients.UnlistedOn   = cocktailIngredientDTO.UnlistedOn;
            //cocktailIngredientsDTO.Quantity = cocktailIngredient.Quantity;
            //cocktailIngredientsDTO.Uom = cocktailIngredient.Uom;
            return(cocktailIngredients);
        }
        public static CocktailIngredientsViewModel CocktailIngredientDTOMapToVM(this CocktailIngredientsDTO cocktailIngredientDTO)
        {
            var cocktailIngredients = new CocktailIngredientsViewModel();

            cocktailIngredients.CocktailId     = cocktailIngredientDTO.CocktailId;
            cocktailIngredients.IngredientId   = cocktailIngredientDTO.IngredientId;
            cocktailIngredients.CocktailName   = cocktailIngredientDTO.CocktailName;
            cocktailIngredients.IngredientName = cocktailIngredientDTO.IngredientName;
            //cocktailIngredientsDTO.Quantity = cocktailIngredient.Quantity;
            //cocktailIngredientsDTO.Uom = cocktailIngredient.Uom;
            return(cocktailIngredients);
        }
        public static CocktailIngredientsDTO CocktailIngredientMapToDTO(this CocktailIngredients cocktailIngredient)
        {
            var cocktailIngredientsDTO = new CocktailIngredientsDTO();

            cocktailIngredientsDTO.CocktailId     = cocktailIngredient.CocktailId;
            cocktailIngredientsDTO.CocktailName   = cocktailIngredient.Cocktail.Name;
            cocktailIngredientsDTO.IngredientId   = cocktailIngredient.IngredientId;
            cocktailIngredientsDTO.UnlistedOn     = cocktailIngredient.UnlistedOn;
            cocktailIngredientsDTO.IngredientName = cocktailIngredient.Ingredient.Name;
            //cocktailIngredientsDTO.Quantity = cocktailIngredient.Quantity;
            //cocktailIngredientsDTO.Uom = cocktailIngredient.Uom;

            return(cocktailIngredientsDTO);
        }
        public async Task <IActionResult> Create([Bind("Id,Name,Description")] CocktailViewModel cocktail,
                                                 List <IngredientViewModel> allIngredients)
        {
            if (ModelState.IsValid)
            {
                try
                {
                    cocktail.Id = Guid.NewGuid();
                    var cocktailDTO = new CocktailDTO
                    {
                        Id          = cocktail.Id,
                        Name        = cocktail.Name,
                        Description = cocktail.Description
                    };

                    var newCocktail = await cocktailService.CreateCocktail(cocktailDTO);

                    foreach (var ingredient in allIngredients)
                    {
                        if (ingredient.isSelected)
                        {
                            var cocktailIngredientDTO = new CocktailIngredientsDTO
                            {
                                CocktailId   = cocktail.Id,
                                IngredientId = ingredient.Id
                            };

                            await cocktailService.AddIngredientToCocktail(cocktailIngredientDTO);
                        }
                    }

                    _toastNotification.AddSuccessToastMessage($"Cocktail {newCocktail.Name} created successfully!");
                    return(RedirectToAction(nameof(Index)));
                }
                catch
                {
                    _toastNotification.AddErrorToastMessage("Cocktail cannot be created!");
                    return(RedirectToAction(nameof(Index)));
                }
            }
            return(View(cocktail));
        }
示例#5
0
        public async Task Return_Correct_CIDTO()
        {
            var options = Utils.GetOptions(nameof(Return_Correct_CIDTO));

            var ingredient = new Ingredient
            {
                Id          = Guid.Parse("4039e0f3-8d2d-43a5-a82b-477e42371cd6"),
                Name        = "Martini Extra Dry",
                Abv         = 0,
                Description = "",
                TypeId      = Guid.Parse("619ac43c-075a-47be-befc-c68249054b85"),
            };

            var cocktail = new Cocktail
            {
                Id          = Guid.Parse("9ef97551-87f6-40ce-a88b-6c0e876ccb51"),
                Name        = "Margarita",
                Description = "The Margarita is one of the most " +
                              "popular cocktails in North America—for good reason. " +
                              "Combining the tang of lime and the sweetness of o" +
                              "range liqueur with the distinctive strength of " +
                              "tequila, our classic Margarita strikes all of the right keys."
            };

            var cocktailIngredientDTO = new CocktailIngredientsDTO
            {
                CocktailId   = cocktail.Id,
                IngredientId = ingredient.Id,
            };

            using (var assertContext = new CMContext(options))
            {
                var sut    = new CocktailService(assertContext);
                var result = await sut.AddIngredientToCocktail(cocktailIngredientDTO);

                Assert.AreEqual(cocktailIngredientDTO.CocktailId, assertContext.
                                CocktailIngredients.First().CocktailId);
            }
        }
示例#6
0
        public void Return_CocktailIngredientVM()
        {
            var cocktailDTO = new CocktailDTO()
            {
                Id          = Guid.Parse("9ef97551-87f6-40ce-a88b-6c0e876ccb51"),
                Name        = "Margarita",
                Description = "The Margarita is one of the most " +
                              "popular cocktails in North America—for good reason. " +
                              "Combining the tang of lime and the sweetness of o" +
                              "range liqueur with the distinctive strength of " +
                              "tequila, our classic Margarita strikes all of the right keys."
            };

            var ingredientDTO = new IngredientDTO
            {
                Id          = Guid.Parse("1b98e50e-8314-4b1e-82df-491c3c8d086f"),
                Name        = "Gin",
                Abv         = 40,
                Description = "Gin is a distilled alcoholic drink that derives its " +
                              "predominant flavour from juniper berries (Juniperus communis). " +
                              "Gin is one of the broadest categories of spirits, all of various origins, styles," +
                              " and flavour profiles, that revolve around juniper as a common ingredient.",
                TypeId = Guid.Parse("27309394-4ac3-4dc6-a81a-c8e147e378f0"),
            };

            var ciDTO = new CocktailIngredientsDTO
            {
                CocktailId     = cocktailDTO.Id,
                IngredientId   = ingredientDTO.Id,
                CocktailName   = cocktailDTO.Name,
                IngredientName = ingredientDTO.Name,
            };

            var result = ciDTO.CocktailIngredientDTOMapToVM();

            Assert.IsInstanceOfType(result, typeof(CocktailIngredientsViewModel));
        }
        public async Task <CocktailIngredientsDTO> AddIngredientToCocktail(CocktailIngredientsDTO cocktailIngredientDTO)
        {
            if (cocktailIngredientDTO == null)
            {
                throw new ArgumentNullException("CocktailIngredient doesn't exist!");
            }

            if (cocktailIngredientDTO.UnlistedOn != null)
            {
                var cocktailIngredient = await _cmContext.CocktailIngredients
                                         .FirstOrDefaultAsync(ci => ci.CocktailId == cocktailIngredientDTO.CocktailId &&
                                                              ci.IngredientId == cocktailIngredientDTO.IngredientId);

                cocktailIngredient.UnlistedOn = null;
            }
            else
            {
                var cocktailIngredient = cocktailIngredientDTO.CocktailIngredientDTOMapToModel();
                await _cmContext.CocktailIngredients.AddAsync(cocktailIngredient);
            }
            await _cmContext.SaveChangesAsync();

            return(cocktailIngredientDTO);
        }
        public async Task <IActionResult> Edit(Guid id, [Bind("Id,Name,Description")] CocktailViewModel cocktail,
                                               List <IngredientViewModel> allIngredients)
        {
            if (id != cocktail.Id)
            {
                return(NotFound());
            }

            if (ModelState.IsValid)
            {
                try
                {
                    await cocktailService.UpdateCocktail(cocktail.Id, cocktail.Name, cocktail.Description);

                    foreach (var ingredient in allIngredients)
                    {
                        if (ingredient.isSelected)
                        {
                            var isUnlisted = await cocktailService.IngredientIsUnlisted(cocktail.Id, ingredient.Id);

                            if (isUnlisted == true)
                            {
                                var existingCIDTO = await cocktailService.GetCocktailIngredient(cocktail.Id, ingredient.Id);

                                await cocktailService.AddIngredientToCocktail(existingCIDTO);
                            }
                            else
                            {
                                var existingCIDTO = await cocktailService.GetCocktailIngredient(cocktail.Id, ingredient.Id);

                                if (existingCIDTO == null)
                                {
                                    var cocktailIngredientDTO = new CocktailIngredientsDTO
                                    {
                                        CocktailId   = cocktail.Id,
                                        IngredientId = ingredient.Id
                                    };
                                    await cocktailService.AddIngredientToCocktail(cocktailIngredientDTO);
                                }
                            }
                        }
                        else
                        {
                            var existingCIDTO = await cocktailService.GetCocktailIngredient(cocktail.Id, ingredient.Id);

                            if (existingCIDTO != null)
                            {
                                var isUnlisted = await cocktailService.IngredientIsUnlisted(cocktail.Id, ingredient.Id);

                                if (isUnlisted == false)
                                {
                                    await cocktailService.RemoveIngredientFromCocktail(cocktail.Id, ingredient.Id);
                                }
                            }
                        }
                    }
                }
                catch
                {
                    _toastNotification.AddErrorToastMessage("Cocktail cannot be edited!");
                    return(RedirectToAction(nameof(Index)));
                }

                _toastNotification.AddSuccessToastMessage($"Cocktail {cocktail.Name} edited successfully!");
                return(RedirectToAction(nameof(Index)));
            }
            return(View(cocktail));
        }