Ejemplo n.º 1
0
        public IHttpActionResult GetLikedPost(int _page = 1, int _limit = 15)
        {
            if (_page < 1)
            {
                return(BadRequest("Page must be at least 1."));
            }

            var accountId = User.Identity.GetUserId();
            var renterId  = _context.Renters.Single(r => r.AccountId == accountId).Id;

            var listLikedPostsInDb = _context.AccommodationRentalPosts
                                     .Include(p => p.Status)
                                     .Include(p => p.AccommodationPictures)
                                     .Include(p => p.Accommodation.Address.Province)
                                     .Include(p => p.Accommodation.PaymentType)
                                     .Include(p => p.Accommodation.RoomAreaRange)
                                     .Where(p => _context.Likes.Any(l =>
                                                                    l.AccommodationRentalPostId == p.Id && l.RenterId == renterId))
                                     .Where(p => p.DateExpired > DateTime.Now && p.Status.Name == RentalPostStatusName.Approved);

            var listSimplePosts = new ListSimplePost()
            {
                MaxPage = (int)Math.Ceiling(1.0 * listLikedPostsInDb.Count() / _limit)
            };

            if (_page > listSimplePosts.MaxPage)
            {
                return(NotFound());
            }

            var listLikedPosts = listLikedPostsInDb
                                 .OrderBy(p => p.Id)
                                 .Skip(_limit * (_page - 1))
                                 .Take(_limit)
                                 .ToList();

            listSimplePosts.SimplePostDtos =
                listLikedPosts.ConvertAll(Mapper.Map <AccommodationRentalPost, SimplePostDto>);

            return(Ok(listSimplePosts));
        }
