コード例 #1
0
        public async Task <IActionResult> AddCarOwner(CarOwnerAddVM vm)
        {
            var user = await _accountService.GetOwner(vm.Name, vm.Surname);

            if (user != null)
            {
                var result = _carService.AddCarToOwner(user.ID, vm.carId);
                if (result.Result)
                {
                    var detailsVM = new CarDetailsVM()
                    {
                        Car = await _carService.GetCarWithOwners(vm.carId),
                        IsUserValidForEditing = await _carService.IsUserOwner(vm.carId, Convert.ToInt32(User.Identity.Name.ToString()))
                    };
                    return(View("../Cars/Details", detailsVM));
                }
                else
                {
                    ModelState.AddModelError("", "This user alreary own this car");
                }
            }
            else
            {
                ModelState.AddModelError("", "There is no user with that name and surname");
            }
            return(View(vm));
        }
コード例 #2
0
        public IActionResult UpdateAdvertisementGet(int?id)
        {
            if (id == null)
            {
                return(NotFound());
            }

            ViewBag.Cities     = _context.Cities;
            ViewBag.Marks      = _context.Marks;
            ViewBag.Fuels      = _context.Fuels;
            ViewBag.Bans       = _context.Bans;
            ViewBag.Colors     = _context.Colors;
            ViewBag.SpeedBoxes = _context.SpeedBoxes;

            CarDetailsVM model = new CarDetailsVM()
            {
                automobile = _context.Automobiles.Find(id),
                carImages  = _context.CarImages.Where(x => x.AutomobileId == id)
            };

            if (model == null)
            {
                return(NotFound());
            }

            return(View(model));
        }
コード例 #3
0
        public async Task <IActionResult> Details(int id)
        {
            var car = _CarRepository.GetCarById(id);

            if (car == null)
            {
                return(NotFound());
            }
            var user = await _userManager.Users.FirstOrDefaultAsync(u => u.Id == car.UserId);

            if (user == null)
            {
                return(NotFound());
            }

            var pictures = _pictureRepository.GetPicturesByCarId(id).ToList();

            var vm = new CarDetailsVM
            {
                Car         = car,
                UserName    = user.UserName,
                Name        = user.Name,
                PhoneNumber = user.PhoneNumber,
                City        = user.City,
                Email       = user.Email,
                DateJoined  = user.DateJoined,
                ReturnUrl   = HttpContext.Request.Host.ToString() + HttpContext.Request.Path.ToString(),
                Pictures    = pictures
            };

            return(View(vm));
        }
コード例 #4
0
        public async Task <IActionResult> CarDetails(int?id)
        {
            CarDetailsVM carDetails = new CarDetailsVM()
            {
                automobile = await _context.Automobiles.FindAsync(id),
                carImages  = _context.CarImages
            };

            return(View(carDetails));
        }
コード例 #5
0
        public async Task <IActionResult> Details(int id)
        {
            var vm = new CarDetailsVM()
            {
                Car = await _carService.GetCarWithOwners(id),
                IsUserValidForEditing = await _carService.IsUserOwner(id, Convert.ToInt32(User.Identity.Name.ToString()))
            };

            return(View(vm));
        }
コード例 #6
0
        public void ShouldValidateNewCar()
        {
            var validator = new Validation();

            var car = new CarDetailsVM();



            bool isValid = validator.IsModelValid(car);

            Assert.IsTrue(isValid);
        }
コード例 #7
0
ファイル: HomeController.cs プロジェクト: mat010/Cars24
        public JsonResult Insert(CarDetailsVM carDetailsVm)
        {
            string validationMessageInputs = string.Empty;

            if (validate.IsModelValid(carDetailsVm, out validationMessageInputs))
            {
                if (repository.Insert(carDetailsVm))
                {
                    return(Json(new { Success = true, Message = "Data will save successfuly" }, JsonRequestBehavior.AllowGet));
                }

                return(Json(new { Success = false, Message = "Problem occured while saving form to database" }, JsonRequestBehavior.AllowGet));
            }
            return(Json(new { Success = false, Message = $"Please check the following inputs: {validationMessageInputs}" }, JsonRequestBehavior.AllowGet));
        }
