Exemple #1
0
        public ActionResult UpdateVehicle(int id)
        {
            var makesRepo             = CarMakesRepositoryFactory.GetRepository();
            var vehicleTypesRepo      = VehicleTypesRepositoryFactory.GetRepository();
            var bodyStylesRepo        = BodyStylesRepositoryFactory.GetRepository();
            var transmissionTypesRepo = TransmissionTypesRepositoryFactory.GetRepository();
            var colorsRepo            = ColorsRepositoryFactory.GetRepository();
            var interiorColorsRepo    = InteriorColorsRepositoryFactory.GetRepository();
            var vehiclesRepo          = VehiclesRepositoryFactory.GetRepository();
            var modelsRepo            = CarModelsRepositoryFactory.GetRepository();

            var viewModel = new UpdateVehicleVM();

            viewModel.SetMakeItems(makesRepo.GetAll());
            viewModel.SetVehicleTypeItems(vehicleTypesRepo.GetAll());
            viewModel.SetBodyStyleItems(bodyStylesRepo.GetAll());
            viewModel.SetTransmissionTypeItems(transmissionTypesRepo.GetAll());
            viewModel.SetColorItems(colorsRepo.GetAll());
            viewModel.SetInteriorColorItems(interiorColorsRepo.GetAll());
            viewModel.SetMakeModelItems(modelsRepo.GetDetailsAll());

            var vehicleToUpdate = vehiclesRepo.GetById(id);
            var modelToUpdate   = modelsRepo.GetById(vehicleToUpdate.ModelId);
            var makeToUpdate    = makesRepo.GetById(modelToUpdate.MakeId);

            viewModel.VehicleId          = vehicleToUpdate.VehicleId;
            viewModel.VIN                = vehicleToUpdate.VIN;
            viewModel.ModelName          = modelToUpdate.Name;
            viewModel.MakeName           = makeToUpdate.Name;
            viewModel.VehicleTypeId      = vehicleToUpdate.VehicleTypeId;
            viewModel.BodyStyleId        = vehicleToUpdate.BodyStyleId;
            viewModel.ModelYear          = vehicleToUpdate.ModelYear;
            viewModel.TransmissionTypeId = vehicleToUpdate.TransmissionTypeId;
            viewModel.ColorId            = vehicleToUpdate.ColorId;
            viewModel.InteriorColorId    = vehicleToUpdate.InteriorColorId;
            viewModel.Mileage            = vehicleToUpdate.Mileage;
            viewModel.MSRP               = vehicleToUpdate.MSRP;
            viewModel.SalePrice          = vehicleToUpdate.SalePrice;
            viewModel.Description        = vehicleToUpdate.Description;
            viewModel.IsFeatured         = vehicleToUpdate.IsFeatured;

            return(View(viewModel));
        }
Exemple #2
0
        public ActionResult UpdateVehicle(UpdateVehicleVM updateVehicleVM)
        {
            if (ModelState.IsValid)
            {
                var originalVehicle = VehiclesRepositoryFactory.GetRepository().GetDetailsById(updateVehicleVM.VehicleId);

                var vehicleToUpdate = new Vehicle();
                var modelToUpdate   = new CarModel();
                var makeToUpdate    = new CarMake();

                var employeeId = int.Parse(User.Identity.GetEmployeeId());

                vehicleToUpdate.EmployeeId = employeeId;

                var repo = VehiclesRepositoryFactory.GetRepository();

                if (updateVehicleVM.File != null && updateVehicleVM.File.ContentLength > 0)
                {
                    var newId = repo.GetAll().Max(v => v.VehicleId) + 1;

                    var fileSplit = Path.GetFileName(updateVehicleVM.File.FileName).Split('.');

                    var fileExtension = fileSplit[fileSplit.Length - 1];

                    var fileName = "inventory-x-" + newId + "." + fileExtension;
                    var path     = Path.Combine(Server.MapPath("~/Images/"), fileName);
                    updateVehicleVM.File.SaveAs(path);

                    vehicleToUpdate.ImageFileName = fileName;
                }

                else
                {
                    vehicleToUpdate.ImageFileName = originalVehicle.ImageFileName;
                }

                vehicleToUpdate.VehicleId          = updateVehicleVM.VehicleId;
                vehicleToUpdate.VIN                = updateVehicleVM.VIN;
                modelToUpdate.Name                 = updateVehicleVM.ModelName;
                makeToUpdate.Name                  = updateVehicleVM.MakeName;
                vehicleToUpdate.VehicleTypeId      = updateVehicleVM.VehicleTypeId;
                vehicleToUpdate.BodyStyleId        = updateVehicleVM.BodyStyleId;
                vehicleToUpdate.ModelYear          = updateVehicleVM.ModelYear;
                vehicleToUpdate.TransmissionTypeId = updateVehicleVM.TransmissionTypeId;
                vehicleToUpdate.ColorId            = updateVehicleVM.ColorId;
                vehicleToUpdate.InteriorColorId    = updateVehicleVM.InteriorColorId;
                vehicleToUpdate.Mileage            = updateVehicleVM.Mileage;
                vehicleToUpdate.MSRP               = updateVehicleVM.MSRP;
                vehicleToUpdate.SalePrice          = updateVehicleVM.SalePrice;
                vehicleToUpdate.Description        = updateVehicleVM.Description;
                vehicleToUpdate.IsFeatured         = updateVehicleVM.IsFeatured;

                repo.Update(vehicleToUpdate, modelToUpdate, makeToUpdate);

                return(RedirectToAction("Vehicles"));
            }

            var makesRepo             = CarMakesRepositoryFactory.GetRepository();
            var vehicleTypesRepo      = VehicleTypesRepositoryFactory.GetRepository();
            var bodyStylesRepo        = BodyStylesRepositoryFactory.GetRepository();
            var transmissionTypesRepo = TransmissionTypesRepositoryFactory.GetRepository();
            var colorsRepo            = ColorsRepositoryFactory.GetRepository();
            var interiorColorsRepo    = InteriorColorsRepositoryFactory.GetRepository();
            var vehiclesRepo          = VehiclesRepositoryFactory.GetRepository();
            var modelsRepo            = CarModelsRepositoryFactory.GetRepository();

            updateVehicleVM.SetMakeItems(makesRepo.GetAll());
            updateVehicleVM.SetVehicleTypeItems(vehicleTypesRepo.GetAll());
            updateVehicleVM.SetBodyStyleItems(bodyStylesRepo.GetAll());
            updateVehicleVM.SetTransmissionTypeItems(transmissionTypesRepo.GetAll());
            updateVehicleVM.SetColorItems(colorsRepo.GetAll());
            updateVehicleVM.SetInteriorColorItems(interiorColorsRepo.GetAll());
            updateVehicleVM.SetMakeModelItems(modelsRepo.GetDetailsAll());

            return(View("UpdateVehicle", updateVehicleVM));
        }