Пример #1
0
        public ActionResult Create([Bind(Include = "Id,Name,BrandId")] CarModel model)
        {
            if (ModelState.IsValid)
            {
                var opStatus = new OperationStatus {
                    Status = true, Message = "Brand added"
                };

                try
                {
                    _modelRepository.Add(model);
                }
                catch (Exception exp)
                {
                    opStatus = OperationStatus.CreateFromExeption("Error adding model car.", exp);
                }

                TempData["OperationStatus"] = opStatus;

                return(RedirectToAction("Index", new { Id = model.BrandId }));
            }

            ViewBag.Brand = model.Brand;
            return(View(model));
        }
Пример #2
0
        public ActionResult Create([Bind(Include = "Id,Color,Price,EngineCapacity,Description,ModelId")] Car car, HttpPostedFileBase image)
        {
            string userId = User.Identity.GetUserId();

            if (userId == null)
            {
                return(HttpNotFound());
            }

            var model = _modelCarRepository.GetModelById(car.ModelId);

            if (model == null)
            {
                ModelState.AddModelError("ModelId", "No existing model");
            }

            if (ModelState.IsValid)
            {
                car.UserId = userId;
                if (image != null)
                {
                    car.ImageMimeType = image.ContentType;
                    car.ImageData     = new byte[image.ContentLength];
                    image.InputStream.Read(car.ImageData, 0, image.ContentLength);
                }
                var opStatus = new OperationStatus {
                    Status = true, Message = "Car added"
                };

                try
                {
                    _carRepository.Add(car);
                }
                catch (Exception exp)
                {
                    opStatus = OperationStatus.CreateFromExeption("Error adding car.", exp);
                }

                TempData["OperationStatus"] = opStatus;
                return(RedirectToAction("Index"));
            }

            ViewBag.BrandTree = _brandsTreeRepository.GetBrandsModelsTree();
            return(View(car));
        }
Пример #3
0
        public ActionResult DeleteConfirmed(int id)
        {
            Car car      = _carRepository.GetCarById(id);
            var opStatus = new OperationStatus {
                Status = true, Message = "Car deleted"
            };

            try
            {
                _carRepository.Delete(car);
            }
            catch (Exception exp)
            {
                opStatus = OperationStatus.CreateFromExeption("Error deleting car.", exp);
            }

            TempData["OperationStatus"] = opStatus;
            return(RedirectToAction("Index", new { Id = car.ModelId }));
        }
Пример #4
0
        public ActionResult Edit([Bind(Include = "Id,Name")] CarBrand brand)
        {
            if (ModelState.IsValid)
            {
                var opStatus = new OperationStatus {
                    Status = true, Message = "Brand updated"
                };

                try
                {
                    _brandRepository.Update(brand);
                }
                catch (Exception exp)
                {
                    opStatus = OperationStatus.CreateFromExeption("Error updating brand car.", exp);
                }

                TempData["OperationStatus"] = opStatus;
                return(RedirectToAction("Index"));
            }
            return(View(brand));
        }
Пример #5
0
        public ActionResult Edit([Bind(Include = "Id,Name,BrandId")] CarModel model)
        {
            if (ModelState.IsValid)
            {
                var opStatus = new OperationStatus {
                    Status = true, Message = "model updated"
                };

                try
                {
                    _modelRepository.Update(model);
                }
                catch (Exception exp)
                {
                    opStatus = OperationStatus.CreateFromExeption("Error updating model car.", exp);
                }

                TempData["OperationStatus"] = opStatus;
                return(RedirectToAction("Index", new { Id = model.BrandId }));
            }
            ViewBag.BrandId = new SelectList(_brandRepository.GetAll(), "Id", "Name", model.BrandId);
            return(View(model));
        }
Пример #6
0
        public ActionResult DeleteConfirmed(int id)
        {
            CarBrand brand = _brandRepository.GetBrandById(id);

            if (brand == null)
            {
                return(HttpNotFound());
            }

            if (!brand.Models.Any())
            {
                var opStatus = new OperationStatus {
                    Status = true, Message = "Brand deleted"
                };
                try
                {
                    _brandRepository.Delete(brand);
                }
                catch (Exception exp)
                {
                    opStatus = OperationStatus.CreateFromExeption("Error deleting brand car.", exp);
                }

                TempData["OperationStatus"] = opStatus;
            }
            else
            {
                TempData["OperationStatus"] = new OperationStatus()
                {
                    Status  = true,
                    Message = "You must delete all models for this brand first"
                }
            };

            return(RedirectToAction("Index"));
        }
    }
Пример #7
0
        public ActionResult DeleteConfirmed(int id)
        {
            CarModel model = _modelRepository.GetModelById(id);

            if (model == null)
            {
                return(HttpNotFound());
            }
            var opStatus = new OperationStatus {
                Status = true, Message = "model deleted"
            };

            if (!model.Cars.Any())
            {
                try
                {
                    _modelRepository.Delete(model);
                }
                catch (Exception exp)
                {
                    opStatus = OperationStatus.CreateFromExeption("Error deleting model car.", exp);
                }

                TempData["OperationStatus"] = opStatus;
            }
            else
            {
                TempData["OperationStatus"] = new OperationStatus()
                {
                    Status = true, Message = "You must delete all cars for this model first"
                }
            };


            return(RedirectToAction("Index", new { Id = model.BrandId }));
        }
    }