public ActionResult AddVehicle()
        {
            var repo  = CarMasterRepoFactory.Create();
            var model = new VehicleAdminViewModel();

            model.SetMakes(repo.GetAllMakes());
            model.SetModels(repo.GetAllModels());
            model.SetTypes(repo.GetAllVehicleTypes());
            model.SetBodyStyles(repo.GetAllBodyStyles());
            model.SetTransmissions(repo.GetAllTransmissions());
            model.SetInteriorColors(repo.GetAllInteriorColors());
            model.SetExteriorColors(repo.GetAllExteriorColors());
            return(View(model));
        }
        public ActionResult EditVehicle(VehicleAdminViewModel m)
        {
            //var repo = CarMasterRepoFactory.Create();
            //int nextYear = DateTime.Now.Year + 1;

            //if (model.Vehicle.Year < 2000 || model.Vehicle.Year > nextYear)
            //{
            //    ModelState.AddModelError("Vehicle.Year", "The vehicle year must be between the year 2000 and this year plus 1.");
            //}
            //if (model.Vehicle.VehicleType.VehicleTypeId.Equals(1) && model.Vehicle.Mileage > 1000 || model.Vehicle.Mileage < 0)
            //{
            //    ModelState.AddModelError("Vehicle.VehicleType.VehicleTypeId", "New vehicle mileage must be between 0 and 1000.");
            //}
            //if (model.Vehicle.VehicleType.VehicleTypeId.Equals(2) && model.Vehicle.Mileage < 1000)
            //{
            //    ModelState.AddModelError("Vehicle.VehicleType.VehicleTypeId", "Used vehicle mileage must be greater than 1000.");
            //}
            //if (string.IsNullOrWhiteSpace(model.Vehicle.Vin))
            //{
            //    ModelState.AddModelError("Vehicle.Vin", "VIN# is required.");
            //}
            //if (model.Vehicle.MSRP < 1)
            //{
            //    ModelState.AddModelError("Vehicle.MSRP", "MSRP must be a positive number.");
            //}
            //if (model.Vehicle.SalePrice < 1)
            //{
            //    ModelState.AddModelError("Vehicle.SalePrice", "Sale Price must be a positive number.");
            //}
            //if (model.Vehicle.SalePrice > model.Vehicle.MSRP)
            //{
            //    ModelState.AddModelError("", "Sale Price must not exceed MSRP.");
            //}
            //if (string.IsNullOrWhiteSpace(model.Vehicle.VehicleDescription))
            //{
            //    ModelState.AddModelError("Vehicle.VehicleDescription", "Description is required.");
            //}
            //if (model.ImageUpload != null && model.ImageUpload.ContentLength > 0)
            //{
            //    var ext = new string[] { ".png", ".jpg", ".jpeg", ".gif" };

            //    if (!ext.Contains(extension))
            //    {
            //        ModelState.AddModelError("ImageUpload", "Image file must be a .png, .jpeg, .jpg, of .gif.");
            //    }
            //}
            //else
            //{
            //    ModelState.AddModelError("ImageUpload", "Image is required.");
            //}

            if (ModelState.IsValid)
            {
                var repo = CarMasterRepoFactory.Create();
                try
                {
                    var old = repo.GetSingleVehicle(m.Vehicle.VehicleId);

                    if (m.ImageUpload != null && m.ImageUpload.ContentLength > 0)
                    {
                        var savePath = Server.MapPath("~/Images");

                        string extension = Path.GetExtension(m.ImageUpload.FileName);
                        string fileName  = Path.GetFileNameWithoutExtension(m.ImageUpload.FileName);
                        var    filePath  = Path.Combine(savePath, fileName + extension);

                        int counter = 1;
                        while (System.IO.File.Exists(filePath))
                        {
                            filePath = Path.Combine(savePath, fileName + counter.ToString() + extension);
                            counter++;
                        }

                        m.ImageUpload.SaveAs(filePath);
                        m.Vehicle.ImageFileName = Path.GetFileName(filePath);

                        //Delete old file
                        var oldPath = Path.Combine(savePath, old.ImageFileName);
                        if (System.IO.File.Exists(oldPath))
                        {
                            System.IO.File.Delete(oldPath);
                        }
                    }
                    else
                    {
                        //if replace image, delete old one and replace
                        m.Vehicle.ImageFileName = old.ImageFileName;
                    }
                    m.Vehicle.ModelId            = m.Vehicle.Model.ModelId;
                    m.Vehicle.VehicleDescription = m.Vehicle.VehicleType.TypeDescription;
                    repo.UpdateVehicle(m.Vehicle);
                    return(RedirectToAction("EditVehicle", new { id = m.Vehicle.VehicleId }));
                }

                catch (Exception ex)
                {
                    throw ex;
                }
            }
            else
            {
                var repo     = CarMasterRepoFactory.Create();
                var newModel = new VehicleAdminViewModel();
                newModel.SetMakes(repo.GetAllMakes());
                newModel.SetModels(repo.GetAllModels());
                newModel.SetBodyStyles(repo.GetAllBodyStyles());
                newModel.SetTransmissions(repo.GetAllTransmissions());
                newModel.SetInteriorColors(repo.GetAllInteriorColors());
                newModel.SetExteriorColors(repo.GetAllExteriorColors());
                return(View(newModel));
            }
        }
        public ActionResult AddVehicle(VehicleAdminViewModel model)
        {
            var repo     = CarMasterRepoFactory.Create();
            int nextYear = DateTime.Now.Year + 1;

            if (model.Vehicle.Year < 2000 || model.Vehicle.Year > nextYear)
            {
                ModelState.AddModelError("Vehicle.Year", "The vehicle year must be between the year 2000 and this year plus 1.");
            }
            if (model.Vehicle.VehicleType.VehicleTypeId.Equals(1) && model.Vehicle.Mileage > 1000 || model.Vehicle.Mileage < 0)
            {
                ModelState.AddModelError("Vehicle.VehicleType.VehicleTypeId", "New vehicle mileage must be between 0 and 1000.");
            }
            if (model.Vehicle.VehicleType.VehicleTypeId.Equals(2) && model.Vehicle.Mileage < 1000)
            {
                ModelState.AddModelError("Vehicle.VehicleType.VehicleTypeId", "Used vehicle mileage must be greater than 1000.");
            }
            if (string.IsNullOrWhiteSpace(model.Vehicle.Vin))
            {
                ModelState.AddModelError("Vehicle.Vin", "VIN# is required.");
            }
            if (model.Vehicle.MSRP < 1)
            {
                ModelState.AddModelError("Vehicle.MSRP", "MSRP must be a positive number.");
            }
            if (model.Vehicle.SalePrice < 1)
            {
                ModelState.AddModelError("Vehicle.SalePrice", "Sale Price must be a positive number.");
            }
            if (model.Vehicle.SalePrice > model.Vehicle.MSRP)
            {
                ModelState.AddModelError("", "Sale Price must not exceed MSRP.");
            }
            if (string.IsNullOrWhiteSpace(model.Vehicle.VehicleDescription))
            {
                ModelState.AddModelError("Vehicle.VehicleDescription", "Description is required.");
            }
            if (ModelState.IsValid)
            {
                try
                {
                    //Vehicle vehicle = new Vehicle();
                    //vehicle.ModelId = model.Vehicle.ModelId;
                    //vehicle.VehicleTypeId = model.Vehicle.VehicleTypeId;
                    //vehicle.BodyStyleId = model.Vehicle.BodyStyleId;
                    //vehicle.Year = model.Vehicle.Year;
                    //vehicle.TransmissionId = model.Vehicle.TransmissionId;
                    //vehicle.ExteriorColorId = model.Vehicle.ExteriorColorId;
                    //vehicle.InteriorColorId = model.Vehicle.InteriorColorId;
                    //vehicle.MSRP = model.Vehicle.MSRP;
                    //vehicle.SalePrice = model.Vehicle.SalePrice;
                    //vehicle.SoldOut = false;
                    //vehicle.Featured = false;
                    //vehicle.VehicleDescription = model.Vehicle.VehicleDescription;
                    //vehicle.Vin = model.Vehicle.Vin;
                    //model.Vehicle.DateModified = null;
                    string extension = Path.GetExtension(model.ImageUpload.FileName);
                    if (model.ImageUpload != null && model.ImageUpload.ContentLength > 0)
                    {
                        var savePath = Server.MapPath("~/Images");

                        string fileName = Path.GetFileNameWithoutExtension(model.ImageUpload.FileName);
                        var    filePath = Path.Combine(savePath, fileName + extension);

                        int counter = 1;
                        while (System.IO.File.Exists(filePath))
                        {
                            filePath = Path.Combine(savePath, fileName + counter.ToString() + extension);
                            counter++;
                        }

                        model.ImageUpload.SaveAs(filePath);
                        model.Vehicle.ImageFileName = Path.GetFileName(filePath);
                    }
                }

                catch
                {
                    Console.WriteLine("Error");
                }
                repo.CreateVehicle(model.Vehicle);
                return(RedirectToAction("EditVehicle", "Admin", new { id = model.Vehicle.VehicleId }));
            }
            else
            {
                var newModel = new VehicleAdminViewModel();
                newModel.SetMakes(repo.GetAllMakes());
                newModel.SetModels(repo.GetAllModels());
                newModel.SetBodyStyles(repo.GetAllBodyStyles());
                newModel.SetTransmissions(repo.GetAllTransmissions());
                newModel.SetInteriorColors(repo.GetAllInteriorColors());
                newModel.SetExteriorColors(repo.GetAllExteriorColors());
                return(View(newModel));
            }
        }