public ActionResult DeleteCar(int id) { Car model = Factory.Create().GetById(id); Factory.Create().Delete(model.CarID); return(RedirectToAction("Vehicles")); }
public ActionResult Add(VehicleVM vehicle) { int carMileage = int.Parse(vehicle.Mileage); decimal carPrice = (decimal)vehicle.SalePrice; decimal carMSRP = (decimal)vehicle.MSRP; if (carMileage >= 1000 && vehicle.CarType == "New") { vehicle.MakesDropDown = new SelectList(Factory.MakeRepo().Get(), nameof(Make.MakeID), nameof(Make.MakeName)); vehicle.ModelsDropDown = new SelectList(Factory.ModelRepo().Get(), nameof(Model.ModelID), nameof(Model.ModelName)); ModelState.AddModelError("Add", "Car cannot be new if mileage is 1000 or more"); return(View(vehicle)); } if (carPrice > carMSRP) { vehicle.MakesDropDown = new SelectList(Factory.MakeRepo().Get(), nameof(Make.MakeID), nameof(Make.MakeName)); vehicle.ModelsDropDown = new SelectList(Factory.ModelRepo().Get(), nameof(Model.ModelID), nameof(Model.ModelName)); ModelState.AddModelError("Add", "Sale Price CANNOT be greater than MSRP"); return(View(vehicle)); } if (ModelState.IsValid) { Make make = Factory.MakeRepo().GetById(vehicle.MakeID); Model carModel = Factory.ModelRepo().GetByID(vehicle.ModelID); carModel.Make = make; int returnInt = 0; Car car = new Car() { Model = carModel, Make = carModel.Make, ModelID = vehicle.ModelID, CarType = vehicle.CarType, BodyStyle = vehicle.BodyStyle, CarYear = vehicle.CarYear, Trans = vehicle.Trans, Color = vehicle.Color, Interior = vehicle.Interior, Mileage = vehicle.Mileage, Vin = vehicle.Vin, MSRP = vehicle.MSRP, SalePrice = vehicle.SalePrice, CarDescription = vehicle.CarDescription, }; Factory.Create().Create(car); if (vehicle.CarPic != null && vehicle.CarPic.ContentLength > 0) { string type = "." + vehicle.CarPic.ContentType.Substring(6); vehicle.CarType = type; if (type == ".png" || type == ".jpg" || type == ".jpeg") { returnInt = car.CarID; car.PicturePath = "/Images/inventory-" + returnInt + type; string path = Path.Combine(Server.MapPath(car.PicturePath)); if (System.IO.File.Exists(path)) { System.IO.File.Delete(path); } vehicle.CarPic.SaveAs(path); } } return(RedirectToAction("Vehicles")); } vehicle.MakesDropDown = new SelectList(Factory.MakeRepo().Get(), nameof(Make.MakeID), nameof(Make.MakeName)); vehicle.ModelsDropDown = new SelectList(Factory.ModelRepo().Get(), nameof(Model.ModelID), nameof(Model.ModelName)); return(View(vehicle)); }
public ActionResult Edit(VehicleVM vehicle) { int.TryParse(vehicle.Mileage, out int carMileage); decimal carPrice = (decimal)vehicle.SalePrice; decimal carMSRP = (decimal)vehicle.MSRP; if (carMileage >= 1000 && vehicle.CarType == "New") { vehicle.MakesDropDown = new SelectList(Factory.MakeRepo().Get(), nameof(Make.MakeID), nameof(Make.MakeName)); vehicle.ModelsDropDown = new SelectList(Factory.ModelRepo().Get(), nameof(Model.ModelID), nameof(Model.ModelName)); ModelState.AddModelError("Add", "Car cannot be new if mileage is 1000 or more"); return(View(vehicle)); } if (carPrice > carMSRP) { vehicle.MakesDropDown = new SelectList(Factory.MakeRepo().Get(), nameof(Make.MakeID), nameof(Make.MakeName)); vehicle.ModelsDropDown = new SelectList(Factory.ModelRepo().Get(), nameof(Model.ModelID), nameof(Model.ModelName)); ModelState.AddModelError("Add", "Sale Price CANNOT be greater than MSRP"); return(View(vehicle)); } if (ModelState.IsValid) { Model carModel = new Model(); carModel = Factory.ModelRepo().GetByID(vehicle.ModelID); Make make = new Make(); make = Factory.MakeRepo().GetById(vehicle.MakeID); Car car = new Car() { CarID = vehicle.CarID, Make = make, Model = carModel, ModelID = vehicle.ModelID, CarType = vehicle.CarType, BodyStyle = vehicle.BodyStyle, CarYear = vehicle.CarYear, Trans = vehicle.Trans, Color = vehicle.Color, Interior = vehicle.Interior, Mileage = vehicle.Mileage, Vin = vehicle.Vin, MSRP = vehicle.MSRP, SalePrice = vehicle.SalePrice, CarDescription = vehicle.CarDescription, IsFeatured = vehicle.IsFeatured, }; Factory.Create().Update(car); return(RedirectToAction("Vehicles")); } vehicle.MakesDropDown = new SelectList(Factory.MakeRepo().Get(), nameof(Make.MakeID), nameof(Make.MakeName)); vehicle.ModelsDropDown = new SelectList(Factory.ModelRepo().Get(), nameof(Model.ModelID), nameof(Model.ModelName)); return(View(vehicle)); }