コード例 #8
0
        public async Task <IActionResult> AdvertisementDetails(int?id)
        {
            Automobile selectedAuto = await _context.Automobiles.FindAsync(id);

            if (selectedAuto == null)
            {
                return(NotFound());
            }
            CarDetailsVM carDetails = new CarDetailsVM()
            {
                automobile = selectedAuto,
                carImages  = _context.CarImages
            };

            return(View(carDetails));
        }
コード例 #9
0
ファイル: CarController.cs プロジェクト: GregMiaz/TrustedRide
        public IActionResult Details(int id)
        {
            var car = _carRepository.GetCarById(id);

            if (car == null)
            {
                return(NotFound());
            }
            var model = new CarDetailsVM()
            {
                Name             = car.Name,
                ShortDescription = car.ShortDescription,
                LongDescription  = car.LongDescription,
                Price            = car.Price,
                PictureUrl       = car.PictureUrl
            };

            return(View(model));
        }
コード例 #10
0
        public IActionResult DeleteAdvertisement(int?id)
        {
            if (id == null)
            {
                return(NotFound());
            }
            CarDetailsVM model = new CarDetailsVM()
            {
                automobile = _context.Automobiles.Find(id),
                carImages  = _context.CarImages
            };

            if (model == null)
            {
                return(NotFound());
            }

            return(View(model));
        }
コード例 #11
0
        public async Task <IActionResult> Contact(CarDetailsVM vm)
        {
            var fromUser = await _userManager.GetUserAsync(HttpContext.User);

            if (ModelState.IsValid)
            {
                if (!string.IsNullOrEmpty(vm.Message) && !string.IsNullOrEmpty(vm.Email) && fromUser != null)
                {
                    var car = new Car();
                    car.Id = vm.Car.Id;


                    var subject = "You have the new message from " + fromUser.UserName.ToString();
                    await _emailService.SendEmailAsync(vm.Email, vm.Message, subject);

                    return(RedirectToAction(nameof(MessageSent), car));
                }
            }

            return(RedirectToAction("Index", "Home"));
        }
コード例 #12
0
        public async Task <IActionResult> Details(int?id)
        {
            if (id == null)
            {
                return(PartialView("ErrorPage"));
            }
            if (!_context.Advertisements.Any(ad => ad.Id == id))
            {
                return(PartialView("ErrorPage"));
            }
            var selectedAd = await _context.Advertisements.Where(ad => ad.Id == id)
                             .Include(ad => ad.Car)
                             .Include(ad => ad.Car.Make)
                             .Include(ad => ad.Car.Make.Brand)
                             .Include(ad => ad.Car.Transmission)
                             .Include(ad => ad.Car.FuelType)
                             .Include(ad => ad.City)
                             .Include(ad => ad.City.Country)
                             .Include(ad => ad.Car.CarDetailPhotos)
                             .FirstOrDefaultAsync();

            CarDetailsVM viewModel = new CarDetailsVM
            {
                Advertisement         = selectedAd,
                RelatedAdvertisements = _context.Advertisements.Where(ad => ad.Car.Make.BrandId == selectedAd.Car.Make.BrandId && ad.Id != selectedAd.Id)
                                        .Include(ad => ad.Car)
                                        .Include(ad => ad.Car.Make)
                                        .Include(ad => ad.Car.Make.Brand)
                                        .Include(ad => ad.Car.Transmission)
                                        .Include(ad => ad.Car.FuelType)
                                        .Include(ad => ad.City)
                                        .Include(ad => ad.City.Country)
                                        .OrderByDescending(ad => ad.UpdatedDate)
                                        .Take(3),
                Blogs = _context.Blogs.OrderByDescending(blog => blog.UpdatedDate).Take(3)
            };

            return(View(viewModel));
        }
