예제 #1
0
        public ActionResult Edit(EditVM model)
        {
            if (!ModelState.IsValid)
            {
                return(View(model));
            }

            CarsRepository repo = new CarsRepository();

            Car item = new Car();

            item.Id    = model.Id;
            item.Make  = model.Make;
            item.Model = model.Marka;
            item.Year  = model.Year;
            item.Price = model.Price;
            item.Color = model.Color;

            if (item.Id > 0)
            {
                repo.Update(item);
            }
            else
            {
                repo.Insert(item);
            }

            return(RedirectToAction("Index", "Cars"));
        }
예제 #2
0
 //add car
 public void Insert(CarViewModel carVM)
 {
     DBCarRent.Car car = MapViewModelToModel(carVM);
     using (CarsRepository carsRepository = new CarsRepository())
     {
         carsRepository.Insert(car);
     }
 }
        public async Task <ActionResult> CreateCar(CreateCarViewModel newCarViewModel)
        {
            if (ModelState.IsValid)
            {
                string carId = Guid.NewGuid().ToString();
                Car    car   = new Car()
                {
                    Id      = carId,
                    Name    = $"{newCarViewModel.Make} {newCarViewModel.Model}",
                    TrackId = newCarViewModel.TrackId
                };

                string carsImagesPath = HttpContext.Server.MapPath("~/Content/Images/Cars");
                bool   fileNotFound   = true;
                // Add uploaded image if selected, or use default
                if (Request.Files.Count > 0)
                {
                    var postedFile = Request.Files[0];
                    if (postedFile != null && postedFile.ContentLength > 0)
                    {
                        fileNotFound = false;
                        string extension   = Path.GetExtension(postedFile.FileName);
                        string carFileName = $"{carId}{extension}";
                        car.ImageName = carFileName;
                        string saveToPath = Path.Combine(carsImagesPath, carFileName);
                        postedFile.SaveAs(saveToPath);
                    }
                }

                if (fileNotFound)
                {
                    var defaultCarFileToUsePath = Path.Combine(carsImagesPath, "0.jpg");
                    if (System.IO.File.Exists(defaultCarFileToUsePath))
                    {
                        string defaultCarImageWithUniqueCarId = $"{carId}.jpg";
                        car.ImageName = defaultCarImageWithUniqueCarId;
                        string defaultSaveToPath = Path.Combine(carsImagesPath, defaultCarImageWithUniqueCarId);
                        byte[] bytes             = System.IO.File.ReadAllBytes(defaultCarFileToUsePath);
                        System.IO.File.WriteAllBytes(defaultSaveToPath, bytes);
                    }
                }

                try
                {
                    CarsRepository <Car, CarDTO> carsRepo = new CarsRepository <Car, CarDTO>();
                    car = await carsRepo.Insert(car);

                    TempData["CarId"] = car.Id;
                }
                catch (Exception)
                {
                    ModelState.AddModelError(string.Empty, "Failed to save new car.");
                }
            }

            return(RedirectToAction("Index"));
        }
예제 #4
0
        public async Task <IHttpActionResult> PostCar(Car car)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            car = await repo.Insert(car);

            return(CreatedAtRoute("DefaultApi", new { id = car.Id }, Mapper.Map <CarDTO>(car)));
        }