Example #1
0
        public ActionResult EditVehicle(int id)
        {
            var model = new EditVehicleViewModel();

            var carRepo       = CarRepositoryFactory.GetRepository();
            var makeModelRepo = MakeModelRepositoryFactory.GetRepository();

            model.Car           = carRepo.GetById(id);
            model.CarMakeName   = new SelectList(makeModelRepo.GetMakes(), "CarMakeId", "CarMakeName");
            model.CarModelName  = new SelectList(makeModelRepo.GetModelByMake(model.Car.CarMakeId), "CarModelId", "CarModelName");
            model.CategoryName  = new SelectList(carRepo.GetCarCategory(), "CategoryId", "CategoryName");
            model.BodyStyle     = new SelectList(carRepo.GetBodyStyle(), "BodyStyle", "BodyStyle");
            model.Transmission  = new SelectList(carRepo.GetTransmission(), "Transmission", "Transmission");
            model.CarColor      = new SelectList(carRepo.GetCarColor(), "CarColor", "CarColor");
            model.InteriorColor = new SelectList(carRepo.GetInteriorColor(), "InteriorColor", "InteriorColor");
            if (model.IsSold == null)
            {
                model.IsSold = false;
            }
            if (model.IsFeatured == null)
            {
                model.IsFeatured = false;
            }
            else
            {
                model.IsFeatured = true;
            }

            return(View(model));
        }
Example #2
0
        public IHttpActionResult EditModelsAssociatedWithMake(int carMakeId)
        {
            var repo = MakeModelRepositoryFactory.GetRepository();

            var model = repo.GetModelByMake(carMakeId);

            return(Ok(model));
        }
Example #3
0
        public ActionResult Models(AddModelViewModel model)
        {
            var repo = MakeModelRepositoryFactory.GetRepository();

            if (string.IsNullOrEmpty(model.CarModelName))
            {
                ModelState.AddModelError("CarModelName",
                                         "Please enter a car model");
            }

            if (ModelState.IsValid)
            {
                try
                {
                    model.NewModel              = new CarModel();
                    model.NewModel.CarMakeName  = model.CarMakeName;
                    model.NewModel.CarModelName = model.CarModelName;
                    model.NewModel.DateCreated  = model.DateCreated;

                    var userMgr = new UserManager <ApplicationUser>(new UserStore <ApplicationUser>(context));

                    var user = userMgr.FindByName(User.Identity.Name);

                    ViewBag.UserName = user.UserName;

                    repo.AddModel(model.NewModel);

                    return(RedirectToAction("Models", "Admin"));
                }
                catch (Exception ex)
                {
                    throw ex;
                }
            }
            else
            {
                model.Models = repo.GetModels();
                model.Makes  = (from type in repo.GetMakes()
                                select new SelectListItem()
                {
                    Text = type.CarMakeName,
                    Value = type.CarMakeName,
                }).ToList();

                return(View(model));
            }
        }
Example #4
0
        public ActionResult Makes()
        {
            if (Request.IsAuthenticated)
            {
                var userMgr = new UserManager <ApplicationUser>(new UserStore <ApplicationUser>(context));

                var user = userMgr.FindByName(User.Identity.Name);

                ViewBag.UserName = user.UserName;
            }

            AddMakeViewModel model = new AddMakeViewModel();
            var repo = MakeModelRepositoryFactory.GetRepository();

            model.Makes = repo.GetMakes();

            return(View(model));
        }
Example #5
0
        public ActionResult Makes(AddMakeViewModel model)
        {
            var repo = MakeModelRepositoryFactory.GetRepository();

            if (string.IsNullOrEmpty(model.CarMakeName))
            {
                ModelState.AddModelError("CarMakeName",
                                         "Please enter a car make name");
            }

            if (ModelState.IsValid)
            {
                try
                {
                    model.NewMake             = new CarMake();
                    model.NewMake.CarMakeId   = model.CarMakeId;
                    model.NewMake.CarMakeName = model.CarMakeName;
                    model.NewMake.DateCreated = model.DateCreated;

                    var userMgr = new UserManager <ApplicationUser>(new UserStore <ApplicationUser>(context));

                    var user = userMgr.FindByName(User.Identity.Name);

                    ViewBag.UserName = user.UserName;

                    repo.AddMake(model.NewMake);

                    return(RedirectToAction("Makes", "Admin"));
                }
                catch (Exception ex)
                {
                    throw ex;
                }
            }
            else
            {
                model.Makes = repo.GetMakes();

                return(View(model));
            }
        }