コード例 #13
0
        public bool IsModelValid(CarDetailsVM carDetailsVm, out string validationMessageInputs)
        {
            validationMessageInputs = string.Empty;
            StringBuilder sb = new StringBuilder();

            if (carDetailsVm.imgList == null)
            {
                sb.Append("At least one image is required\n");
                validationMessageInputs = sb.ToString();
                //return false;
            }

            if (carDetailsVm.BodyId < 0)
            {
                sb.Append(", Car body\n");
                validationMessageInputs = sb.ToString();
                //return false;
            }

            if (carDetailsVm.BrandId < 0)
            {
                sb.Append(", Brand\n");
                validationMessageInputs = sb.ToString();
                //return false;
            }

            if (carDetailsVm.Capacity == null || carDetailsVm.Capacity < 500)
            {
                sb.Append(", Capacity\n");
                validationMessageInputs = sb.ToString();
                //return false;
            }

            if (string.IsNullOrEmpty(carDetailsVm.City))
            {
                sb.Append(", City\n");
                validationMessageInputs = sb.ToString();
                //return false;
            }

            if (carDetailsVm.ConditionId < 0)
            {
                sb.Append(", Condition\n");
                validationMessageInputs = sb.ToString();
                //return false;
            }

            if (string.IsNullOrEmpty(carDetailsVm.Description))
            {
                sb.Append(", Description\n");
                validationMessageInputs = sb.ToString();
                //return false;
            }

            if (carDetailsVm.Distance == null || carDetailsVm.Distance < 100)
            {
                sb.Append(", Distance\n");
                validationMessageInputs = sb.ToString();
                //return false;
            }

            if (string.IsNullOrEmpty(carDetailsVm.Email))
            {
                sb.Append(", Email\n");
                validationMessageInputs = sb.ToString();
                //return false;
            }

            if (carDetailsVm.ModelId < 0)
            {
                sb.Append(", Model\n");
                validationMessageInputs = sb.ToString();
                //return false;
            }

            if (carDetailsVm.PetrolTypeId < 0)
            {
                sb.Append(", Petrol type\n");
                validationMessageInputs = sb.ToString();
                //return false;
            }

            if (string.IsNullOrEmpty(carDetailsVm.Phone))
            {
                sb.Append(", Phone\n");
                validationMessageInputs = sb.ToString();
                //return false;
            }

            if (carDetailsVm.Price <= 0)
            {
                sb.Append(", Price\n");
                validationMessageInputs = sb.ToString();
                //return false;
            }

            if (carDetailsVm.ProductYear == null || carDetailsVm.ProductYear > DateTime.Now.Year)
            {
                sb.Append(", Product year\n");
                validationMessageInputs = sb.ToString();
                //return false;
            }

            if (carDetailsVm.TransmissionTypeId < 0)
            {
                sb.Append(", Transmision type\n");
                validationMessageInputs = sb.ToString();
                //return false;
            }

            if (string.IsNullOrEmpty(carDetailsVm.UserName))
            {
                sb.Append(", User name\n");
                validationMessageInputs = sb.ToString();
                //return false;
            }

            if (string.IsNullOrEmpty(validationMessageInputs))
            {
                return(true);
            }
            return(false);
        }
