示例#1
0
        public async Task <List <CocktailInListDTO> > SearchAsync(CocktailSearchDTO dto, int itemsPerPage, int currentPage)
        {
            var resultDTO = await context.Cocktails
                            .Include(c => c.CocktailIngredients)
                            .Include(c => c.CocktailReviews)
                            .Where(c => c.IsDeleted == false)
                            .FilterByName(dto.NameKey)
                            .FilterByIngredient(dto.IngredientId)
                            .FilterByRating(dto.MinRating)
                            .Select(c => new CocktailInListDTO
            {
                Id            = c.Id,
                Name          = c.Name,
                ImagePath     = c.ImagePath,
                AverageRating = c.CocktailReviews
                                .Where(r => r.Rating != null)
                                .Select(r => r.Rating)
                                .Average()
            })
                            .Skip((currentPage - 1) * itemsPerPage)
                            .Take(itemsPerPage)
                            .ToListAsync();

            return(resultDTO);
        }
        public async Task <IActionResult> Search(int id, [FromQuery] CocktailSearchViewModel vm)
        {
            var allIngredients = (await ingredientServices.GetAllDTOAsync());

            vm.AllIngredients = new List <SelectListItem>();
            vm.AllIngredients.Add(new SelectListItem("Select...", ""));

            allIngredients.ForEach(c => vm.AllIngredients.Add(new SelectListItem(c.Name, c.Id.ToString())));

            if (string.IsNullOrEmpty(vm.NameKey) && vm.MinRating == null && vm.IngredientId == null)
            {
                return(View(vm));
            }

            var searchParameters = new CocktailSearchDTO
            {
                NameKey      = vm.NameKey,
                MinRating    = vm.MinRating,
                IngredientId = vm.IngredientId
            };

            vm.Paging.Count = await cocktailServices.SerchResultCountAsync(searchParameters);

            vm.Paging.ItemsPerPage = itemsPerPage;
            vm.Paging.CurrentPage  = id == 0 ? 1 : id;

            vm.Result = (await cocktailServices.SearchAsync(searchParameters, itemsPerPage, vm.Paging.CurrentPage))
                        .Select(b => b.MapToViewModel());

            return(View(vm));
        }
示例#3
0
        public async Task <int> SerchResultCountAsync(CocktailSearchDTO dto)
        {
            var resultCount = await context.Cocktails
                              .Include(c => c.CocktailIngredients)
                              .Include(c => c.CocktailReviews)
                              .Where(c => c.IsDeleted == false)
                              .FilterByName(dto.NameKey)
                              .FilterByIngredient(dto.IngredientId)
                              .FilterByRating(dto.MinRating)
                              .Select(c => new CocktailInListDTO
            {
                Id            = c.Id,
                Name          = c.Name,
                ImagePath     = c.ImagePath,
                AverageRating = c.CocktailReviews
                                .Where(r => r.Rating != null)
                                .Select(r => r.Rating)
                                .Average()
            })
                              .CountAsync();

            return(resultCount);
        }
