public async Task <ActionResult> AddVehicleAsync(AddEditVehicleViewModel model, List <IFormFile> attachment) { SetAddEditVehicleValidations(model, out bool valid); model.ModelID = Convert.ToInt32(Request.Form["modelID"].ToString().Substring(Request.Form["modelID"].ToString().IndexOf(',') + 1)); //if (!valid) //{ // AddEditVehicleViewModel viewModel = new AddEditVehicleViewModel // { // Makes = await _vehicle.GenerateMakeList(), // BodyStyles = await _vehicle.GenerateBodyStyleList(), // Transmissions = await _vehicle.GenerateTransmissionTypeList(), // Colors = await _vehicle.GenerateColorList(), // Interiors = await _vehicle.GenerateInteriorList() // }; // return View(viewModel); //} var noDupsList = attachment.GroupBy(x => x).Where(g => g.Count() == 1).Select(y => y.Key).OrderByDescending(x => x.FileName).ToList(); var res = await _vehicle.AddVehicle(model); UploadedFile(noDupsList, res); return(View()); }
public async Task <int> AddVehicle(AddEditVehicleViewModel viewModel) { Vehicle vehicle = new Vehicle(); //vehicle.StaffID = Manager.CurrentUser.StaffID; vehicle.MakeID = viewModel.MakeID; vehicle.ModelID = viewModel.ModelID; vehicle.InteriorID = viewModel.InteriorID; // vehicle.BodyStyle = Manager.BodyStyleRepository.Get(viewModel.BodyStyleID).Style; vehicle.Category = viewModel.Type; vehicle.Description = viewModel.Description; vehicle.SalePrice = viewModel.SalePrice; vehicle.MSRP = viewModel.MSRP; vehicle.Mileage = viewModel.Mileage; vehicle.Year = viewModel.Year; vehicle.VIN = viewModel.Vin; vehicle.Featured = viewModel.FeatureVehicle; vehicle.Transmission = viewModel.transmission; vehicle.BodyStyle = "Sedan"; vehicle.Color = "White"; vehicle.PicturePath = viewModel.PicturePath; // vehicle.Transmission = Manager.TransmissionTypeRepository.Get(viewModel.TransmissionID).TransmissionName; // vehicle.Color = Manager.ColorRepository.Get(viewModel.ColorID).ColorName; _context.Vehicles.Add(vehicle); _context.SaveChanges(); return(vehicle.VehicleID); }
public ActionResult Inventory() { AddEditVehicleViewModel model = new AddEditVehicleViewModel(); model.Vehicles = _vehicle.getAllVehicle(); return(View(model)); }
public async Task <ActionResult> AddVehicleAsync() { AddEditVehicleViewModel viewModel = new AddEditVehicleViewModel { Makes = await _vehicle.GenerateMakeList(), BodyStyles = await _vehicle.GenerateBodyStyleList(), Transmissions = await _vehicle.GenerateTransmissionTypeList(), Colors = await _vehicle.GenerateColorList(), Interiors = await _vehicle.GenerateInteriorList(), Types = await _vehicle.GenerateTypeList() }; return(View(viewModel)); }
public ActionResult EditVehicle(AddEditVehicleViewModel aevm) { string fileName = string.Empty; if (aevm.UploadedFile != null && aevm.UploadedFile.ContentLength > 0) { string path = Path.Combine(Server.MapPath("~/Images"), Path.GetFileName(aevm.UploadedFile.FileName)); fileName = Path.GetFileName(aevm.UploadedFile.FileName); aevm.UploadedFile.SaveAs(path); } var repo = RepositoryFactory.GetRepository(); VehicleQueryModel vcm = new VehicleQueryModel(); if (string.IsNullOrEmpty(aevm.ImageFileName)) { aevm.ImageFileName = string.Empty; } // Translate the Vechicle Query model to the AddEditVehicleViewModel vcm.ConditionType = aevm.Condition.ToString(); vcm.TransmissionType = aevm.Transmission.ToString(); vcm.VIN = aevm.VIN.ToString(); vcm.Year = aevm.Year; vcm.MSRP = aevm.MSRP; vcm.SalePrice = aevm.SalePrice; vcm.ModelId = aevm.ModelId; vcm.MakeId = aevm.MakeId; vcm.ImageFileName = string.IsNullOrEmpty(fileName) ? aevm.ImageFileName : fileName; vcm.Mileage = aevm.Mileage; vcm.InteriorColor = aevm.InteriorColor.ToString(); vcm.ExteriorColor = aevm.ExteriorColor.ToString(); vcm.Description = aevm.Description.ToString(); vcm.BodyStyle = aevm.BodyStyle.ToString(); vcm.VehicleId = aevm.VehicleId; vcm.FeaturedFlag = aevm.FeaturedFlag; //Find id of logged in user var userName = User.Identity.GetUserName(); var userId = context.Users.FirstOrDefault(u => u.UserName == userName).Id; vcm.UserId = aevm.UserId; repo.EditVehicle(vcm); //Save the changes return(RedirectToAction("Vehicles", "Admin")); }
public ActionResult AddVehicle() { var LookupRepo = LookupValueFactory.GetRepository(); var repo = RepositoryFactory.GetRepository(); var model = new AddEditVehicleViewModel(); //set lookup values model.SetBodyStyles(LookupRepo.GetBodyStyles()); model.SetMakes(repo.GetMakes()); model.SetCondition(LookupRepo.GetCondition()); model.SetExteriorColor(LookupRepo.GetExteriorColor()); model.SetInteriorColor(LookupRepo.GetInteriorColor()); model.SetTransmissions(LookupRepo.GetTransmissionTypes()); model.SetYears(LookupRepo.GetYears()); return(View(model)); }
public ActionResult AddVehicle(AddEditVehicleViewModel model) { string fileName = string.Empty; if (model.UploadedFile != null && model.UploadedFile.ContentLength > 0) { string path = Path.Combine(Server.MapPath("~/Images"), Path.GetFileName(model.UploadedFile.FileName)); fileName = Path.GetFileName(model.UploadedFile.FileName); model.UploadedFile.SaveAs(path); } if (string.IsNullOrEmpty(model.ImageFileName)) { model.ImageFileName = string.Empty; } var repo = RepositoryFactory.GetRepository(); VehicleQueryModel vcm = new VehicleQueryModel(); //Translate VehicleAddEditViewModel to VehicleQueryModel vcm.BodyStyle = model.BodyStyle.ToString(); vcm.ConditionType = model.Condition.ToString(); vcm.Description = model.Description.ToString(); vcm.ExteriorColor = model.ExteriorColor.ToString(); vcm.InteriorColor = model.InteriorColor.ToString(); vcm.MakeId = model.MakeId; vcm.ModelId = model.ModelId; vcm.MSRP = model.MSRP; vcm.SalePrice = model.SalePrice; vcm.Mileage = model.Mileage; vcm.SoldFlag = false; vcm.ImageFileName = string.IsNullOrEmpty(fileName) ? model.ImageFileName : fileName; vcm.TransmissionType = model.Transmission; vcm.SoldFlag = false; vcm.FeaturedFlag = false; vcm.FeaturedFlag = model.FeaturedFlag; vcm.VIN = model.VIN.ToString(); vcm.Year = model.Year; vcm.UserId = "913dd4f1-4506-4fad-91ae-4539b7a5bdb5"; ///Get the logged in user VehicleQueryModel newVehicle = repo.AddVehicle(vcm); int id = newVehicle.VehicleId; return(RedirectToAction("EditVehicle", "Admin", new { id })); //Need RouteValues pass id to edit.. }
public ActionResult EditVehicle(int id) { var repo = RepositoryFactory.GetRepository(); var LookupValueRepo = LookupValueFactory.GetRepository(); VehicleQueryModel vcm = repo.GetVehicleDetail(id); AddEditVehicleViewModel aevm = new AddEditVehicleViewModel(); //Set lookup values aevm.SetBodyStyles(LookupValueRepo.GetBodyStyles()); aevm.SetCondition(LookupValueRepo.GetCondition()); aevm.SetExteriorColor(LookupValueRepo.GetExteriorColor()); aevm.SetInteriorColor(LookupValueRepo.GetInteriorColor()); aevm.SetMakes(repo.GetMakes()); aevm.SetTransmissions(LookupValueRepo.GetTransmissionTypes()); aevm.SetYears(LookupValueRepo.GetYears()); //Get the models for the make of this vehicle aevm.SetModels(repo.GetModelsByMakeId(vcm.MakeId)); // Translate the Vechicle Query model to the AddEditVehicleViewModel aevm.Condition = vcm.ConditionType.ToString(); aevm.Transmission = vcm.TransmissionType.ToString(); aevm.VIN = vcm.VIN.ToString(); aevm.Year = vcm.Year; aevm.MSRP = vcm.MSRP; aevm.SalePrice = vcm.SalePrice; aevm.ModelId = vcm.ModelId; aevm.MakeId = vcm.MakeId; aevm.ImageFileName = vcm.ImageFileName.ToString(); aevm.Mileage = vcm.Mileage; aevm.InteriorColor = vcm.InteriorColor.ToString(); aevm.ExteriorColor = vcm.ExteriorColor.ToString(); aevm.Description = vcm.Description.ToString(); aevm.BodyStyle = vcm.BodyStyle.ToString(); aevm.VehicleId = vcm.VehicleId; aevm.FeaturedFlag = vcm.FeaturedFlag; aevm.UserId = vcm.UserId; return(View(aevm)); }
private void SetAddEditVehicleValidations(AddEditVehicleViewModel model, out bool valid) { const int newMileageMax = 1000; const int minimumYear = 2000; valid = true; if (!string.IsNullOrEmpty(model.Type) && model.Mileage >= 0) { if (model.Type.ToLower().Equals("new") && model.Mileage > newMileageMax) { ModelState.AddModelError("", "New vehicles cannot have a mileage over " + newMileageMax + "."); valid = false; } if (model.Type.ToLower().Equals("used") && model.Mileage <= newMileageMax) { ModelState.AddModelError("", "Used vehicles must have a mileage greater than" + newMileageMax + "."); valid = false; } } else { if (string.IsNullOrEmpty(model.Type)) { ModelState.AddModelError("", "Vehicle must have a type (new/used)"); } if (model.Mileage < 0) { ModelState.AddModelError("", "Vehicle must have a mileage of 0 or higher."); } } if (string.IsNullOrEmpty(model.Vin)) { ModelState.AddModelError("", "Vehicle must have a valid VIN #."); valid = false; } if (model.MSRP <= 0) { ModelState.AddModelError("", "Vehicle MSRP must be a positive number greater than 0."); valid = false; } if (model.SalePrice <= 0 || model.SalePrice > model.MSRP) { if (model.SalePrice <= 0) { ModelState.AddModelError("", "Vehicle sale price must be a positive number greater than 0."); } else { ModelState.AddModelError("", "Vehicle sale price cannot be greater than MSRP."); } valid = false; } if (string.IsNullOrEmpty(model.Description)) { ModelState.AddModelError("", "Vehicle must have a description."); valid = false; } int latestYear = (DateTime.Today.Year + 1); if (model.Year < minimumYear || model.Year > latestYear) { ModelState.AddModelError("", "Vehicle year must be between " + minimumYear + " and " + latestYear + "."); valid = false; } //if (model.UploadedFile != null && model.UploadedFile.ContentLength > 0) //{ // if (!string.IsNullOrEmpty(model.UploadedFile.FileName)) // { // string path = Path.Combine(Server.MapPath("~/Content/images"), // Path.GetFileName(model.UploadedFile.FileName)); // model.UploadedFile.SaveAs(path); // model.PicturePath = "/Content/images/" + model.UploadedFile.FileName; // } // else // { // ModelState.AddModelError("", "Uploaded image had an invalid file name."); // valid = false; // } //} //else //{ // if (string.IsNullOrEmpty(model.PicturePath)) // { // ModelState.AddModelError("", "Vehicle must have an image."); // valid = false; // } //} }