Esempio n. 1
0
        /// <summary>
        /// Searches the specified search.
        /// </summary>
        /// <param name="search">The search.</param>
        /// <returns>
        /// A paginated list of search results.
        /// </returns>
        public Status<Search> Search(Search search)
        {
            // if it is null create a new one
            if (search == null)
                search = new Search();

            if (search.Page < 1)
                search.Page = 1;
            if (search.ResultsPerPage < 5)
                search.ResultsPerPage = 30;

            PaginatedList<BuildingPreview> results = null;

            // get the zip codes for the given location

            string[] codes = this.zipAdapter.GetZipCodesFromLocation(search.Location);

            var featured = featuredAdapter.GetFeatured(codes);

            search.ResultsPerPage = search.ResultsPerPage - featured.Result.Count;

            // find all the buildings
            using (var context = new RentlerContext())
            {
                var listings = from b in context.Buildings
                               where b.IsActive &&
                               b.IsDeleted == false &&
                               b.IsRemovedByAdmin == false
                               select b;

                // Location is defined
                if (codes.Length > 0)
                {
                    listings = from b in listings
                               where codes.Contains(b.Zip)
                               select b;
                }

                // Property Type is defined
                if (search.PropertyType != PropertyType.Undefined)
                {
                    listings = from b in listings
                               where b.PropertyTypeCode == search.PropertyTypeCode
                               select b;
                }

                // Minimum Price defined
                if (search.MinPrice.HasValue)
                {
                    listings = from b in listings
                               where b.Price >= search.MinPrice
                               select b;
                }

                // Maximum Price defined
                if (search.MaxPrice.HasValue)
                {
                    listings = from b in listings
                               where b.Price <= search.MaxPrice
                               select b;
                }

                // start of advanced

                // Bedrooms is defined
                if (search.Bedrooms.HasValue)
                {
                    listings = from b in listings
                               where b.Bedrooms >= search.Bedrooms
                               select b;
                }

                // Bathrooms is defined
                if (search.Bathrooms.HasValue)
                {
                    listings = from b in listings
                               where b.Bathrooms >= search.Bathrooms
                               select b;
                }

                // Minimum Square Footage is defined
                if (search.MinSquareFootage.HasValue)
                {
                    listings = from b in listings
                               where b.SquareFeet >= search.MinSquareFootage
                               select b;
                }

                // Maximum Square Footage is defined
                if (search.MaxSquareFootage.HasValue)
                {
                    listings = from b in listings
                               where b.SquareFeet <= search.MaxSquareFootage
                               select b;
                }

                // Year Built Minimum is defined
                if (search.YearBuiltMin.HasValue)
                {
                    listings = from b in listings
                               where b.YearBuilt >= search.YearBuiltMin
                               select b;
                }

                // Year Built Maximum is defined
                if (search.YearBuiltMax.HasValue)
                {
                    listings = from b in listings
                               where b.YearBuilt <= search.YearBuiltMax
                               select b;
                }

                // Amenities are defined
                if (search.Amenities != null && search.Amenities.Length > 0)
                {
                    listings = from b in listings
                               let ba = b.BuildingAmenities.Select(x => x.AmenityId)
                               where search.Amenities.All(a => ba.Contains(a))
                               select b;
                }

                // Seller Type is defined
                if (search.SellerType != ContactInfoType.Undefined)
                {
                    listings = from b in listings
                               where b.ContactInfo.ContactInfoTypeCode == search.SellerTypeCode
                               select b;
                }

                // Terms
                if (search.Terms != null)
                {
                    // pet friendly
                    if (search.Terms.Contains("petfriendly"))
                    {
                        listings = from b in listings
                                   where b.ArePetsAllowed == true
                                   select b;
                    }

                    // smoking allowed
                    if (search.Terms.Contains("smokingallowed"))
                    {
                        listings = from b in listings
                                   where b.IsSmokingAllowed == true
                                   select b;
                    }
                }

                // Terms Lease Length
                if (search.LeaseLength != LeaseLength.Undefined)
                {
                    listings = from b in listings
                               where b.LeaseLengthCode == search.LeaseLengthCode
                               select b;
                }

                // photos only
                if (search.PhotosOnly)
                {
                    // if it has a primary photo then it has at least 1 photo
                    listings = from b in listings
                               where b.PrimaryPhotoId.HasValue
                               select b;
                }

                // keywords
                if (!string.IsNullOrWhiteSpace(search.Keywords))
                {
                    List<string> keywords = new List<string>();

                    // add words
                    keywords.AddRange(
                        search.Keywords.Split(
                            new char[0],
                            StringSplitOptions.RemoveEmptyEntries
                        )
                    );

                    // add the whole phrase by default if more
                    // than 1 word
                    if (keywords.Count > 1)
                        keywords.Add(search.Keywords);

                    // replace commas
                    for (int i = 0; i < keywords.Count; ++i)
                        keywords[i] = keywords[i].Replace(",", "");

                    // apply to Title, Description and Custom Amenities
                    listings = from b in listings
                               let ca = b.CustomAmenities.Select(a => a.Name)
                               where keywords.Any(k => b.Title.Contains(k)) ||
                               keywords.Any(k => b.Description.Contains(k)) ||
                               keywords.Any(k => ca.Any(a => a.Contains(k)))
                               select b;
                }

                // end of advanced

                // apply default ordering
                switch (search.OrderBy)
                {
                    case "NewOld":
                        //order by for priority listings, as well as date activated
                        listings = listings.OrderByDescending(l => l.HasPriority)
                                           .ThenByDescending(l => l.DateActivatedUtc);
                        break;
                    case "OldNew":
                        listings = listings.OrderBy(m => m.DateActivatedUtc);
                        break;
                    case "PriceHighLow":
                        listings = listings.OrderByDescending(m => m.Price);
                        break;
                    case "PriceLowHigh":
                        listings = listings.OrderBy(m => m.Price);
                        break;
                    case "DateAvailable":
                        break;
                    default:
                        listings = listings.OrderByDescending(l => l.HasPriority)
                                           .ThenByDescending(l => l.DateActivatedUtc);
                        break;
                }

                // convert to building preview
                var final = from b in listings
                            select new BuildingPreview()
                            {
                                Address1 = b.Address1,
                                Address2 = b.Address2,
                                Bathrooms = b.Bathrooms.Value,
                                Bedrooms = b.Bedrooms.Value,
                                BuildingId = b.BuildingId,
                                RibbonId = b.RibbonId,
                                City = b.City,
                                IsFeatured = false,
                                Price = b.Price,
                                PrimaryPhotoExtension = b.PrimaryPhotoExtension,
                                PrimaryPhotoId = b.PrimaryPhotoId,
                                State = b.State,
                                Title = b.Title,
                                IsActive = b.IsActive,
                                Latitude = b.Latitude,
                                Longitude = b.Longitude,
                                HasPriority = b.HasPriority,
                                DatePrioritized = b.DatePrioritized,
                                Zip = b.Zip
                            };

            #if DEBUG
                Tracer.OutputQuery<BuildingPreview>(final);
            #endif
                // get the results to show
                results = new PaginatedList<BuildingPreview>(
                    final,
                    search.Page,
                    search.ResultsPerPage
                );

                //grab featured items if we have any results
                if (results.Count > 0)
                    results.InsertRange(0, featured.Result);
            }

            // increment search views for each listing
            IncrementSearchViews(results.Select(m => m.BuildingId).ToArray());

            search.Results = results;

            search.HasNextPage = results.HasNextPage;
            search.HasPreviousPage = results.HasPreviousPage;

            return Status.OK<Search>(search);
        }
