Esempio n. 1
0
        public ActionResult GetCars(FilterParams filter, int page = 1)
        {
            CheckCookies();
            var cars = _carRepository.GetCarsFromFilter(filter);
            int totalElements = cars.Count();
            if (page < 1) page = 1;
            var selectedCars = _carRepository.GetCarsFromPageRange(cars, page, _elementsPerPage);

            if (filter.Date.HasValue) // add price to car
            {
                var carPrices = _changePriceRepository.GetListPriceCars(selectedCars, filter.MinPrice, filter.MaxPrice, filter.Date.Value);
                selectedCars = selectedCars.Join(carPrices,
                    car => car.Id, history => history.CarId,
                    (car, history) =>
                    {
                        car.Price = history.Price;
                        return car;
                    }).ToList();
            }

            if (filter.ModelId.HasValue)
            {
                var filteredModel = _modelRepository.GetModelById(filter.ModelId);
                if (filter.BrandId.HasValue)
                {
                    if (filteredModel.BrandId != filter.BrandId)
                        filter.BrandId = filteredModel.BrandId;

                    ViewBag.BrandName = _brandRepository.GetBrandById(filter.BrandId).Name;
                }
                ViewBag.ModelName = _modelRepository.GetModelById(filter.ModelId).Name;
            }
            else
                if (filter.BrandId.HasValue)
                    ViewBag.BrandName = _brandRepository.GetBrandById(filter.BrandId).Name;

            CarsListViewModel model = new CarsListViewModel()
            {
                Cars = selectedCars,
                PagingInfo = new PagingInfo()
                {
                    CurrentPage = page,
                    ItemsPerPage = _elementsPerPage,
                    TotalItems = totalElements
                },
                FilterParams = filter
            };
            return PartialView("Cars", model);
        }
Esempio n. 2
0
        public ActionResult GetDistinctColors(FilterParams filter)
        {
            if (filter.Color.Length > 20)
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);

            var cars = _carRepository.GetCarsFromFilter(filter);
            var distinctColors = _carRepository.DistinctCarsColor(cars, filter.Color);
            return Json(distinctColors, JsonRequestBehavior.AllowGet);
        }
Esempio n. 3
0
        public ActionResult GetDistinctEngineCapacity(FilterParams filter)
        {
            if (filter.Engine == null || filter.Engine.Length > 5)
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);

            var cars = _carRepository.GetCarsFromFilter(filter);
            var distinctEngines = _carRepository.DistinctCarsEngineCapacity(cars, filter.Engine);
            return Json(distinctEngines, JsonRequestBehavior.AllowGet);
        }
Esempio n. 4
0
        public static MvcHtmlString ResetFilter(this HtmlHelper html, FilterParams filter, string brandName,
            string modelName)
        {
            TagBuilder tagUl = new TagBuilder("ul");

            StringBuilder builder = new StringBuilder();

            tagUl.AddCssClass("filter-active");
            TagBuilder tagLi;
            TagBuilder divContent = new TagBuilder("div");
            divContent.AddCssClass("selected-filter-link");

            TagBuilder crossBtn = new TagBuilder("span");
            crossBtn.AddCssClass("remove-filter");
            crossBtn.InnerHtml = "x";

            if (filter.BrandId.HasValue)
            {
                tagLi = new TagBuilder("li");
                tagLi.AddCssClass("selected-filter");

                divContent.InnerHtml = brandName + crossBtn;
                tagLi.InnerHtml = divContent.ToString();
                tagLi.MergeAttribute("id", "remove-brand-filter");
                builder.Append(tagLi);
            }
            if (filter.ModelId.HasValue)
            {
                tagLi = new TagBuilder("li");
                tagLi.AddCssClass("selected-filter");

                divContent.InnerHtml = modelName + crossBtn;
                tagLi.InnerHtml = divContent.ToString();
                tagLi.MergeAttribute("id", "remove-model-filter");
                builder.Append(tagLi);
            }
            if (filter.Color != null)
            {
                tagLi = new TagBuilder("li");
                tagLi.AddCssClass("selected-filter");

                divContent.InnerHtml = filter.Color + crossBtn;
                tagLi.InnerHtml = divContent.ToString();
                tagLi.MergeAttribute("id", "remove-color-filter");
                builder.Append(tagLi);
            }
            if (filter.Engine != null)
            {
                tagLi = new TagBuilder("li");
                tagLi.AddCssClass("selected-filter");

                divContent.InnerHtml = filter.Engine + "L" + crossBtn;
                tagLi.InnerHtml = divContent.ToString();
                tagLi.MergeAttribute("id", "remove-engine-filter");
                builder.Append(tagLi);
            }
            if (filter.MinPrice.HasValue || filter.MaxPrice.HasValue || filter.Date.HasValue)
            {
                tagLi = new TagBuilder("li");
                tagLi.AddCssClass("selected-filter");

                string priceFilter = "";
                if (filter.MinPrice.HasValue)
                {
                    priceFilter += "Price from " + filter.MinPrice.Value.ToString("c0");

                }
                if (filter.MaxPrice.HasValue)
                {

                    if (filter.MinPrice.HasValue)
                    {
                        priceFilter += " to " + filter.MaxPrice.Value.ToString("c0");

                    }
                    else
                    {
                        priceFilter = "Price to " + filter.MaxPrice.Value.ToString("c0");
                    }
                }
                if (filter.Date.HasValue)
                {
                    if (filter.MinPrice.HasValue || filter.MaxPrice.HasValue)
                    {
                        priceFilter += " for " + filter.Date.Value.ToString("d");
                    }
                    else
                    {
                        priceFilter = "Price for " + filter.Date.Value.ToString("d");
                    }

                }

                divContent.InnerHtml = priceFilter + crossBtn;
                tagLi.InnerHtml = divContent.ToString();
                tagLi.MergeAttribute("id", "remove-price-filter");
                builder.Append(tagLi);
            }

            if (filter.BrandId.HasValue || filter.ModelId.HasValue || filter.Color != null || filter.Engine != null ||
                filter.MinPrice.HasValue || filter.MaxPrice.HasValue || filter.Date.HasValue)
            {
                tagLi = new TagBuilder("li");
                tagLi.AddCssClass("selected-filter");

                divContent.InnerHtml = "Clear All" + crossBtn;
                tagLi.InnerHtml = divContent.ToString();
                tagLi.GenerateId("remove-all-filters");
                builder.Append(tagLi);
            }

            tagUl.InnerHtml = builder.ToString();
            return new MvcHtmlString(tagUl.ToString());
        }