public ActionResult Models(ModelsViewModel model)
        {
            if (ModelState.IsValid)
            {
                var repo = ModelsRepositoryFactory.GetRepository();

                try
                {
                    CarMastery.Models.Tables.Models addModel = new CarMastery.Models.Tables.Models();
                    addModel.UserId           = AuthorizeUtilities.GetUserId(this);
                    addModel.ModelDateAdded   = DateTime.Now;
                    addModel.ModelDescription = model.ModelToAdd.ModelDescription;
                    addModel.MakeId           = model.ModelToAdd.MakeId;

                    repo.AddModel(addModel);

                    return(RedirectToAction("Models"));
                }
                catch (Exception ex)
                {
                    throw ex;
                }
            }
            else
            {
                var makesRepo = MakesRepositoryFactory.GetRepository();
                model.Makes = new SelectList(makesRepo.GetAllMakes(), "MakeId", "MakeDescription");
                return(View(model));
            }
        }
        public ActionResult EditVehicle(int id)
        {
            {
                var model              = new EditVehiclesViewModel();
                var vehiclesRepo       = VehicleRepositoryFactory.GetRepository();
                var makesRepo          = MakesRepositoryFactory.GetRepository();
                var modelsRepo         = ModelsRepositoryFactory.GetRepository();
                var vehicleTypesRepo   = VehicleTypesRepositoryFactory.GetRepository();
                var bodyStylesRepo     = BodyStylesRepositoryFactory.GetRepository();
                var transmissionsRepo  = TransmissionsRepositoryFactory.GetRepository();
                var bodyColorsRepo     = BodyColorsRepositoryFactory.GetRepository();
                var interiorColorsRepo = InteriorColorsRepositoryFactory.GetRepository();

                model.VehicleToEdit = vehiclesRepo.SelectVehicle(id);
                model.Makes         = makesRepo.GetAllMakes();
                model.SelectedMake  = makesRepo.GetMakeForModel(model.VehicleToEdit.ModelId).MakeId;
                model.VehicleType   = new SelectList(vehicleTypesRepo.GetAllVehicleTypes(), "VehicleTypeId", "VehicleTypeDescription");
                model.BodyStyle     = new SelectList(bodyStylesRepo.GetAllBodyStyles(), "BodyStyleId", "BodyStyleDescription");
                model.Transmission  = new SelectList(transmissionsRepo.GetAllTransmissions(), "TransmissionId", "TransmissionDescription");
                model.BodyColor     = new SelectList(bodyColorsRepo.GetAllBodyColors(), "BodyColorId", "BodyColorDescription");
                model.InteriorColor = new SelectList(interiorColorsRepo.GetAllInteriorColors(), "InteriorColorId", "InteriorColorDescription");

                return(View(model));
            }
        }
        public ActionResult EditVehicle(EditVehiclesViewModel model)
        {
            if (ModelState.IsValid)
            {
                var repo = VehicleRepositoryFactory.GetRepository();

                try
                {
                    var oldVehicle = repo.SelectVehicle(model.VehicleToEdit.VehicleId);
                    if (model.ReplacePicture != null && model.ReplacePicture.ContentLength > 0)
                    {
                        var savepath  = Server.MapPath("~/Images");
                        var vehicleId = model.VehicleToEdit.VehicleId.ToString();
                        var filePath  = Path.Combine(savepath, "inventory-" + vehicleId + ".PNG");

                        model.ReplacePicture.SaveAs(filePath);
                        model.VehicleToEdit.VehiclePicture = Path.GetFileName(filePath);
                    }
                    else
                    {
                        model.VehicleToEdit.VehiclePicture = oldVehicle.VehiclePicture;
                    }

                    repo.UpdateVehicle(model.VehicleToEdit);

                    return(RedirectToAction("Vehicles"));
                }
                catch (Exception ex)
                {
                    throw ex;
                }
            }
            else
            {
                var vehiclesRepo       = VehicleRepositoryFactory.GetRepository();
                var makesRepo          = MakesRepositoryFactory.GetRepository();
                var modelsRepo         = ModelsRepositoryFactory.GetRepository();
                var vehicleTypesRepo   = VehicleTypesRepositoryFactory.GetRepository();
                var bodyStylesRepo     = BodyStylesRepositoryFactory.GetRepository();
                var transmissionsRepo  = TransmissionsRepositoryFactory.GetRepository();
                var bodyColorsRepo     = BodyColorsRepositoryFactory.GetRepository();
                var interiorColorsRepo = InteriorColorsRepositoryFactory.GetRepository();

                model.VehicleToEdit = vehiclesRepo.SelectVehicle(model.VehicleToEdit.VehicleId);
                model.Makes         = makesRepo.GetAllMakes();
                model.VehicleType   = new SelectList(vehicleTypesRepo.GetAllVehicleTypes(), "VehicleTypeId", "VehicleTypeDescription");
                model.BodyStyle     = new SelectList(bodyStylesRepo.GetAllBodyStyles(), "BodyStyleId", "BodyStyleDescription");
                model.Transmission  = new SelectList(transmissionsRepo.GetAllTransmissions(), "TransmissionId", "TransmissionDescription");
                model.BodyColor     = new SelectList(bodyColorsRepo.GetAllBodyColors(), "BodyColorId", "BodyColorDescription");
                model.InteriorColor = new SelectList(interiorColorsRepo.GetAllInteriorColors(), "InteriorColorId", "InteriorColorDescription");

                return(View(model));
            }
        }
        public ActionResult Models()
        {
            var model     = new ModelsViewModel();
            var repo      = ModelsRepositoryFactory.GetRepository();
            var makesRepo = MakesRepositoryFactory.GetRepository();

            model.ModelsList = repo.GetAllModels().ToList();
            model.ModelToAdd = new CarMastery.Models.Tables.Models();
            model.Makes      = new SelectList(makesRepo.GetAllMakes(), "MakeId", "MakeDescription");

            return(View(model));
        }
