public void TestFilterStyles(StylesFilterDTO filter)
        {
            // arrange
            _dbContext      = _dbContext ?? Configurations.GetDbContext();
            _mapperProvider = _mapperProvider ?? Configurations.GetMapperProvider();
            _mapper         = _mapper ?? Configurations.GetMapper();

            // act
            var result = new StyleRepo(_dbContext, _mapperProvider, _mapper).FilterStyles(filter);

            // assert
            Assert.IsType <StylesFilteredDTO>(result);

            Assert.True(result.TotalCount == 1);

            Assert.True(result.BrandCounts.Count() == 7);
            Assert.All(result.BrandCounts, brandCount =>
            {
                Assert.True(brandCount.BrandId > 0);
                Assert.False(string.IsNullOrEmpty(brandCount.BrandName));
                Assert.True(brandCount.BrandCount >= 0);
            });

            Assert.True(result.GenderCounts.Count() == 3);
            Assert.All(result.GenderCounts, genderCount =>
            {
                Assert.True(genderCount.GenderId > 0);
                Assert.False(string.IsNullOrEmpty(genderCount.GenderName));
                Assert.True(genderCount.GenderCount >= 0);
            });

            Assert.True(result.IdealForCounts.Count() == 4);
            Assert.All(result.IdealForCounts, idealForCount =>
            {
                Assert.True(idealForCount.IdealForId > 0);
                Assert.False(string.IsNullOrEmpty(idealForCount.IdealForSpec));
                Assert.True(idealForCount.IdealForCount >= 0);
            });

            Assert.True(result.StylesFiltered.Count() == 1);
            Assert.All(result.StylesFiltered, style =>
            {
                Assert.True(style.StyleId > 0);
                Assert.False(string.IsNullOrEmpty(style.StyleName));
                Assert.True(style.CategoryId > 0);
                Assert.False(string.IsNullOrEmpty(style.BrandName));
                Assert.False(string.IsNullOrEmpty(style.GenderName));
                Assert.False(string.IsNullOrEmpty(style.ImageSmall));
                Assert.True(style.PriceCurrent > 0);
                Assert.True(style.PriceRegular > 0);
                Assert.True(style.AverageRatings >= 0);
                Assert.True(style.ReviewCount >= 0);
                Assert.IsType <bool>(style.SoldOut);
            });
        }