コード例 #14
0
        public async Task <IActionResult> UpdateAdvertisementPost(Automobile automobile, int?id)
        {
            #region ViewBags
            ViewBag.Cities     = _context.Cities;
            ViewBag.Marks      = _context.Marks;
            ViewBag.Fuels      = _context.Fuels;
            ViewBag.Bans       = _context.Bans;
            ViewBag.Colors     = _context.Colors;
            ViewBag.SpeedBoxes = _context.SpeedBoxes;
            #endregion

            CarDetailsVM model = new CarDetailsVM()
            {
                automobile = _context.Automobiles.Find(id),
                carImages  = _context.CarImages.Where(x => x.AutomobileId == id)
            };

            if (!ModelState.IsValid)
            {
                #region ViewBags
                ViewBag.Cities     = _context.Cities;
                ViewBag.Marks      = _context.Marks;
                ViewBag.Fuels      = _context.Fuels;
                ViewBag.Bans       = _context.Bans;
                ViewBag.Colors     = _context.Colors;
                ViewBag.SpeedBoxes = _context.SpeedBoxes;
                #endregion

                ModelState.AddModelError("", "Xahiw olunur duzgun deyerler daxil edesiniz");
                return(View(model));
            }

            Automobile      automobileFromDb = _context.Automobiles.Find(id);
            ApplicationUser user             = await _userManager.FindByNameAsync(User.Identity.Name);

            automobile.ApplicationUserId = user.Id;

            if (user == null || automobileFromDb.ApplicationUserId != user.Id)
            {
                #region ViewBags
                ViewBag.Cities     = _context.Cities;
                ViewBag.Marks      = _context.Marks;
                ViewBag.Fuels      = _context.Fuels;
                ViewBag.Bans       = _context.Bans;
                ViewBag.Colors     = _context.Colors;
                ViewBag.SpeedBoxes = _context.SpeedBoxes;
                #endregion

                ModelState.AddModelError("", "Siz active istifadeci deyilsiniz");
                return(View(model));
            }

            if (id == null)
            {
                return(NotFound());
            }

            if (automobileFromDb == null)
            {
                return(NotFound());
            }

            if (automobile.Photo == null)
            {
                #region ViewBags
                ViewBag.Cities     = _context.Cities;
                ViewBag.Marks      = _context.Marks;
                ViewBag.Fuels      = _context.Fuels;
                ViewBag.Bans       = _context.Bans;
                ViewBag.Colors     = _context.Colors;
                ViewBag.SpeedBoxes = _context.SpeedBoxes;
                #endregion
                ModelState.AddModelError("Image", "Please input Image");
                return(View(automobile));
            }

            foreach (var item in automobile.AllCarImages)
            {
                if (item == null)
                {
                    #region ViewBags
                    ViewBag.Cities     = _context.Cities;
                    ViewBag.Marks      = _context.Marks;
                    ViewBag.Fuels      = _context.Fuels;
                    ViewBag.Bans       = _context.Bans;
                    ViewBag.Colors     = _context.Colors;
                    ViewBag.SpeedBoxes = _context.SpeedBoxes;
                    #endregion
                    ModelState.AddModelError("Image", "Please input Image");
                    return(View(automobile));
                }
            }

            if (automobile.Photo.ContentType.Contains("image/"))
            {
                string folderPath = Path.Combine(_env.WebRootPath, "img");
                string fileName   = Guid.NewGuid().ToString() + "_" + automobile.Photo.FileName;
                string filePath   = Path.Combine(folderPath, fileName);
                automobileFromDb.PhotoUrl = fileName;

                using (FileStream fileStream = new FileStream(filePath, FileMode.Create))
                {
                    await automobile.Photo.CopyToAsync(fileStream);
                }

                string currentFilePath = Path.Combine(_env.WebRootPath, "image", automobileFromDb.PhotoUrl);
                if (System.IO.File.Exists(currentFilePath))
                {
                    System.IO.File.Delete(currentFilePath);
                }
            }

            foreach (var p in automobile.AllCarImages)
            {
                if (p.ContentType.Contains("image/"))
                {
                    string folderPathAll = Path.Combine(_env.WebRootPath, "img");
                    string fileNameAll   = Guid.NewGuid().ToString() + "_" + p.FileName;
                    string filePathALL   = Path.Combine(folderPathAll, fileNameAll);

                    using (FileStream fileStreama = new FileStream(filePathALL, FileMode.Create))
                    {
                        await p.CopyToAsync(fileStreama);
                    }

                    CarImages img = new CarImages()
                    {
                        AutomobileId = automobileFromDb.Id,
                        CarPhotoUrl  = fileNameAll
                    };
                    _context.CarImages.Add(img);
                }
            }

            automobileFromDb.ApplicationUserId = user.Id;
            automobileFromDb.Name           = automobile.Name;
            automobileFromDb.Price          = automobile.Price;
            automobileFromDb.GraduationYear = automobile.GraduationYear;
            automobileFromDb.EnginePower    = automobile.EnginePower;
            automobileFromDb.EngineVolume   = automobile.EngineVolume;
            automobileFromDb.Mileage        = automobile.Mileage;
            automobileFromDb.BanId          = automobile.BanId;
            automobileFromDb.CityId         = automobile.CityId;
            automobileFromDb.ColorId        = automobile.ColorId;
            automobileFromDb.FuelId         = automobile.FuelId;
            automobileFromDb.ModelId        = automobile.ModelId;
            automobileFromDb.SpeedBoxId     = automobile.SpeedBoxId;
            _context.SaveChanges();
            return(RedirectToAction("UserProfile", "Account"));
        }