public async Task<ActionResult> Index(string id)
        {
            if (!string.IsNullOrEmpty(id))
                return RedirectToAction("ContentPage", "Home", new { id = id.ToLowerInvariant() });

            var model = new SearchListingModel();
            await GetSearchResult(model);

            return View(model);
        }
        private async Task GetSearchResult(SearchListingModel model)
        {
            IEnumerable<Item> items = null;

            // Category
            if (model.CategoryID != 0)
                items = await _itemService.Query(x => x.CategoryID == model.CategoryID).Include(x => x.ItemPictures).Include(x => x.Category).SelectAsync();

            // Search Text
            if (!string.IsNullOrEmpty(model.SearchText))
            {
                model.SearchText = model.SearchText.ToLower();

                if (items != null)
                    items = items.Where(x => x.Title.ToLower().Contains(model.SearchText));
                else
                    items = await _itemService.Query(x => x.Title.ToLower().Contains(model.SearchText)).Include(x => x.ItemPictures).Include(x => x.Category).SelectAsync();
            }

            // Latest
            if (items == null)
                items = await _itemService.Query().OrderBy(x => x.OrderByDescending(y => y.Created)).Include(x => x.ItemPictures).Include(x => x.Category).SelectAsync();

            // Location
            if (!string.IsNullOrEmpty(model.Location))
            {
                items = items.Where(x => !string.IsNullOrEmpty(x.Location) && x.Location.IndexOf(model.Location, StringComparison.OrdinalIgnoreCase) != -1);
            }

            // Picture
            if (model.PhotoOnly)
                items = items.Where(x => x.ItemPictures.Count > 0);

            /// Price
            if (model.PriceFrom.HasValue)
                items = items.Where(x => x.Price >= model.PriceFrom.Value);

            if (model.PriceTo.HasValue)
                items = items.Where(x => x.Price <= model.PriceTo.Value);

            var itemsModelList = new List<ItemModel>();
            foreach (var item in items.OrderByDescending(x => x.Created))
            {
                itemsModelList.Add(new ItemModel()
                {
                    ItemCurrent = item,
                    UrlPicture = item.ItemPictures.Count == 0 ? ImageHelper.GetItemImagePath(0) : ImageHelper.GetItemImagePath(item.ItemPictures.OrderBy(x => x.Ordering).FirstOrDefault().PictureID)
                });
            }
            var breadCrumb = GetParents(model.CategoryID).Reverse().ToList();

            model.BreadCrumb = breadCrumb;
            model.Categories = CacheHelper.Categories;
            model.Items = itemsModelList;
            model.ItemsPageList = itemsModelList.ToPagedList(model.PageNumber, model.PageSize);
            model.Grid = new ListingModelGrid(model.ItemsPageList.AsQueryable());
        }
        public async Task<ActionResult> Search(SearchListingModel model)
        {
            await GetSearchResult(model);

            return View("~/Views/Listing/Listings.cshtml", model);
        }
Exemple #4
0
        private async Task GetSearchResult(SearchListingModel model)
        {
            IEnumerable<Listing> items = null;

            // Category
            if (model.CategoryID != 0)
            {
                items = await _listingService.Query(x => x.CategoryID == model.CategoryID)
                    .Include(x => x.ListingPictures)
                    .Include(x => x.Category)
                    .Include(x => x.ListingType)
                    .Include(x => x.AspNetUser)
                    .Include(x => x.ListingReviews)
                    .SelectAsync();

                // Set listing types
                model.ListingTypes = CacheHelper.ListingTypes.Where(x => x.CategoryListingTypes.Any(y => y.CategoryID == model.CategoryID)).ToList();
            }
            else
            {
                model.ListingTypes = CacheHelper.ListingTypes;
            }

            // Set default Listing Type if it's not set or listing type is not set
            if (model.ListingTypes.Count > 0 &&
                (model.ListingTypeID == null || !model.ListingTypes.Any(x => model.ListingTypeID.Contains(x.ID))))
            {
                model.ListingTypeID = new List<int>();
                model.ListingTypeID.Add(model.ListingTypes.FirstOrDefault().ID);
            }

            // Search Text
            if (!string.IsNullOrEmpty(model.SearchText))
            {
                model.SearchText = model.SearchText.ToLower();

                // Search by title, description, location
                if (items != null)
                {
                    items = items.Where(x =>
                        x.Title.ToLower().Contains(model.SearchText) ||
                        x.Description.ToLower().Contains(model.SearchText) ||
                        x.Location.ToLower().Contains(model.SearchText));
                }
                else
                    items = await _listingService.Query(
                        x => x.Title.ToLower().Contains(model.SearchText) ||
                        x.Description.ToLower().Contains(model.SearchText) ||
                        x.Location.ToLower().Contains(model.SearchText))
                        .Include(x => x.ListingPictures)
                        .Include(x => x.Category)
                        .Include(x => x.AspNetUser)
                        .Include(x => x.ListingReviews)
                        .SelectAsync();
            }

            // Latest
            if (items == null)
            {
                items = await _listingService.Query().OrderBy(x => x.OrderByDescending(y => y.Created))
                    .Include(x => x.ListingPictures)
                    .Include(x => x.Category)
                    .Include(x => x.AspNetUser)
                    .Include(x => x.ListingReviews)
                    .SelectAsync();
            }

            // Filter items by Listing Type
            items = items.Where(x => model.ListingTypeID.Contains(x.ListingTypeID));

            // Location
            if (!string.IsNullOrEmpty(model.Location))
            {
                items = items.Where(x => !string.IsNullOrEmpty(x.Location) && x.Location.IndexOf(model.Location, StringComparison.OrdinalIgnoreCase) != -1);
            }

            // Picture
            if (model.PhotoOnly)
                items = items.Where(x => x.ListingPictures.Count > 0);

            /// Price
            if (model.PriceFrom.HasValue)
                items = items.Where(x => x.Price >= model.PriceFrom.Value);

            if (model.PriceTo.HasValue)
                items = items.Where(x => x.Price <= model.PriceTo.Value);

            // Show active and enabled only
            var itemsModelList = new List<ListingItemModel>();
            foreach (var item in items.Where(x => x.Active && x.Enabled).OrderByDescending(x => x.Created))
            {
                itemsModelList.Add(new ListingItemModel()
                {
                    ListingCurrent = item,
                    UrlPicture = item.ListingPictures.Count == 0 ? ImageHelper.GetListingImagePath(0) : ImageHelper.GetListingImagePath(item.ListingPictures.OrderBy(x => x.Ordering).FirstOrDefault().PictureID)
                });
            }
            var breadCrumb = GetParents(model.CategoryID).Reverse().ToList();

            model.BreadCrumb = breadCrumb;
            model.Categories = CacheHelper.Categories;

            model.Listings = itemsModelList;
            model.ListingsPageList = itemsModelList.ToPagedList(model.PageNumber, model.PageSize);
            model.Grid = new ListingModelGrid(model.ListingsPageList.AsQueryable());
        }