Example #6
0
        public ActionResult Models()
        {
            if (Request.IsAuthenticated)
            {
                var userMgr = new UserManager <ApplicationUser>(new UserStore <ApplicationUser>(context));

                var user = userMgr.FindByName(User.Identity.Name);

                ViewBag.UserName = user.UserName;
            }

            AddModelViewModel model = new AddModelViewModel();
            var repo = MakeModelRepositoryFactory.GetRepository();

            model.Models = repo.GetModels();
            model.Makes  = (from type in repo.GetMakes()
                            select new SelectListItem()
            {
                Text = type.CarMakeName,
                Value = type.CarMakeName,
            }).ToList();

            return(View(model));
        }
Example #7
0
        private AddVehicleViewModel GetAddVehicleModel(AddVehicleViewModel model)
        {
            var carRepo       = CarRepositoryFactory.GetRepository();
            var makeModelRepo = MakeModelRepositoryFactory.GetRepository();

            model.Car           = new Car();
            model.CarMakeName   = new SelectList(makeModelRepo.GetMakes(), "CarMakeId", "CarMakeName");
            model.CarModelName  = new SelectList(makeModelRepo.GetModels(), "CarModelId", "CarModelName");
            model.CategoryName  = new SelectList(carRepo.GetCarCategory(), "CategoryId", "CategoryName");
            model.BodyStyle     = new SelectList(carRepo.GetBodyStyle(), "BodyStyle", "BodyStyle");
            model.Transmission  = new SelectList(carRepo.GetTransmission(), "Transmission", "Transmission");
            model.CarColor      = new SelectList(carRepo.GetCarColor(), "CarColor", "CarColor");
            model.InteriorColor = new SelectList(carRepo.GetInteriorColor(), "InteriorColor", "InteriorColor");
            if (model.IsSold == null)
            {
                model.IsSold = false;
            }
            if (model.IsFeatured == null)
            {
                model.IsFeatured = false;
            }

            return(model);
        }
Example #8
0
        public ActionResult EditVehicle(EditVehicleViewModel model)
        {
            var repo = CarRepositoryFactory.GetRepository();

            int     carMileage;
            decimal carSalePrice;
            int     carYear;
            decimal msrp;

            if (!Decimal.TryParse(model.Car.CarPrice.ToString(), out msrp))
            {
                ModelState.AddModelError("Car.CarPrice",
                                         "Please enter a valid number.");
            }
            if (!Int32.TryParse(model.Car.Mileage.ToString(), out carMileage))
            {
                ModelState.AddModelError("Car.Mileage",
                                         "Please enter a valid number.");
            }
            if (!Decimal.TryParse(model.Car.CarSalePrice.ToString(), out carSalePrice))
            {
                ModelState.AddModelError("Car.CarSalePrice",
                                         "Please enter a valid number.");
            }
            if (!Int32.TryParse(model.Car.CarYear.ToString(), out carYear))
            {
                ModelState.AddModelError("Car.CarYear",
                                         "Please enter a valid number.");
            }
            if (model.Car.CarYear.ToString().Length < 4)
            {
                ModelState.AddModelError("Car.CarYear",
                                         "Please enter a four digit year.");
            }

            if (ModelState.IsValid)
            {
                try
                {
                    var oldCar = repo.GetById(model.Car.CarId);

                    if (model.IsSold == null)
                    {
                        model.Car.IsSold = false;
                    }

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

                        string fileName  = Path.GetFileNameWithoutExtension("inventory");
                        string extension = Path.GetExtension(model.ImageUpload.FileName);
                        if (extension != "png" || extension != "jpg" || extension != "jpeg")
                        {
                            ModelState.AddModelError("Car.ImageFileName",
                                                     "Please upload a png, jpg, or jpeg file.");
                        }

                        var filePath = Path.Combine(savepath, fileName + model.Car.CarId + extension);

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

                        // delete old file
                        var oldPath = Path.Combine(savepath, oldCar.ImageFileName);
                        if (System.IO.File.Exists(oldPath))
                        {
                            System.IO.File.Delete(oldPath);
                        }
                    }
                    else
                    {
                        // they did not replace the old file, so keep the old file name
                        model.Car.ImageFileName = oldCar.ImageFileName;
                    }

                    repo.Update(model.Car);

                    TempData["Message"] = "Vehicle has been saved.";
                    return(RedirectToAction("EditVehicle", new { id = model.Car.CarId }));
                }
                catch (Exception ex)
                {
                    throw ex;
                }
            }
            else
            {
                var carRepo       = CarRepositoryFactory.GetRepository();
                var makeModelRepo = MakeModelRepositoryFactory.GetRepository();

                model.Car           = carRepo.GetById(model.Car.CarId);
                model.CarMakeName   = new SelectList(makeModelRepo.GetMakes(), "CarMakeId", "CarMakeName");
                model.CarModelName  = new SelectList(makeModelRepo.GetModelByMake(model.Car.CarMakeId), "CarModelId", "CarModelName");
                model.CategoryName  = new SelectList(carRepo.GetCarCategory(), "CategoryId", "CategoryName");
                model.BodyStyle     = new SelectList(carRepo.GetBodyStyle(), "BodyStyle", "BodyStyle");
                model.Transmission  = new SelectList(carRepo.GetTransmission(), "Transmission", "Transmission");
                model.CarColor      = new SelectList(carRepo.GetCarColor(), "CarColor", "CarColor");
                model.InteriorColor = new SelectList(carRepo.GetInteriorColor(), "InteriorColor", "InteriorColor");
                if (model.IsSold == null)
                {
                    model.IsSold = false;
                }
                if (model.IsFeatured == null)
                {
                    model.IsFeatured = false;
                }
                else
                {
                    model.IsFeatured = true;
                }

                return(View(model));
            }
        }