示例#4
0
        public async Task ReturnCount_WhenFilterByMultipleParameters()
        {
            var cocktailFactoryMock           = new Mock <ICocktailFactory>();
            var cocktailIngredinetFactoryMock = new Mock <ICocktailIngredientFactory>();
            var barCocktailFactoryMock        = new Mock <IBarCocktailFactory>();

            var cocktail1NameTest = "abCd";
            var cocktail2NameTest = "ghjg";
            var cocktail3NameTest = "Bcgh";
            var cocktail4NameTest = "kjgh";

            var imageURLTest = "https://www.google.com/";

            var ingr1Name = "Ingr1";
            var ingr2Name = "Ingr2";
            var ingr3Name = "Ingr3";

            var ingrUnitTest = "Unit";
            var quantityTest = 0.5;

            var ingredient1 = new Ingredient
            {
                Name = ingr1Name,
                Unit = ingrUnitTest
            };
            var ingredient2 = new Ingredient
            {
                Name = ingr2Name,
                Unit = ingrUnitTest
            };
            var ingredient3 = new Ingredient
            {
                Name = ingr3Name,
                Unit = ingrUnitTest
            };

            var ingredientToSearch = ingredient2;
            var substringToSearh   = "BC";
            var minRatingToSearch  = 4;

            var cocktail1 = new Cocktail
            {
                Name            = cocktail1NameTest,
                ImagePath       = imageURLTest,
                CocktailReviews = new List <CocktailReview>
                {
                    new CocktailReview
                    {
                        Rating = 3
                    }
                }
            };
            var cocktail2 = new Cocktail
            {
                Name            = cocktail2NameTest,
                ImagePath       = imageURLTest,
                CocktailReviews = new List <CocktailReview>
                {
                    new CocktailReview
                    {
                        Rating = 4
                    }
                }
            };
            var cocktail3 = new Cocktail
            {
                Name            = cocktail3NameTest,
                ImagePath       = imageURLTest,
                CocktailReviews = new List <CocktailReview>
                {
                    new CocktailReview
                    {
                        Rating = 4
                    }
                }
            };
            var cocktail4 = new Cocktail
            {
                Name            = cocktail4NameTest,
                ImagePath       = imageURLTest,
                CocktailReviews = new List <CocktailReview>
                {
                    new CocktailReview
                    {
                        Rating = 5
                    }
                }
            };

            var options = TestUtilities.GetOptions(nameof(ReturnCount_WhenFilterByMultipleParameters));

            using (var arrangeContext = new CocktailMagicianDb(options))
            {
                arrangeContext.CocktailIngredients.AddRange(new List <CocktailIngredient>
                {
                    new CocktailIngredient
                    {
                        Cocktail   = cocktail1,
                        Ingredient = ingredient1,
                        Quatity    = quantityTest
                    },
                    new CocktailIngredient
                    {
                        Cocktail   = cocktail2,
                        Ingredient = ingredient1,
                        Quatity    = quantityTest
                    },
                    new CocktailIngredient
                    {
                        Cocktail   = cocktail2,
                        Ingredient = ingredient2,
                        Quatity    = quantityTest
                    },
                    new CocktailIngredient
                    {
                        Cocktail   = cocktail3,
                        Ingredient = ingredient2,
                        Quatity    = quantityTest
                    },
                    new CocktailIngredient
                    {
                        Cocktail   = cocktail4,
                        Ingredient = ingredient2,
                        Quatity    = quantityTest
                    },
                    new CocktailIngredient
                    {
                        Cocktail   = cocktail4,
                        Ingredient = ingredient3,
                        Quatity    = quantityTest
                    }
                });

                await arrangeContext.SaveChangesAsync();
            }
            using (var assertContext = new CocktailMagicianDb(options))
            {
                var ingredientId = await assertContext.Ingredients.Where(i => i.Name == ingr2Name).Select(i => i.Id).FirstAsync();

                var sut = new CocktailServices(assertContext, cocktailFactoryMock.Object, cocktailIngredinetFactoryMock.Object, barCocktailFactoryMock.Object);

                var DTOTest = new CocktailSearchDTO
                {
                    NameKey      = substringToSearh,
                    IngredientId = ingredientId,
                    MinRating    = minRatingToSearch
                };
                var resultCount = await sut.SerchResultCountAsync(DTOTest);

                Assert.AreEqual(1, resultCount);
            }
        }
        public async Task FilterByRating()
        {
            var cocktailFactoryMock           = new Mock <ICocktailFactory>();
            var cocktailIngredinetFactoryMock = new Mock <ICocktailIngredientFactory>();
            var barCocktailFactoryMock        = new Mock <IBarCocktailFactory>();

            var minRatingToFilter = 4;

            var cocktail1NameTest = "abCd";
            var cocktail2NameTest = "ghjg";
            var cocktail3NameTest = "Bcgh";
            var cocktail4NameTest = "kjgh";

            var imageURLTest = "https://www.google.com/";

            var ingr1Name = "Ingr1";
            var ingr2Name = "Ingr2";
            var ingr3Name = "Ingr3";

            var ingrUnitTest = "Unit";
            var quantityTest = 0.5;

            var ingredient1 = new Ingredient
            {
                Name = ingr1Name,
                Unit = ingrUnitTest
            };
            var ingredient2 = new Ingredient
            {
                Name = ingr2Name,
                Unit = ingrUnitTest
            };
            var ingredient3 = new Ingredient
            {
                Name = ingr3Name,
                Unit = ingrUnitTest
            };

            var cocktail1 = new Cocktail
            {
                Name            = cocktail1NameTest,
                ImagePath       = imageURLTest,
                CocktailReviews = new List <CocktailReview>
                {
                    new CocktailReview
                    {
                        Rating = 3
                    }
                }
            };
            var cocktail2 = new Cocktail
            {
                Name            = cocktail2NameTest,
                ImagePath       = imageURLTest,
                CocktailReviews = new List <CocktailReview>
                {
                    new CocktailReview
                    {
                        Rating = 4
                    }
                }
            };
            var cocktail3 = new Cocktail
            {
                Name            = cocktail3NameTest,
                ImagePath       = imageURLTest,
                CocktailReviews = new List <CocktailReview>
                {
                    new CocktailReview
                    {
                        Rating = 4
                    }
                }
            };
            var cocktail4 = new Cocktail
            {
                Name            = cocktail4NameTest,
                ImagePath       = imageURLTest,
                CocktailReviews = new List <CocktailReview>
                {
                    new CocktailReview
                    {
                        Rating = 5
                    }
                }
            };

            var options = TestUtilities.GetOptions(nameof(FilterByRating));

            using (var arrangeContext = new CocktailMagicianDb(options))
            {
                arrangeContext.CocktailIngredients.AddRange(new List <CocktailIngredient>
                {
                    new CocktailIngredient
                    {
                        Cocktail   = cocktail1,
                        Ingredient = ingredient1,
                        Quatity    = quantityTest
                    },
                    new CocktailIngredient
                    {
                        Cocktail   = cocktail2,
                        Ingredient = ingredient1,
                        Quatity    = quantityTest
                    },
                    new CocktailIngredient
                    {
                        Cocktail   = cocktail2,
                        Ingredient = ingredient2,
                        Quatity    = quantityTest
                    },
                    new CocktailIngredient
                    {
                        Cocktail   = cocktail3,
                        Ingredient = ingredient3,
                        Quatity    = quantityTest
                    },
                    new CocktailIngredient
                    {
                        Cocktail   = cocktail4,
                        Ingredient = ingredient2,
                        Quatity    = quantityTest
                    },
                    new CocktailIngredient
                    {
                        Cocktail   = cocktail4,
                        Ingredient = ingredient3,
                        Quatity    = quantityTest
                    }
                });

                await arrangeContext.SaveChangesAsync();
            }
            using (var assertContext = new CocktailMagicianDb(options))
            {
                var sut     = new CocktailServices(assertContext, cocktailFactoryMock.Object, cocktailIngredinetFactoryMock.Object, barCocktailFactoryMock.Object);
                var DTOTest = new CocktailSearchDTO
                {
                    MinRating = minRatingToFilter
                };
                var result = await sut.SearchAsync(DTOTest, 5, 1);

                Assert.AreEqual(3, result.Count());
                Assert.IsFalse(result.Any(c => c.AverageRating < minRatingToFilter));
            }
        }