Exemplo n.º 1
0
        public async Task <ActionResult> Search(SearchListingModel model)
        {
            await GetSearchResult(model);

            //Si las listas vienen vacias
            if (model.ListingsPageList.Count() == 0 && model.ListingsPageList2.Count() == 0)
            {
                TempData[TempDataKeys.UserMessageAlertState] = "bg-danger";
                TempData[TempDataKeys.UserMessage]           = "[[[La busqueda no arrojo ningún resultado, por favor verifique los campos]]]";
                return(View("~/Views/Listing/Listings.cshtml", model));
            }

            if (!string.IsNullOrEmpty(model.string_fromdate))
            {
                model.FromDate = Convert.ToDateTime(model.string_fromdate);
            }
            else
            {
                model.ToDate = null;
            }

            if (!string.IsNullOrEmpty(model.string_todate))
            {
                model.ToDate = Convert.ToDateTime(model.string_todate);
            }
            else
            {
                model.ToDate = null;
            }
            Session.Add("fechas", model);


            return(View("~/Views/Listing/Listings.cshtml", model));
        }
Exemplo n.º 2
0
        public async Task <ActionResult> Index(string id)
        {
            if (!string.IsNullOrEmpty(id))
            {
                return(RedirectToAction("ContentPage", "Home", new { id = id.ToLowerInvariant() }));
            }

            var model = new SearchListingModel();

            model.ListingTypeID = CacheHelper.ListingTypes.Select(x => x.ID).ToList();
            await GetSearchResult(model);

            return(View(model));
        }
Exemplo n.º 3
0
        public IActionResult Search(SearchFormModel model)
        {
            var viewModel = new SearchListingModel
            {
                SeachText = model.SearchText
            };

            if (model.SearchInCourses)
            {
                viewModel.Courses = this.courses.Find(model.SearchText);
            }

            if (model.SearchInStudents)
            {
                viewModel.Users = this.users.Find(model.SearchText);
            }

            return(View(viewModel));
        }
