Пример #1
0
        public ActionResult AddVehicle()
        {
            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 viewModel = new AddVehicleVM();

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

            return(View(viewModel));
        }
Пример #2
0
        public ActionResult AddVehicle(AddVehicleVM addVehicleVM)
        {
            var duplicateVehicle = from v in VehiclesRepositoryFactory.GetRepository().GetAll()
                                   where v.VIN == addVehicleVM.VIN
                                   select v;

            if (duplicateVehicle.Count() != 0)
            {
                ModelState.AddModelError("VIN", "VIN already exists in database. Duplicate vehicles are not allowed.");
            }

            if (ModelState.IsValid)
            {
                var vehicleToAdd = new Vehicle();
                var modelToAdd   = new CarModel();
                var makeToAdd    = new CarMake();

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

                vehicleToAdd.EmployeeId = employeeId;

                var repo = VehiclesRepositoryFactory.GetRepository();

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

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

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

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

                    vehicleToAdd.ImageFileName = fileName;
                }

                modelToAdd.Name                 = addVehicleVM.ModelName;
                makeToAdd.Name                  = addVehicleVM.MakeName;
                vehicleToAdd.VIN                = addVehicleVM.VIN;
                vehicleToAdd.BodyStyleId        = addVehicleVM.BodyStyleId;
                vehicleToAdd.ModelYear          = addVehicleVM.ModelYear;
                vehicleToAdd.TransmissionTypeId = addVehicleVM.TransmissionTypeId;
                vehicleToAdd.ColorId            = addVehicleVM.ColorId;
                vehicleToAdd.InteriorColorId    = addVehicleVM.InteriorColorId;
                vehicleToAdd.Mileage            = addVehicleVM.Mileage;
                vehicleToAdd.MSRP               = addVehicleVM.MSRP;
                vehicleToAdd.SalePrice          = addVehicleVM.SalePrice;
                vehicleToAdd.Description        = addVehicleVM.Description;
                vehicleToAdd.VehicleTypeId      = addVehicleVM.VehicleTypeId;


                repo.Insert(vehicleToAdd, modelToAdd, makeToAdd);

                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 modelsRepo            = CarModelsRepositoryFactory.GetRepository();

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

            return(View("AddVehicle", addVehicleVM));
        }