Example #9
0
        public ActionResult AddVehicle(AddVehicleViewModel model)
        {
            var repo = CarRepositoryFactory.GetRepository();

            int     carMileage;
            decimal carSalePrice;
            int     carYear;
            decimal msrp;

            if (!Decimal.TryParse(model.Car.CarPrice.ToString(), out msrp))
            {
                ModelState.AddModelError("Car.CarPrice",
                                         "Please enter a valid number.");
            }
            if (!Int32.TryParse(model.Car.Mileage.ToString(), out carMileage))
            {
                ModelState.AddModelError("Car.Mileage",
                                         "Please enter a valid number.");
            }
            if (!Decimal.TryParse(model.Car.CarSalePrice.ToString(), out carSalePrice))
            {
                ModelState.AddModelError("Car.CarSalePrice",
                                         "Please enter a valid number.");
            }
            if (!Int32.TryParse(model.Car.CarYear.ToString(), out carYear))
            {
                ModelState.AddModelError("Car.CarYear",
                                         "Please enter a valid number.");
            }
            if (model.Car.CarYear.ToString().Length < 4)
            {
                ModelState.AddModelError("Car.CarYear",
                                         "Please enter a four digit year.");
            }
            if (model.Car.CarYear < 2000 || model.Car.CarYear > DateTime.Now.Year + 1)
            {
                ModelState.AddModelError("Car.CarYear",
                                         "Car year can not be less than year 2000 or greater than next year.");
            }
            if (model.ImageUpload == null)
            {
                ModelState.AddModelError("Car.ImageFileName",
                                         "Please upload an image of the vehicle.");
            }
            if ((model.Car.CategoryId == 2) && (model.Car.CarYear >= DateTime.Now.Year))
            {
                if (model.Car.Mileage > 1000)
                {
                    ModelState.AddModelError("Car.Mileage",
                                             "Mileage must be below 1000 miles for new vehicles.");
                }
            }

            if (ModelState.IsValid)
            {
                try
                {
                    if (model.IsSold == null)
                    {
                        model.Car.IsSold = false;
                    }
                    if (model.IsFeatured == null)
                    {
                        model.IsFeatured = false;
                    }

                    repo.Insert(model.Car);

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

                        string fileName  = Path.GetFileNameWithoutExtension("inventory");
                        string extension = Path.GetExtension(model.ImageUpload.FileName);
                        if (extension != "png" || extension != "jpg" || extension != "jpeg")
                        {
                            ModelState.AddModelError("Car.ImageFileName",
                                                     "Please upload a png, jpg, or jpeg file.");
                        }
                        var filePath = Path.Combine(savepath, fileName + model.Car.CarId + extension);

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

                    repo.Update(model.Car);

                    TempData["Message"] = "New vehicle has been added.";
                    return(RedirectToAction("EditVehicle", new { id = model.Car.CarId }));
                }
                catch (Exception ex)
                {
                    throw ex;
                }
            }
            else
            {
                var carRepo       = CarRepositoryFactory.GetRepository();
                var makeModelRepo = MakeModelRepositoryFactory.GetRepository();

                model.Car           = new Car();
                model.CarMakeName   = new SelectList(makeModelRepo.GetMakes(), "CarMakeId", "CarMakeName");
                model.CarModelName  = new SelectList(makeModelRepo.GetModels(), "CarModelId", "CarModelName");
                model.CategoryName  = new SelectList(carRepo.GetCarCategory(), "CategoryId", "CategoryName");
                model.BodyStyle     = new SelectList(carRepo.GetBodyStyle(), "BodyStyle", "BodyStyle");
                model.Transmission  = new SelectList(carRepo.GetTransmission(), "Transmission", "Transmission");
                model.CarColor      = new SelectList(carRepo.GetCarColor(), "CarColor", "CarColor");
                model.InteriorColor = new SelectList(carRepo.GetInteriorColor(), "InteriorColor", "InteriorColor");
                if (model.IsSold == null)
                {
                    model.IsSold = false;
                }
                if (model.IsFeatured == null)
                {
                    model.IsFeatured = false;
                }

                return(View(model));
            }
        }