public HomePageFilter(HousingExtensions.FilterParams param)
 {
     HousingTypeListIds = param.HouseTypeId.ToList();
     DistrictListIds = param.DistrictId.ToList();
     CityId = param.CityId ?? 0;
     MinCost = param.PriceFrom;
     MaxCost = param.PriceTo;
 }
        public IActionResult Index(string filter,
                                   int page       = 1,
                                   int?houseType  = null,
                                   int?cityId     = null,
                                   int?districtId = null,
                                   int?minCost    = null,
                                   int?maxCost    = null,
                                   int?objectId   = null,
                                   bool?isArchive = null)
        {
            var typesHousings = _context.TypesHousing.ToList();

            if (User.IsInRole(RoleNames.Employee))
            {
                cityId = CurrentUser?.City?.Id;
            }

            var filterData = new HousingExtensions.FilterParams()
            {
                CityId     = cityId,
                PriceTo    = minCost,
                PriceFrom  = maxCost,
                Page       = page,
                IsArchived = isArchive
            };

            if (houseType.HasValue)
            {
                filterData.HouseTypeId = new int[] { houseType.Value };
            }


            var query = _context.Housing.Where(x => HousingExtensions.Filter(filterData)(x));

            int totalPages;
            int totalItems;
            var queryResult = query.GetPage(page, out totalItems, out totalPages);
            var items       = queryResult.Select(x => HousingEditModel.Create(x, typesHousings, User)).ToList();

            ViewBag.TotalItems         = _context.Housing.Count();
            ViewBag.FilteredItemsCount = totalItems;

            var model = new HousingIndexModel
            {
                Items   = items,
                Filters = new HousingIndexFilterModel
                {
                    IsArchived    = isArchive ?? false,
                    HousingTypeId = houseType ?? 0,
                    CityId        = cityId ?? 0,
                    DistrictId    = districtId ?? 0
                },
                TotalPages  = totalPages,
                CurrentPage = page
            };

            return(View(model));
        }
Beispiel #3
0
        public IActionResult Index(int?page, string houseTypeId, int?cityId, int?priceFrom, int?priceTo, string districtId)
        {
            var houseTypeIdArray = string.IsNullOrEmpty(houseTypeId) ? new int[] {} : houseTypeId.Split(',').Select(x => Convert.ToInt32(x)).ToArray();
            var districtIdArray  = string.IsNullOrEmpty(districtId) ? new int[] { } : districtId.Split(',').Select(x => Convert.ToInt32(x)).ToArray();

            var filterParams = new HousingExtensions.FilterParams
            {
                CityId      = cityId,
                Page        = page,
                PriceFrom   = priceFrom,
                PriceTo     = priceTo,
                HouseTypeId = houseTypeIdArray,
                DistrictId  = districtIdArray
            };

            if (IsCustomer)
            {
                var customer = _context.Clients.Include(x => x.TypesHousingToCustomers).Include(x => x.DistrictToClients).FirstOrDefault(x => x.Id == CustomerUser.CustomerId);
                if (customer != null)
                {
                    filterParams.CityId      = customer.CityId;
                    filterParams.HouseTypeId = customer.TypesHousingToCustomers.Select(x => x.TypesHousingId).ToArray();

                    var districtIds = customer.DistrictToClients.Select(x => x.DistrictId).ToArray();
                    filterParams.DistrictId = districtIds;
                }
                else
                {
                    return(new HttpNotFoundObjectResult("Customer not found"));
                }
            }

            IQueryable <Housing> query = _context.Housing
                                         .IncludeAll()
                                         .Where(x => HousingExtensions.Filter(filterParams)(x));

            int totalItems;
            int totalPages;
            var items = query.PagedResult(page ?? 1, 20, x => x.CreatedAt, false, out totalItems, out totalPages).ToList();

            bool isAuth = User.Identity.IsAuthenticated;


            var model = new HomePageViewModel
            {
                Items       = items.Select(x => HousingViewModel.Create(x, isAuth)).ToList(),
                CurrentPage = page ?? 1,
                TotalPages  = totalPages,
                Filter      = new HomePageFilter(filterParams)
            };

            return(View(model));
        }