예제 #1
0
        public Status<Rentler.Common.KslPropertySearch> SearchUserProperties(string username, Rentler.Common.KslPropertySearch search)
        {
            if (string.IsNullOrWhiteSpace(username))
                return Status.ValidationError<Rentler.Common.KslPropertySearch>(null, "username", "The username is required");

            // if it is null create a new one
            if (search == null)
                search = new Rentler.Common.KslPropertySearch();
            if (search.Page < 1)
                search.Page = 1;
            if (search.ResultsPerPage < 5)
                search.ResultsPerPage = 25;
            if (string.IsNullOrEmpty(search.OrderBy))
                search.OrderBy = "CreateDate";

            // get the user
            using (var context = new RentlerContext())
            {
                var user = (from u in context.Users
                            where !u.IsDeleted && (u.Username == username || u.Email == username)
                            select u).FirstOrDefault();

                if (user == null)
                    return Status.NotFound<Rentler.Common.KslPropertySearch>();

                var props = from b in context.Buildings
                            where b.UserId == user.UserId && !b.IsDeleted
                            select b;

                // keyword search
                if (!string.IsNullOrEmpty(search.Keywords))
                {
                    props = from b in props
                            where b.Address1.Contains(search.Keywords) ||
                            b.City.Contains(search.Keywords)
                            select b;
                }

                // ordering
                switch (search.OrderBy.ToLower())
                {
                    case "islisted":
                        props = from b in props
                                orderby b.IsActive descending
                                select b;
                        break;
                    default:
                        props = from b in props
                                orderby b.CreateDateUtc descending
                                select b;
                        break;
                }

                var final = from b in props
                            select new BuildingPreview()
                            {
                                Bathrooms = b.Bathrooms ?? 0,
                                Bedrooms = b.Bedrooms ?? 0,
                                BuildingId = b.BuildingId,
                                City = b.City,
                                IsFeatured = false,
                                Price = b.Price,
                                PrimaryPhotoExtension = b.PrimaryPhotoExtension,
                                PrimaryPhotoId = b.PrimaryPhotoId,
                                State = b.State,
                                Title = b.Title,
                                IsRemovedByAdmin = b.IsRemovedByAdmin,
                                Address1 = b.Address1,
                                IsActive = b.IsActive
                            };

                var results = final.ToList();
                search.Results = results;

                return Status.OK<Rentler.Common.KslPropertySearch>(search);
            }
        }
예제 #2
0
        public Status <Rentler.Common.KslPropertySearch> SearchUserProperties(string username, Rentler.Common.KslPropertySearch search)
        {
            if (string.IsNullOrWhiteSpace(username))
            {
                return(Status.ValidationError <Rentler.Common.KslPropertySearch>(null, "username", "The username is required"));
            }

            // if it is null create a new one
            if (search == null)
            {
                search = new Rentler.Common.KslPropertySearch();
            }
            if (search.Page < 1)
            {
                search.Page = 1;
            }
            if (search.ResultsPerPage < 5)
            {
                search.ResultsPerPage = 25;
            }
            if (string.IsNullOrEmpty(search.OrderBy))
            {
                search.OrderBy = "CreateDate";
            }

            // get the user
            using (var context = new RentlerContext())
            {
                var user = (from u in context.Users
                            where !u.IsDeleted && (u.Username == username || u.Email == username)
                            select u).FirstOrDefault();

                if (user == null)
                {
                    return(Status.NotFound <Rentler.Common.KslPropertySearch>());
                }

                var props = from b in context.Buildings
                            where b.UserId == user.UserId && !b.IsDeleted
                            select b;

                // keyword search
                if (!string.IsNullOrEmpty(search.Keywords))
                {
                    props = from b in props
                            where b.Address1.Contains(search.Keywords) ||
                            b.City.Contains(search.Keywords)
                            select b;
                }

                // ordering
                switch (search.OrderBy.ToLower())
                {
                case "islisted":
                    props = from b in props
                            orderby b.IsActive descending
                            select b;
                    break;

                default:
                    props = from b in props
                            orderby b.CreateDateUtc descending
                            select b;
                    break;
                }


                var final = from b in props
                            select new BuildingPreview()
                {
                    Bathrooms             = b.Bathrooms ?? 0,
                    Bedrooms              = b.Bedrooms ?? 0,
                    BuildingId            = b.BuildingId,
                    City                  = b.City,
                    IsFeatured            = false,
                    Price                 = b.Price,
                    PrimaryPhotoExtension = b.PrimaryPhotoExtension,
                    PrimaryPhotoId        = b.PrimaryPhotoId,
                    State                 = b.State,
                    Title                 = b.Title,
                    IsRemovedByAdmin      = b.IsRemovedByAdmin,
                    Address1              = b.Address1,
                    IsActive              = b.IsActive
                };

                var results = final.ToList();
                search.Results = results;

                return(Status.OK <Rentler.Common.KslPropertySearch>(search));
            }
        }