Exemplo n.º 4
0
        public async Task <ActionResult> Order(Model.Models.Order order)
        {
            var listing = await _listingService.FindAsync(order.ListingID);

            var ordersListing = await _orderService.Query(x => x.ListingID == order.ListingID).SelectAsync();

            if ((order.ToDate.Value - order.FromDate.Value).Days < 2)
            {
                TempData[TempDataKeys.UserMessageAlertState] = "bg-danger";
                TempData[TempDataKeys.UserMessage]           = "[[[The minium quantity for order is two nights]]]";

                return(RedirectToAction("Listing", "Listing", new { id = order.ListingID }));
            }
            if (order.ToDate < order.FromDate)
            {
                TempData[TempDataKeys.UserMessageAlertState] = "bg-danger";
                TempData[TempDataKeys.UserMessage]           = "[[[The date of check out can't be less than the check in date]]]";

                return(RedirectToAction("Listing", "Listing", new { id = order.ListingID }));
            }
            if ((order.Children + order.Adults) > listing.Max_Capacity)
            {
                TempData[TempDataKeys.UserMessageAlertState] = "bg-danger";
                TempData[TempDataKeys.UserMessage]           = string.Format("La maxima capacidad de pasajeros es de  {0}", listing.Max_Capacity);

                return(RedirectToAction("Listing", "Listing", new { id = order.ListingID }));
            }

            if (listing == null)
            {
                return(new HttpNotFoundResult());
            }

            // Redirect if not authenticated
            if (!User.Identity.IsAuthenticated)
            {
                if (Session["fechas"] != null)
                {
                    SearchListingModel fecha = (SearchListingModel)Session["fechas"];
                    fecha.FromDate = order.FromDate;
                    fecha.ToDate   = order.ToDate;

                    fecha.Niños      = order.Children;
                    fecha.Passengers = order.Adults;

                    Session["fechas"] = fecha;
                }
                return(RedirectToAction("Login", "Account", new { ReturnUrl = Url.Action("Listing", "Listing", new { id = order.ListingID }), FromDate = order.FromDate, ToDate = order.ToDate, Adults = order.Adults, Children = order.Children }));
            }
            var userCurrent = User.Identity.User();

            //validar que los dias no esten reservados
            List <DateTime> FechasCocinadas = new List <DateTime>();

            for (DateTime date = order.FromDate.Value; date < order.ToDate.Value; date = date.Date.AddDays(1))
            {
                FechasCocinadas.Add(date);
            }
            foreach (Model.Models.Order ordenesArrendadas in ordersListing.Where(x => x.Status != (int)Enum_OrderStatus.Cancelled))
            {
                for (DateTime date = ordenesArrendadas.FromDate.Value; date < ordenesArrendadas.ToDate.Value; date = date.Date.AddDays(1))
                {
                    if (FechasCocinadas.Contains(date))
                    {
                        TempData[TempDataKeys.UserMessageAlertState] = "bg-danger";
                        TempData[TempDataKeys.UserMessage]           = "[[[You can not book with these selected dates!]]]";
                        return(RedirectToAction("Listing", "Listing", new { id = listing.ID }));
                    }
                }
            }

            // Check if payment method is setup on user or the platform
            var descriptors = _pluginFinder.GetPluginDescriptors <IHookPlugin>(LoadPluginsMode.InstalledOnly, "Payment").Where(x => x.Enabled);

            if (descriptors.Count() == 0)
            {
                TempData[TempDataKeys.UserMessageAlertState] = "bg-danger";
                TempData[TempDataKeys.UserMessage]           = "[[[The provider has not setup the payment option yet, please contact the provider.]]]";

                return(RedirectToAction("Listing", "Listing", new { id = order.ListingID }));
            }

            if (!User.IsInRole("Administrator"))
            {
                if (listing.UserID != userCurrent.Id)
                {
                    //foreach (var descriptor in descriptors)
                    //{
                    //    var controllerType = descriptor.Instance<IHookPlugin>().GetControllerType();
                    //    var controller = ContainerManager.GetConfiguredContainer().Resolve(controllerType) as IPaymentController;

                    //    if (!controller.HasPaymentMethod(listing.UserID))
                    //    {
                    //        TempData[TempDataKeys.UserMessageAlertState] = "bg-danger";
                    //        TempData[TempDataKeys.UserMessage] = string.Format("[[[The provider has not setup the payment option for {0} yet, please contact the provider.]]]", descriptor.FriendlyName);

                    //        return RedirectToAction("Listing", "Listing", new { id = order.ListingID });
                    //    }
                    //}
                    if (order.ID == 0)
                    {
                        order.ObjectState   = Repository.Pattern.Infrastructure.ObjectState.Added;
                        order.Created       = DateTime.Now;
                        order.Modified      = DateTime.Now;
                        order.Status        = (int)Enum_OrderStatus.Created;
                        order.UserProvider  = listing.UserID;
                        order.UserReceiver  = userCurrent.Id;
                        order.ListingTypeID = order.ListingTypeID;
                        order.Currency      = listing.Currency;
                        order.OrderType     = 3;
                        order.Price         = 0;
                        order.Quantity      = 0;
                        var listingPrice = await _listingPriceService.Query(x => x.ListingID == order.ListingID).SelectAsync();

                        var precioEnero   = listingPrice.FirstOrDefault(x => x.FromDate.Month.Equals(01));
                        var precioFebrero = listingPrice.FirstOrDefault(x => x.FromDate.Month.Equals(02));

                        for (DateTime date = order.FromDate.Value; date <= order.ToDate.Value.AddDays(-1); date = date.Date.AddDays(1))
                        {
                            if (date >= precioEnero.FromDate && date <= precioEnero.ToDate)
                            {
                                order.Price    = order.Price + precioEnero.Price;
                                order.Quantity = order.Quantity + 1;
                            }
                            else if (date >= precioFebrero.FromDate && date <= precioFebrero.ToDate)
                            {
                                order.Price    = order.Price + precioFebrero.Price;
                                order.Quantity = order.Quantity + 1;
                            }
                            else
                            {
                                order.Price    = order.Price + listing.Price;
                                order.Quantity = order.Quantity + 1;
                            }
                        }

                        var servicio = order.Price * 0.04;
                        order.Total = order.Price + listing.CleanlinessPrice + servicio;

                        order.Description = HttpContext.ParseAndTranslate(
                            string.Format("{0} #{1} ([[[From]]] {2} [[[To]]] {3})",
                                          listing.Title,
                                          listing.ID,
                                          order.FromDate.Value.ToShortDateString(),
                                          order.ToDate.Value.ToShortDateString()));

                        _orderService.Insert(order);

                        var provider = await _aspNetUserService.FindAsync(order.UserProvider);

                        //await EnviarCorreo(confirmacion, provider.Email);
                    }
                    await _unitOfWorkAsync.SaveChangesAsync();

                    ClearCache();
                    return(RedirectToAction("ConfirmOrder", "Payment", new { id = order.ID }));
                }
                else
                {
                    order.OrderType = 2;
                    if (order.ID == 0)
                    {
                        order.ObjectState   = Repository.Pattern.Infrastructure.ObjectState.Added;
                        order.Created       = DateTime.Now;
                        order.Modified      = DateTime.Now;
                        order.Status        = (int)Enum_OrderStatus.Pending;
                        order.UserProvider  = listing.UserID;
                        order.UserReceiver  = userCurrent.Id;
                        order.ListingTypeID = order.ListingTypeID;
                        order.Currency      = listing.Currency;

                        if (order.ToDate.HasValue && order.FromDate.HasValue)
                        {
                            order.Description = HttpContext.ParseAndTranslate(
                                string.Format("{0} #{1} ([[[From]]] {2} [[[To]]] {3})",
                                              listing.Title,
                                              listing.ID,
                                              order.FromDate.Value.ToShortDateString(),
                                              order.ToDate.Value.ToShortDateString()));

                            order.Quantity = order.ToDate.Value.Subtract(order.FromDate.Value.Date).Days;
                            order.Price    = 0;
                        }
                        else if (order.Quantity.HasValue)
                        {
                            order.Description = string.Format("{0} #{1}", listing.Title, listing.ID);
                            order.Quantity    = order.Quantity.Value - 1;
                            order.Price       = 0;
                        }
                        else
                        {
                            // Default
                            order.Description = string.Format("{0} #{1}", listing.Title, listing.ID);
                            order.Quantity    = 1;
                            order.Price       = 0;
                        }

                        _orderService.Insert(order);
                    }

                    await _unitOfWorkAsync.SaveChangesAsync();

                    ClearCache();
                    TempData[TempDataKeys.UserMessage] = string.Format("[[[Has bloqueado correctamenter tu propiedad entre las fechas {0} y {1}]]]", order.FromDate.Value.ToString("dd-MM-yyyy"), order.ToDate.Value.ToString("dd-MM-yyyy"));
                    return(RedirectToAction("Listing", "Listing", new { id = listing.ID }));
                }
            }
            else
            {
                order.OrderType = 1;
                if (order.ID == 0)
                {
                    order.ObjectState   = Repository.Pattern.Infrastructure.ObjectState.Added;
                    order.Created       = DateTime.Now;
                    order.Modified      = DateTime.Now;
                    order.Status        = (int)Enum_OrderStatus.Pending;
                    order.UserProvider  = listing.UserID;
                    order.UserReceiver  = userCurrent.Id;
                    order.ListingTypeID = order.ListingTypeID;
                    order.Currency      = listing.Currency;

                    if (order.ToDate.HasValue && order.FromDate.HasValue)
                    {
                        order.Description = HttpContext.ParseAndTranslate(
                            string.Format("{0} #{1} ([[[From]]] {2} [[[To]]] {3})",
                                          listing.Title,
                                          listing.ID,
                                          order.FromDate.Value.ToShortDateString(),
                                          order.ToDate.Value.ToShortDateString()));

                        order.Quantity = order.ToDate.Value.Subtract(order.FromDate.Value.Date).Days;
                        order.Price    = 0;
                    }
                    else if (order.Quantity.HasValue)
                    {
                        order.Description = string.Format("{0} #{1}", listing.Title, listing.ID);
                        order.Quantity    = order.Quantity.Value - 1;
                        order.Price       = 0;
                    }
                    else
                    {
                        // Default
                        order.Description = string.Format("{0} #{1}", listing.Title, listing.ID);
                        order.Quantity    = 1;
                        order.Price       = 0;
                    }

                    _orderService.Insert(order);
                }

                await _unitOfWorkAsync.SaveChangesAsync();

                ClearCache();
                TempData[TempDataKeys.UserMessage] = "[[[The maintenance was scheduled successfully!]]]";
                return(RedirectToAction("Listing", "Listing", new { id = listing.ID }));
            }
        }
