Beispiel #1
0
        public List <Ad> GetAdGridByFilters(string searchString, FilterRepositoryModel filterModel)
        {
            var ads = _context.Ads.Include(c => c.Category)
                      .Where(c => c.Price <= filterModel.MaximumPrice &&
                             c.Price >= filterModel.MinimumPrice &&
                             c.CountryID == filterModel.CountryId &&
                             c.Name.Contains(searchString))
                      .Include(c => c.City)
                      .Include(o => o.Owner)
                      .Include(i => i.AdDetail)
                      ?.ThenInclude(t => t.AdDetailPictures)
                      .ToList();

            List <Ad> items = new List <Ad>();

            if (filterModel.CityIds.Count > 0)
            {
                ads = ads.Where(c => filterModel.CityIds.Contains(c.CityID))
                      .ToList();
            }
            if (filterModel.CategoryIds.Count > 0)
            {
                ads = ads.Where(c => filterModel.CategoryIds.Contains(c.CategoryID)).ToList();
            }

            if (ads == null)
            {
                throw new Exception();
            }

            return(ads);
        }
Beispiel #2
0
        public List <AdGridViewModel> GetPageForAdGridByFilter(PageModel pageModel, FilterModel filterModel, out int count, out int maxPrice)
        {
            //business rule : retrieve subcategories for current category and apply to filtering
            List <Guid> categoryIds = new List <Guid>();

            if (filterModel.CategoryId != null && filterModel.CategoryId != Guid.Empty)
            {
                categoryIds = _categoryRepository.GetSubcategoriesIdForCategory((Guid)filterModel.CategoryId, pageModel.CountryId);
            }

            FilterRepositoryModel repositoryModel = FilterModelMapper.MapToFilterRepositoryModel(filterModel, categoryIds, pageModel.CountryId);
            List <Ad>             sourceEntities;

            switch (pageModel.ColumnName.ToLower())
            {
            case "name":
                sourceEntities = _repository.GetAdGridByFilters(pageModel.SearchString, repositoryModel);
                break;

            default:
                sourceEntities = _repository.GetAdGridByFilters(repositoryModel);
                break;
            }

            if (sourceEntities == null)
            {
                throw new Exception(LOCALIZATION_AD_NOT_FOUND);
            }
            sourceEntities = _repository.OrderAdsByColumn(sourceEntities, pageModel.SortColumn);
            maxPrice       = (int)_repository.GetMaximumPriceForAd();
            count          = sourceEntities.Count;

            var entities = _repository.GetAdPage(sourceEntities, pageModel.PageIndex, pageModel.PageSize);

            if (entities == null)
            {
                throw new Exception(LOCALIZATION_AD_NOT_FOUND);
            }

            var list = AdMapper.MapToAdGridModel(entities);

            foreach (var ad in list)
            {
                ad.IsInWishlist = _wishlistRepository.Exists(ad.ID, pageModel.CurrentUser);
            }
            return(list);
        }
        public static FilterRepositoryModel MapToFilterRepositoryModel(FilterModel filterModel, List <Guid> categoryIds, Guid countryId)
        {
            if (filterModel.CityIds == null)
            {
                filterModel.CityIds = new List <Guid>();
            }

            int maxPrice = filterModel.MaximumPrice <= 0 ? MAX_NUMBER : filterModel.MaximumPrice;

            var filterRespositoryModel = new FilterRepositoryModel()
            {
                CategoryIds  = categoryIds,
                CityIds      = filterModel.CityIds,
                MinimumPrice = filterModel.MinimumPrice,
                MaximumPrice = maxPrice,
                CountryId    = countryId
            };

            return(filterRespositoryModel);
        }