Example #2
0
        public StylesFilteredDTO FilterStyles(StylesFilterDTO filter)
        {
            var result = new StylesFilteredDTO();

            var styleIds = _DbContext.VwStyleIdealFors
                           .Where(style => style.CategoryId == filter.CategoryId &&
                                  (filter.BrandIds == null || !filter.BrandIds.Any() || filter.BrandIds.Contains(style.BrandId)) &&
                                  (filter.GenderIds == null || !filter.GenderIds.Any() || filter.GenderIds.Contains(style.GenderId)) &&
                                  (filter.IdealForIds == null || !filter.IdealForIds.Any() || filter.IdealForIds.Contains(style.IdealForId)))
                           .Select(style => style.StyleId).Distinct();

            result.TotalCount = styleIds.Count();

            var brands = _DbContext.VwStyles
                         .Where(style => style.CategoryId == filter.CategoryId)
                         .Select(style => new
            {
                style.BrandId,
                style.BrandName
            }).Distinct();

            var genders = _DbContext.VwStyles
                          .Where(style => style.CategoryId == filter.CategoryId)
                          .Select(style => new
            {
                style.GenderId,
                style.GenderName
            }).Distinct();

            var idealFors = _DbContext.VwStyleIdealFors
                            .Where(style => style.CategoryId == filter.CategoryId)
                            .Select(style => new
            {
                style.IdealForId,
                style.IdealForSpec
            }).Distinct();

            result.BrandCounts = brands.GroupJoin(_DbContext.VwStyleIdealFors,
                                                  brand => brand.BrandId,
                                                  style => style.BrandId,
                                                  (brand, style) => new
            {
                Brand = brand, Style = style.Where(s => s.CategoryId == filter.CategoryId &&
                                                   (filter.GenderIds == null || !filter.GenderIds.Any() || filter.GenderIds.Contains(s.GenderId)) &&
                                                   (filter.IdealForIds == null || !filter.IdealForIds.Any() || filter.IdealForIds.Contains(s.IdealForId)))
                                       .Select(s => s.StyleId).Distinct()
            })
                                 .SelectMany(
                x => x.Style.DefaultIfEmpty(),
                (x, y) => new BrandCountDTO
            {
                BrandId    = x.Brand.BrandId,
                BrandName  = x.Brand.BrandName,
                BrandCount = x.Style.Count()
            }
                )
                                 .GroupBy(x => x.BrandId)
                                 .Select(y => y.FirstOrDefault());

            result.GenderCounts = genders.GroupJoin(_DbContext.VwStyleIdealFors,
                                                    gender => gender.GenderId,
                                                    style => style.GenderId,
                                                    (gender, style) => new
            {
                Gender = gender, Style = style.Where(s => s.CategoryId == filter.CategoryId &&
                                                     (filter.BrandIds == null || !filter.BrandIds.Any() || filter.BrandIds.Contains(s.BrandId)) &&
                                                     (filter.IdealForIds == null || !filter.IdealForIds.Any() || filter.IdealForIds.Contains(s.IdealForId)))
                                         .Select(s => s.StyleId).Distinct()
            })
                                  .SelectMany(
                x => x.Style.DefaultIfEmpty(),
                (x, y) => new GenderCountDTO
            {
                GenderId    = x.Gender.GenderId,
                GenderName  = x.Gender.GenderName,
                GenderCount = x.Style.Count()
            }
                )
                                  .GroupBy(x => x.GenderId)
                                  .Select(y => y.FirstOrDefault());

            result.IdealForCounts = idealFors.GroupJoin(_DbContext.VwStyleIdealFors,
                                                        idealFor => idealFor.IdealForId,
                                                        style => style.IdealForId,
                                                        (idealFor, style) => new
            {
                IdealFor = idealFor, Style = style.Where(s => s.CategoryId == filter.CategoryId &&
                                                         (filter.BrandIds == null || !filter.BrandIds.Any() || filter.BrandIds.Contains(s.BrandId)) &&
                                                         (filter.GenderIds == null || !filter.GenderIds.Any() || filter.GenderIds.Contains(s.GenderId)))
                                             .Select(s => s.StyleId).Distinct()
            })
                                    .SelectMany(
                x => x.Style.DefaultIfEmpty(),
                (x, y) => new IdealForCountDTO
            {
                IdealForId    = x.IdealFor.IdealForId,
                IdealForSpec  = x.IdealFor.IdealForSpec,
                IdealForCount = x.Style.Count()
            }
                )
                                    .GroupBy(x => x.IdealForId)
                                    .Select(y => y.FirstOrDefault());

            var stylesFiltered = _DbContext.VwStyles.Where(style => styleIds.Contains(style.StyleId));

            if (filter.Sort == 1)
            {
                stylesFiltered = stylesFiltered.OrderBy(style => style.PriceCurrent);
            }
            else if (filter.Sort == 2)
            {
                stylesFiltered = stylesFiltered.OrderByDescending(style => style.PriceCurrent);
            }

            result.StylesFiltered = stylesFiltered.Skip((filter.PageNumber - 1) * filter.PageSize)
                                    .Take(filter.PageSize).ProjectTo <StyleForListDTO>(_mapperProvider);

            return(result);
        }
        public ActionResult <StylesFilteredDTO> GetStylesFiltered([FromQuery] StylesFilterDTO filter)
        {
            var result = _styleRepo.FilterStyles(filter);

            return(Ok(result));
        }