Exemplo n.º 5
0
        public async Task <ActionResult> Search(SearchListingModel model)
        {
            await GetSearchResult(model);

            return(View("~/Views/Listing/Listings.cshtml", model));
        }
Exemplo n.º 6
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());
        }
Exemplo n.º 7
0
        private async Task GetSearchResult(SearchListingModel model)
        {
            IEnumerable <Listing> items  = null;
            IEnumerable <Listing> items2 = null;

            if (model.Niños == null)
            {
                model.Niños = 0;
            }
            // Search Text
            #region Busqueda Texto
            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.ToLower()) ||
                        x.Description.ToLower().Contains(model.SearchText.ToLower()) ||
                        x.Location.ToLower().Contains(model.SearchText.ToLower()))
                            .Include(x => x.ListingPictures)
                            .Include(x => x.Category)
                            .Include(x => x.AspNetUser)
                            .Include(x => x.Orders)
                            .Include(x => x.ListingReviews)
                            .SelectAsync();
                }
            }
            #endregion

            // Search dates
            #region Busqueda Fecha
            if (!string.IsNullOrEmpty(model.FromDate.ToString()) && !string.IsNullOrEmpty(model.ToDate.ToString()))
            {
                var Todate2   = model.ToDate.Value.AddDays(2);
                var FromDate2 = model.FromDate.Value.AddDays(-2);

                if (items != null)
                {
                    //TODO: Generar las condiciones por fecha
                    items  = items.Where(x => x.Orders.Where(y => y.FromDate <= model.ToDate && y.ToDate >= model.FromDate).Count() == 0);
                    items2 = await _listingService.Query(
                        x => x.Orders.Any(y => y.FromDate >= FromDate2 && y.FromDate <model.FromDate ||
                                                                                      y.ToDate> model.ToDate && y.ToDate <= Todate2))
                             .Include(x => x.ListingPictures)
                             .Include(x => x.Category)
                             .Include(x => x.AspNetUser)
                             .Include(x => x.ListingReviews)
                             .Include(x => x.Orders)
                             .SelectAsync();
                }
                else
                {
                    //Si los campos fecha vienen con datos, genera condicion por FROMDATE y TODATE
                    items = await _listingService.Query(
                        x => x.Orders.Where(y => y.FromDate <= model.ToDate && y.ToDate >= model.FromDate).Count() == 0)
                            .Include(x => x.ListingPictures)
                            .Include(x => x.Category)
                            .Include(x => x.AspNetUser)
                            .Include(x => x.ListingReviews)
                            .Include(x => x.Orders)
                            .SelectAsync();

                    items2 = await _listingService.Query(
                        x => x.Orders.Any(y => y.FromDate >= FromDate2 && y.FromDate <model.FromDate ||
                                                                                      y.ToDate> model.ToDate && y.ToDate <= Todate2 ||
                                          y.FromDate <= Todate2 && y.FromDate > model.ToDate ||
                                          y.ToDate < model.FromDate && y.ToDate >= FromDate2))
                             .Include(x => x.ListingPictures)
                             .Include(x => x.Category)
                             .Include(x => x.AspNetUser)
                             .Include(x => x.ListingReviews)
                             .Include(x => x.Orders)
                             .SelectAsync();
                }
            }
            #endregion
            //Niños
            if (model.Niños > 0)
            {
                items = await _listingService.Query(x => x.Children == true)
                        .Include(x => x.ListingPictures)
                        .Include(x => x.Category)
                        .Include(x => x.AspNetUser)
                        .Include(x => x.ListingReviews)
                        .Include(x => x.Orders).SelectAsync();

                items2 = await _listingService.Query(x => x.Children == true)
                         .Include(x => x.ListingPictures)
                         .Include(x => x.Category)
                         .Include(x => x.AspNetUser)
                         .Include(x => x.ListingReviews)
                         .SelectAsync();
            }

            // Category
            #region Busqueda Condominio
            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.Orders)
                        .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;
            }
            #endregion
            // 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 Passengers
            #region Busqueda Pasajeros
            if (!string.IsNullOrEmpty(model.Passengers.ToString()))
            {
                if (items != null)
                {
                    items = items.Where(x => x.Max_Capacity >= model.Passengers + model.Niños);
                }
                else
                {
                    items = await _listingService.Query(
                        x => x.Max_Capacity >= model.Passengers + model.Niños)
                            .Include(x => x.ListingPictures)
                            .Include(x => x.Category)
                            .Include(x => x.AspNetUser)
                            .Include(x => x.Orders)
                            .Include(x => x.ListingReviews)
                            .SelectAsync();
                }
                if (items2 != null)
                {
                    items2 = items2.Where(x => x.Max_Capacity >= model.Passengers + model.Niños);
                }
            }
            #endregion
            //Search Type of Property
            #region Busqueda por tipo de propiedad
            if (!string.IsNullOrEmpty(model.TypeOfProperty))
            {
                if (items != null)
                {
                    items = items.Where(x => x.TypeOfProperty == model.TypeOfProperty);
                }
                else
                {
                    items = await _listingService.Query(
                        x => x.TypeOfProperty == model.TypeOfProperty)
                            .Include(x => x.ListingPictures)
                            .Include(x => x.Category)
                            .Include(x => x.AspNetUser)
                            .Include(x => x.Orders)
                            .Include(x => x.ListingReviews)
                            .SelectAsync();
                }
            }
            #endregion
            //Search ParkingLot
            #region Busqueda Estacionamientos
            if (!string.IsNullOrEmpty(model.ParkingLot.ToString()))
            {
                if (items != null)
                {
                    items = items.Where(x => x.ParkingLot == model.ParkingLot);
                }
                else
                {
                    items = await _listingService.Query(
                        x => x.ParkingLot == model.ParkingLot)
                            .Include(x => x.ListingPictures)
                            .Include(x => x.Category)
                            .Include(x => x.AspNetUser)
                            .Include(x => x.Orders)
                            .Include(x => x.ListingReviews)
                            .SelectAsync();
                }
            }
            #endregion
            //Search BathRooms
            #region Busqueda Baños
            if (!string.IsNullOrEmpty(model.Bathrooms.ToString()))
            {
                if (items != null)
                {
                    items = items.Where(x => x.Bathrooms == model.Bathrooms);
                }
                else
                {
                    items = await _listingService.Query(
                        x => x.Bathrooms == model.Bathrooms)
                            .Include(x => x.ListingPictures)
                            .Include(x => x.Category)
                            .Include(x => x.AspNetUser)
                            .Include(x => x.Orders)
                            .Include(x => x.ListingReviews)
                            .SelectAsync();
                }
            }
            #endregion
            //Search Rooms
            #region Busqueda Habitaciones
            if (!string.IsNullOrEmpty(model.Rooms.ToString()))
            {
                if (items != null)
                {
                    items = items.Where(x => x.Rooms == model.Rooms);
                }
                else
                {
                    items = await _listingService.Query(
                        x => x.Rooms == model.Rooms)
                            .Include(x => x.ListingPictures)
                            .Include(x => x.Category)
                            .Include(x => x.AspNetUser)
                            .Include(x => x.Orders)
                            .Include(x => x.ListingReviews)
                            .SelectAsync();
                }
            }
            #endregion
            //Search M2
            #region Busqueda M2
            if (!string.IsNullOrEmpty(model.M2From.ToString()))
            {
                if (!string.IsNullOrEmpty(model.M2To.ToString()))
                {
                    if (items != null)
                    {
                        items = items.Where(x => x.M2 >= model.M2From && x.M2 <= model.M2To);
                    }
                    else
                    {
                        items = await _listingService.Query(
                            x => x.M2 >= model.M2From && x.M2 <= model.M2To)
                                .Include(x => x.ListingPictures)
                                .Include(x => x.Category)
                                .Include(x => x.AspNetUser)
                                .Include(x => x.Orders)
                                .Include(x => x.ListingReviews)
                                .SelectAsync();
                    }
                }
                else
                {
                    if (items != null)
                    {
                        items = items.Where(x => x.M2 >= model.M2From);
                    }
                    else
                    {
                        items = await _listingService.Query(
                            x => x.M2 >= model.M2From)
                                .Include(x => x.ListingPictures)
                                .Include(x => x.Category)
                                .Include(x => x.AspNetUser)
                                .Include(x => x.Orders)
                                .Include(x => x.ListingReviews)
                                .SelectAsync();
                    }
                }
            }
            #endregion
            // Latest
            #region BusquedaLatest
            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.Orders)
                        .Include(x => x.ListingReviews)
                        .SelectAsync();
            }
            #endregion
            // Filter items by Listing Type
            #region Busqueda ListingType
            //items = items.Where(x => model.ListingTypeID.Contains(x.ListingTypeID));
            #endregion
            // Price
            #region Busqueda Precio
            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);
            }
            #endregion
            // Property
            #region Busqueda Propiedad
            if (model.Property > 0)
            {
                items = items.Where(x => x.ID == model.Property);
            }
            #endregion

            // Show active and enabled only
            var itemsModelList  = new List <ListingItemModel>();
            var itemsModelList2 = new List <ListingItemModel>();

            if (items2 != null)
            {
                foreach (var varitem in items)
                {
                    //if(items2.Contains(varitem))
                    //{
                    //    items2.ToList().Remove(varitem);
                    //}
                    items2 = items2.Where(x => x.ID != varitem.ID);
                }
            }

            foreach (var item in items.Where(x => x.Active && x.Enabled).OrderBy(x => x.Price))
            {
                itemsModelList.Add(new ListingItemModel()
                {
                    ListingCurrent = item,
                    UrlPicture     = item.ListingPictures.Count == 0 ? ImageHelper.GetListingImagePath(0) : ImageHelper.GetListingImagePath(item.ListingPictures.OrderBy(x => x.Ordering).FirstOrDefault().PictureID)
                });
            }
            if (items2 != null)
            {
                foreach (var item2 in items2.Where(x => x.Active && x.Enabled).OrderBy(x => x.Price))
                {
                    itemsModelList2.Add(new ListingItemModel()
                    {
                        ListingCurrent = item2,
                        UrlPicture     = item2.ListingPictures.Count == 0 ? ImageHelper.GetListingImagePath(0) : ImageHelper.GetListingImagePath(item2.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.Listings2         = itemsModelList2;
            model.ListingsPageList  = itemsModelList.ToPagedList(model.PageNumber, model.PageSize);
            model.ListingsPageList2 = itemsModelList2.ToPagedList(model.PageNumber, model.PageSize);
            model.Grid = new ListingModelGrid(model.ListingsPageList.AsQueryable());
            model.Grid = new ListingModelGrid(model.ListingsPageList2.AsQueryable());
        }
Exemplo n.º 8
0
        private async Task GetSearchResult(SearchListingModel model)
        {
            IEnumerable <Listing>       items      = null;
            List <MetaFieldSearchModel> metaFields = model.CustomFields;

            // 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)
                        .Include(x => x.ListingMetas)
                        .SelectAsync();

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

                //Custom fields which are searchable for selected category
                var customFieldCategoryQuery = await _customFieldCategoryService.Query(x => x.CategoryID == model.CategoryID && x.MetaField.Searchable).Include(x => x.MetaField.ListingMetas).SelectAsync();

                var customFieldCategories = customFieldCategoryQuery.ToList();
                model.CustomFields = customFieldCategoryQuery.Select(x => new MetaFieldSearchModel
                {
                    ID            = x.MetaField.ID,
                    Name          = x.MetaField.Name,
                    Placeholder   = x.MetaField.Placeholder,
                    ControlTypeID = x.MetaField.ControlTypeID,
                    Options       = x.MetaField.Options,
                    Ordering      = x.MetaField.Ordering
                }).OrderBy(ob => ob.Ordering).ToList();
            }
            else
            {
                model.ListingTypes = CacheHelper.ListingTypes;

                // All Custom fields which are searchable
                var metaFieldQuery = await _metaFieldService.Query(x => x.Searchable).SelectAsync();

                model.CustomFields = metaFieldQuery.Select(x => new MetaFieldSearchModel
                {
                    ID            = x.ID,
                    Name          = x.Name,
                    Placeholder   = x.Placeholder,
                    ControlTypeID = x.ControlTypeID,
                    Options       = x.Options,
                    Ordering      = x.Ordering
                }).OrderBy(ob => ob.Ordering).ToList();
            }

            // 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)
                        .Include(x => x.ListingMetas)
                        .SelectAsync();
            }

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

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

            // Custom fields
            if (metaFields != null)
            {
                metaFields.RemoveAll(x => x.Options == null); // ignore any blank default values
                if (metaFields.Count > 0)
                {
                    var metaFieldSelected = metaFields.Select(x => new { x.ID, x.Options }).ToList();
                    //var metaFieldTest = items.Select(i => i.ListingMetas).ToList(); //debug test....
                    foreach (var metaField in metaFields)
                    {
                        items = items.Where(x => x.ListingMetas.Any(y => metaField.ID == y.FieldID && metaField.Options.Contains(y.Value))).ToList();
                        model.CustomFields.FirstOrDefault(x => x.ID == metaField.ID).Selected = metaField.Options;
                    }
                }
            }

            // 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());
        }