Ejemplo n.º 1
0
        public async Task <IActionResult> Create(Travel newTravel)
        {
            if (!ModelState.IsValid)
            {
                return(RedirectToAction("Create"));
            }

            var currentUser = await _userManager.GetUserAsync(User);

            if (currentUser == null)
            {
                return(Challenge());
            }

            var travelToSave = new Travel()
            {
                Seats         = newTravel.Seats,
                ArrivalCity   = newTravel.ArrivalCity,
                DepartureCity = newTravel.DepartureCity,
                DueAt         = newTravel.DueAt,
                PassengerId   = newTravel.PassengerId,
                Price         = newTravel.Price,
                DriverId      = currentUser.Id
            };


            var successful = await _travelService.AddTravelAsync(travelToSave);

            if (!successful)
            {
                return(BadRequest("Could not add the travel."));
            }

            return(RedirectToAction("Index"));
        }