Exemplo n.º 1
0
        public async Task <IActionResult> Index(IndexViewModel model)
        {
            SetListData();

            if (!ModelState.IsValid)
            {
                return(View(model));
            }

            var user = await _userManager.GetUserAsync(User);

            if (user == null)
            {
                throw new ApplicationException($"Unable to load user with ID '{_userManager.GetUserId(User)}'.");
            }

            var email = user.Email;

            if (model.Email != email)
            {
                var setEmailResult = await _userManager.SetEmailAsync(user, model.Email);

                if (!setEmailResult.Succeeded)
                {
                    throw new ApplicationException($"Unexpected error occurred setting email for user with ID '{user.Id}'.");
                }
            }
            var Passenger = _takeAFlightContext.Passengers.Include(u => u.User).FirstOrDefault(obj => obj.ApplicationUserID == user.Id);

            if (Passenger != null)
            {
                model.Passenger.ApplicationUserID = Passenger.ApplicationUserID;
                model.Passenger.User = Passenger.User;
                model.Passenger.ID   = Passenger.ID;
                _takeAFlightContext.Entry(Passenger).State = EntityState.Detached;
                _takeAFlightContext.Update(model.Passenger);
                await _takeAFlightContext.SaveChangesAsync();
            }
            else
            {
                StatusMessage = "Error: Cannot find passenger data... pls try again later ";
                return(RedirectToAction(nameof(Index)));
            }

            //var phoneNumber = user.PhoneNumber;
            //if (model.PhoneNumber != phoneNumber)
            //{
            //	var setPhoneResult = await _userManager.SetPhoneNumberAsync(user, model.PhoneNumber);
            //	if (!setPhoneResult.Succeeded)
            //	{
            //		throw new ApplicationException($"Unexpected error occurred setting phone number for user with ID '{user.Id}'.");
            //	}
            //}

            StatusMessage = "Your profile has been updated";
            return(RedirectToAction(nameof(Index)));
        }
Exemplo n.º 2
0
        public async Task <IActionResult> Edit(int id, [Bind("FlightID,DestinationID,Price,Duration,Departure")] Flight flight)
        {
            if (id != flight.FlightID)
            {
                return(RedirectToAction("Error", "Error"));
            }

            if (ModelState.IsValid)
            {
                try
                {
                    _context.Update(flight);
                    await _context.SaveChangesAsync();
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!FlightExists(flight.FlightID))
                    {
                        return(RedirectToAction("Error", "Error"));
                    }
                    else
                    {
                        throw;
                    }
                }
                return(RedirectToAction(nameof(Index)));
            }
            ViewData["DestinationID"] = new SelectList(_context.Set <Destination>(), "DestinationID", "DestinationID", flight.DestinationID);
            return(View(flight));
        }