Ejemplo n.º 2
0
        public IHttpActionResult SearchRentalPosts(int _page                  = 1, int _limit     = 15
                                                   , byte provinceId          = 0, int districtId = 0, int wardId   = 0, string street = "", string publicLocationNearby = ""
                                                   , byte paymentTypeId       = 0, int minPrice   = 0, int maxPrice = 0
                                                   , byte accommodationTypeId = 0, byte roomAreaRangeId   = 0
                                                   , int liveWithOwner        = 0, int haveClosedBathroom = 0, int haveWaterHeater = 0
                                                   , byte kitchenTypeId       = 0, int haveAirConditioner = 0, int haveBalcony     = 0)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            if (_page < 1)
            {
                return(BadRequest("Page must be at least 1."));
            }

            if (_page > Math.Ceiling(1.0 * _context.AccommodationRentalPosts.Count() / _limit))
            {
                return(NotFound());
            }

            var rentalPostsInDb = _context.AccommodationRentalPosts
                                  .Include(p => p.Accommodation.Address.Province)
                                  .Include(p => p.AccommodationPictures)
                                  .Include(p => p.Accommodation.PaymentType)
                                  .Include(p => p.Accommodation.RoomAreaRange);

            // Don't show expired post
            rentalPostsInDb = rentalPostsInDb
                              .Where(p => p.DateExpired > DateTime.Now && p.Status.Name == RentalPostStatusName.Approved);

            // Search by Address
            if (wardId != 0)
            {
                rentalPostsInDb = rentalPostsInDb.Where(p => p.Accommodation.Address.WardId == wardId);
            }
            else if (districtId != 0)
            {
                rentalPostsInDb = rentalPostsInDb.Where(p => p.Accommodation.Address.DistrictId == districtId);
            }
            else if (provinceId != 0)
            {
                rentalPostsInDb = rentalPostsInDb.Where(p => p.Accommodation.Address.ProvinceId == provinceId);
            }
            if (!street.IsNullOrWhiteSpace())
            {
                rentalPostsInDb = rentalPostsInDb.Where(p => p.Accommodation.Address.Street.Contains(street));
            }
            if (!publicLocationNearby.IsNullOrWhiteSpace())
            {
                rentalPostsInDb = rentalPostsInDb.Where(p => p.Accommodation.Address.PublicLocationNearby.Contains(publicLocationNearby));
            }

            // Search by Price
            if (paymentTypeId != 0)
            {
                rentalPostsInDb = rentalPostsInDb.Where(p => p.Accommodation.PaymentTypeId == paymentTypeId);
                if (minPrice != 0)
                {
                    rentalPostsInDb = rentalPostsInDb.Where(p => p.Accommodation.Price >= minPrice);
                }
                if (maxPrice != 0)
                {
                    rentalPostsInDb = rentalPostsInDb.Where(p => p.Accommodation.Price <= maxPrice);
                }
            }

            // Search by Accommodation type
            if (accommodationTypeId != 0)
            {
                rentalPostsInDb =
                    rentalPostsInDb.Where(p => p.Accommodation.AccommodationTypeId == accommodationTypeId);
            }

            // Search by Room area range
            if (roomAreaRangeId != 0)
            {
                rentalPostsInDb = rentalPostsInDb.Where(p => p.Accommodation.RoomAreaRangeId == roomAreaRangeId);
            }

            switch (haveAirConditioner)
            {
            // Search by Accommodation facilities
            case 1:
                rentalPostsInDb = rentalPostsInDb.Where(p => p.Accommodation.HaveAirConditioner);
                break;

            case -1:
                rentalPostsInDb = rentalPostsInDb.Where(p => !p.Accommodation.HaveAirConditioner);
                break;

            default:
                if (haveAirConditioner != 0)
                {
                    return(BadRequest("Invalid input: haveAirConditioner."));
                }
                break;
            }

            switch (haveBalcony)
            {
            case 1:
                rentalPostsInDb = rentalPostsInDb.Where(p => p.Accommodation.HaveBalcony);
                break;

            case -1:
                rentalPostsInDb = rentalPostsInDb.Where(p => !p.Accommodation.HaveBalcony);
                break;

            default:
                if (haveBalcony != 0)
                {
                    return(BadRequest("Invalid input: haveBalcony."));
                }
                break;
            }

            switch (haveClosedBathroom)
            {
            case 1:
                rentalPostsInDb = rentalPostsInDb.Where(p => p.Accommodation.HaveClosedBathroom);
                break;

            case -1:
                rentalPostsInDb = rentalPostsInDb.Where(p => !p.Accommodation.HaveClosedBathroom);
                break;

            default:
                if (haveClosedBathroom != 0)
                {
                    return(BadRequest("Invalid input: haveCloseBathroom."));
                }
                break;
            }

            switch (haveWaterHeater)
            {
            case 1:
                rentalPostsInDb = rentalPostsInDb.Where(p => p.Accommodation.HaveWaterHeater);
                break;

            case -1:
                rentalPostsInDb = rentalPostsInDb.Where(p => !p.Accommodation.HaveWaterHeater);
                break;

            default:
                if (haveWaterHeater != 0)
                {
                    return(BadRequest("Invalid input: haveWaterHeater."));
                }
                break;
            }

            switch (liveWithOwner)
            {
            case 1:
                rentalPostsInDb = rentalPostsInDb.Where(p => p.Accommodation.LiveWithOwner);
                break;

            case -1:
                rentalPostsInDb = rentalPostsInDb.Where(p => !p.Accommodation.LiveWithOwner);
                break;

            default:
                if (liveWithOwner != 0)
                {
                    return(BadRequest("Invalid input: liveWithOwner"));
                }
                break;
            }

            if (kitchenTypeId != 0)
            {
                rentalPostsInDb = rentalPostsInDb.Where(p => p.Accommodation.KitchenTypeId == kitchenTypeId);
            }

            // Page and limit result
            var listSimplePost = new ListSimplePost()
            {
                MaxPage = (int)Math.Ceiling(1.0 * rentalPostsInDb.Count() / _limit)
            };

            if (_page > listSimplePost.MaxPage)
            {
                return(NotFound());
            }

            var rentalPosts = rentalPostsInDb.OrderBy(p => p.Id)
                              .Skip(_limit * (_page - 1))
                              .Take(_limit)
                              .ToList();

            var simplePostDtos =
                rentalPosts.ConvertAll(Mapper.Map <AccommodationRentalPost, SimplePostDto>);

            foreach (var simplePostDto in simplePostDtos)
            {
                var comments = _context.Comments
                               .Where(c => c.IsApproved && c.AccommodationRentalPostId == simplePostDto.Id)
                               .ToList();
                if (comments.Count > 0)
                {
                    simplePostDto.Rate = comments.Average(c => c.Rate * 1.0);
                }
                else
                {
                    simplePostDto.Rate = 0;
                }
            }

            listSimplePost.SimplePostDtos = simplePostDtos;

            return(Ok(listSimplePost));
        }