Esempio n. 2
0
        /// <summary>
        /// Searches the specified search.
        /// </summary>
        /// <param name="search">The search.</param>
        /// <returns>
        /// A paginated list of search results.
        /// </returns>
        public Status <Search> Search(Search search)
        {
            // if it is null create a new one
            if (search == null)
            {
                search = new Search();
            }

            if (search.Page < 1)
            {
                search.Page = 1;
            }
            if (search.ResultsPerPage < 5)
            {
                search.ResultsPerPage = 30;
            }

            PaginatedList <BuildingPreview> results = null;

            // get the zip codes for the given location

            string[] codes = this.zipAdapter.GetZipCodesFromLocation(search.Location);

            var featured = featuredAdapter.GetFeatured(codes);

            search.ResultsPerPage = search.ResultsPerPage - featured.Result.Count;

            // find all the buildings
            using (var context = new RentlerContext())
            {
                var listings = from b in context.Buildings
                               where b.IsActive &&
                               b.IsDeleted == false &&
                               b.IsRemovedByAdmin == false
                               select b;

                // Location is defined
                if (codes.Length > 0)
                {
                    listings = from b in listings
                               where codes.Contains(b.Zip)
                               select b;
                }

                // Property Type is defined
                if (search.PropertyType != PropertyType.Undefined)
                {
                    listings = from b in listings
                               where b.PropertyTypeCode == search.PropertyTypeCode
                               select b;
                }

                // Minimum Price defined
                if (search.MinPrice.HasValue)
                {
                    listings = from b in listings
                               where b.Price >= search.MinPrice
                               select b;
                }

                // Maximum Price defined
                if (search.MaxPrice.HasValue)
                {
                    listings = from b in listings
                               where b.Price <= search.MaxPrice
                               select b;
                }

                // start of advanced

                // Bedrooms is defined
                if (search.Bedrooms.HasValue)
                {
                    listings = from b in listings
                               where b.Bedrooms >= search.Bedrooms
                               select b;
                }

                // Bathrooms is defined
                if (search.Bathrooms.HasValue)
                {
                    listings = from b in listings
                               where b.Bathrooms >= search.Bathrooms
                               select b;
                }

                // Minimum Square Footage is defined
                if (search.MinSquareFootage.HasValue)
                {
                    listings = from b in listings
                               where b.SquareFeet >= search.MinSquareFootage
                               select b;
                }

                // Maximum Square Footage is defined
                if (search.MaxSquareFootage.HasValue)
                {
                    listings = from b in listings
                               where b.SquareFeet <= search.MaxSquareFootage
                               select b;
                }

                // Year Built Minimum is defined
                if (search.YearBuiltMin.HasValue)
                {
                    listings = from b in listings
                               where b.YearBuilt >= search.YearBuiltMin
                               select b;
                }

                // Year Built Maximum is defined
                if (search.YearBuiltMax.HasValue)
                {
                    listings = from b in listings
                               where b.YearBuilt <= search.YearBuiltMax
                               select b;
                }

                // Amenities are defined
                if (search.Amenities != null && search.Amenities.Length > 0)
                {
                    listings                   = from b in listings
                                        let ba = b.BuildingAmenities.Select(x => x.AmenityId)
                                                 where search.Amenities.All(a => ba.Contains(a))
                                                 select b;
                }

                // Seller Type is defined
                if (search.SellerType != ContactInfoType.Undefined)
                {
                    listings = from b in listings
                               where b.ContactInfo.ContactInfoTypeCode == search.SellerTypeCode
                               select b;
                }

                // Terms
                if (search.Terms != null)
                {
                    // pet friendly
                    if (search.Terms.Contains("petfriendly"))
                    {
                        listings = from b in listings
                                   where b.ArePetsAllowed == true
                                   select b;
                    }

                    // smoking allowed
                    if (search.Terms.Contains("smokingallowed"))
                    {
                        listings = from b in listings
                                   where b.IsSmokingAllowed == true
                                   select b;
                    }
                }

                // Terms Lease Length
                if (search.LeaseLength != LeaseLength.Undefined)
                {
                    listings = from b in listings
                               where b.LeaseLengthCode == search.LeaseLengthCode
                               select b;
                }

                // photos only
                if (search.PhotosOnly)
                {
                    // if it has a primary photo then it has at least 1 photo
                    listings = from b in listings
                               where b.PrimaryPhotoId.HasValue
                               select b;
                }

                // keywords
                if (!string.IsNullOrWhiteSpace(search.Keywords))
                {
                    List <string> keywords = new List <string>();

                    // add words
                    keywords.AddRange(
                        search.Keywords.Split(
                            new char[0],
                            StringSplitOptions.RemoveEmptyEntries
                            )
                        );

                    // add the whole phrase by default if more
                    // than 1 word
                    if (keywords.Count > 1)
                    {
                        keywords.Add(search.Keywords);
                    }

                    // replace commas
                    for (int i = 0; i < keywords.Count; ++i)
                    {
                        keywords[i] = keywords[i].Replace(",", "");
                    }

                    // apply to Title, Description and Custom Amenities
                    listings                   = from b in listings
                                        let ca = b.CustomAmenities.Select(a => a.Name)
                                                 where keywords.Any(k => b.Title.Contains(k)) ||
                                                 keywords.Any(k => b.Description.Contains(k)) ||
                                                 keywords.Any(k => ca.Any(a => a.Contains(k)))
                                                 select b;
                }

                // end of advanced

                // apply default ordering
                switch (search.OrderBy)
                {
                case "NewOld":
                    //order by for priority listings, as well as date activated
                    listings = listings.OrderByDescending(l => l.HasPriority)
                               .ThenByDescending(l => l.DateActivatedUtc);
                    break;

                case "OldNew":
                    listings = listings.OrderBy(m => m.DateActivatedUtc);
                    break;

                case "PriceHighLow":
                    listings = listings.OrderByDescending(m => m.Price);
                    break;

                case "PriceLowHigh":
                    listings = listings.OrderBy(m => m.Price);
                    break;

                case "DateAvailable":
                    break;

                default:
                    listings = listings.OrderByDescending(l => l.HasPriority)
                               .ThenByDescending(l => l.DateActivatedUtc);
                    break;
                }

                // convert to building preview
                var final = from b in listings
                            select new BuildingPreview()
                {
                    Address1              = b.Address1,
                    Address2              = b.Address2,
                    Bathrooms             = b.Bathrooms.Value,
                    Bedrooms              = b.Bedrooms.Value,
                    BuildingId            = b.BuildingId,
                    RibbonId              = b.RibbonId,
                    City                  = b.City,
                    IsFeatured            = false,
                    Price                 = b.Price,
                    PrimaryPhotoExtension = b.PrimaryPhotoExtension,
                    PrimaryPhotoId        = b.PrimaryPhotoId,
                    State                 = b.State,
                    Title                 = b.Title,
                    IsActive              = b.IsActive,
                    Latitude              = b.Latitude,
                    Longitude             = b.Longitude,
                    HasPriority           = b.HasPriority,
                    DatePrioritized       = b.DatePrioritized,
                    Zip = b.Zip
                };

#if DEBUG
                Tracer.OutputQuery <BuildingPreview>(final);
#endif
                // get the results to show
                results = new PaginatedList <BuildingPreview>(
                    final,
                    search.Page,
                    search.ResultsPerPage
                    );

                //grab featured items if we have any results
                if (results.Count > 0)
                {
                    results.InsertRange(0, featured.Result);
                }
            }

            // increment search views for each listing
            IncrementSearchViews(results.Select(m => m.BuildingId).ToArray());

            search.Results = results;

            search.HasNextPage     = results.HasNextPage;
            search.HasPreviousPage = results.HasPreviousPage;

            return(Status.OK <Search>(search));
        }