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)); }
public ActionResult Details(int?id) { if (id == null) { return(new HttpStatusCodeResult(HttpStatusCode.BadRequest)); } CarModel model = _modelRepository.GetModelById(id); if (model == null) { return(HttpNotFound()); } return(View(model)); }
public ActionResult Create([Bind(Include = "Id,Color,Price,EngineCapacity,Description,ModelId")] Car car, HttpPostedFileBase image) { string userId = User.Identity.GetUserId(); if (userId == null) { return(HttpNotFound()); } var model = _modelCarRepository.GetModelById(car.ModelId); if (model == null) { ModelState.AddModelError("ModelId", "No existing model"); } if (ModelState.IsValid) { car.UserId = userId; if (image != null) { car.ImageMimeType = image.ContentType; car.ImageData = new byte[image.ContentLength]; image.InputStream.Read(car.ImageData, 0, image.ContentLength); } var opStatus = new OperationStatus { Status = true, Message = "Car added" }; try { _carRepository.Add(car); } catch (Exception exp) { opStatus = OperationStatus.CreateFromExeption("Error adding car.", exp); } TempData["OperationStatus"] = opStatus; return(RedirectToAction("Index")); } ViewBag.BrandTree = _brandsTreeRepository.GetBrandsModelsTree(); return(View(car)); }