Esempio n. 1
0
        public ActionResult AddVehicle(AddEditListingVM model)
        {
            var repo = VehicleListingsFactory.GetRepository();

            if (ModelState.IsValid)
            {
                try
                {
                    var savepath = Server.MapPath("~/Images");

                    if (model.ImageUpload != null && model.ImageUpload.ContentLength > 0)
                    {
                        string fileName = Path.GetFileName(model.ImageUpload.FileName);
                        var    filePath = Path.Combine(savepath, fileName);

                        repo.Insert(model.Listing);

                        var extension = Path.GetExtension(filePath);
                        var vehicle   = repo.GetById(model.Listing.VehicleListingId);
                        vehicle.ImageFileName = "inventory-" + vehicle.VehicleListingId + extension;

                        var newFilePath = Path.Combine(savepath, vehicle.ImageFileName);

                        if (new FileInfo(newFilePath).Exists)
                        {
                            new FileInfo(newFilePath).Delete();
                        }

                        model.ImageUpload.SaveAs(newFilePath);

                        repo.Update(vehicle);
                    }

                    return(RedirectToAction("EditVehicle", new { id = model.Listing.VehicleListingId }));
                }
                catch (Exception ex)
                {
                    throw ex;
                }
            }
            else
            {
                var makesRepo = VehicleMakesFactory.GetRepository();
                var typesRepo = VehicleTypesFactory.GetRepository();
                var bodyRepo  = BodyStylesFactory.GetRepository();
                var transRepo = TransmissionTypesFactory.GetRepository();
                var colorRepo = ColorsFactory.GetRepository();

                model.VehicleMakes      = makesRepo.GetVehicleMakes();
                model.VehicleTypes      = typesRepo.GetAll();
                model.BodyStyles        = bodyRepo.GetAll();
                model.TransmissionTypes = transRepo.GetAll();
                model.Colors            = colorRepo.GetAll();

                return(View("AddVehicle", model));
            }
        }
Esempio n. 2
0
        public ActionResult EditVehicle(AddEditListingVM model)
        {
            var repo = VehicleListingsFactory.GetRepository();

            if (ModelState.IsValid)
            {
                try
                {
                    if (model.ImageUpload != null && model.ImageUpload.ContentLength > 0)
                    {
                        var savepath = Server.MapPath("~/Images");

                        string fileName      = Path.GetFileName(model.ImageUpload.FileName);
                        string fileExtension = Path.GetExtension(model.ImageUpload.FileName);

                        fileName = "inventory-" + model.Listing.VehicleListingId + fileExtension;

                        var filePath = Path.Combine(savepath, fileName);

                        model.ImageUpload.SaveAs(filePath);

                        model.Listing.ImageFileName = fileName;
                    }

                    repo.Update(model.Listing);

                    return(RedirectToAction("Vehicles"));
                }
                catch (Exception ex)
                {
                    throw ex;
                }
            }
            else
            {
                var makesRepo  = VehicleMakesFactory.GetRepository();
                var modelsRepo = VehicleModelsFactory.GetRepository();
                var typesRepo  = VehicleTypesFactory.GetRepository();
                var bodyRepo   = BodyStylesFactory.GetRepository();
                var transRepo  = TransmissionTypesFactory.GetRepository();
                var colorRepo  = ColorsFactory.GetRepository();

                model.VehicleMakes      = makesRepo.GetVehicleMakes();
                model.VehicleModels     = modelsRepo.GetVehicleModels();
                model.VehicleTypes      = typesRepo.GetAll();
                model.BodyStyles        = bodyRepo.GetAll();
                model.TransmissionTypes = transRepo.GetAll();
                model.Colors            = colorRepo.GetAll();


                return(View("EditVehicle", model));
            }
        }
Esempio n. 3
0
        public ActionResult AddVehicle()
        {
            var makesRepo = VehicleMakesFactory.GetRepository();
            var typesRepo = VehicleTypesFactory.GetRepository();
            var bodyRepo  = BodyStylesFactory.GetRepository();
            var transRepo = TransmissionTypesFactory.GetRepository();
            var colorRepo = ColorsFactory.GetRepository();

            AddEditListingVM model = new AddEditListingVM()
            {
                Listing           = new VehicleListing(),
                VehicleMakes      = makesRepo.GetVehicleMakes(),
                VehicleTypes      = typesRepo.GetAll(),
                BodyStyles        = bodyRepo.GetAll(),
                TransmissionTypes = transRepo.GetAll(),
                Colors            = colorRepo.GetAll()
            };

            return(View(model));
        }