Пример #5
0
        public IHttpActionResult SearchForModels(int makeId)
        {
            var repo = ModelsRepositoryFactory.GetRepository();

            try
            {
                var result = repo.GetModelsForMake(makeId);
                return(Ok(result));
            }
            catch (Exception ex)
            {
                return(BadRequest(ex.Message));
            }
        }
        public ActionResult AddVehicle()
        {
            {
                var model              = new AddVehiclesViewModel();
                var makesRepo          = MakesRepositoryFactory.GetRepository();
                var modelsRepo         = ModelsRepositoryFactory.GetRepository();
                var vehicleTypesRepo   = VehicleTypesRepositoryFactory.GetRepository();
                var bodyStylesRepo     = BodyStylesRepositoryFactory.GetRepository();
                var transmissionsRepo  = TransmissionsRepositoryFactory.GetRepository();
                var bodyColorsRepo     = BodyColorsRepositoryFactory.GetRepository();
                var interiorColorsRepo = InteriorColorsRepositoryFactory.GetRepository();

                model.Makes         = makesRepo.GetAllMakes();
                model.VehicleType   = new SelectList(vehicleTypesRepo.GetAllVehicleTypes(), "VehicleTypeId", "VehicleTypeDescription");
                model.BodyStyle     = new SelectList(bodyStylesRepo.GetAllBodyStyles(), "BodyStyleId", "BodyStyleDescription");
                model.Transmission  = new SelectList(transmissionsRepo.GetAllTransmissions(), "TransmissionId", "TransmissionDescription");
                model.BodyColor     = new SelectList(bodyColorsRepo.GetAllBodyColors(), "BodyColorId", "BodyColorDescription");
                model.InteriorColor = new SelectList(interiorColorsRepo.GetAllInteriorColors(), "InteriorColorId", "InteriorColorDescription");
                model.VehicleToAdd  = new Vehicles();

                return(View(model));
            }
        }