Exemplo n.º 1
0
        public async Task <IActionResult> Add(CarViewModel carViewModel)
        {
            if (!ModelState.IsValid)
            {
                return(View(carViewModel));
            }

            var user = await _userManager.GetUserAsync(User);

            if (user == null)
            {
                ModelState.AddModelError("", "Błąd użytkownika");
                return(View(carViewModel));
            }

            var car = carViewModel.GetCar();

            car.User          = user;
            car.ActualMileage = car.StartMileage;

            var result = await _carRepository.Add(car);

            if (!result)
            {
                ModelState.AddModelError("", "Błąd dodawania pojazdu");
                return(View(carViewModel));
            }

            return(RedirectToAction("index"));
        }