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))); }
public async Task <IActionResult> Create([Bind("FlightID,DestinationID,Price,Duration,Departure")] Flight flight) { if (ModelState.IsValid) { _context.Add(flight); await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } ViewData["DestinationID"] = new SelectList(_context.Set <Destination>(), "DestinationID", "DestinationID", flight.DestinationID); return(View(flight)); }
public async Task <IActionResult> Register(RegisterViewModel model, string returnUrl = null) { try { ViewData["ReturnUrl"] = returnUrl; SetRegisterListData(); bool isAlreadyExistPassenger = _DbContext.Passengers.FirstOrDefault(obj => obj.IdPassenger == model.Passenger.IdPassenger) != null; if (ModelState.IsValid && !isAlreadyExistPassenger) { var user = new ApplicationUser { UserName = model.Email, Email = model.Email }; var result = await _userManager.CreateAsync(user, model.Password); if (result.Succeeded) { _logger.LogInformation("User created a new account with password."); var code = await _userManager.GenerateEmailConfirmationTokenAsync(user); var callbackUrl = Url.EmailConfirmationLink(user.Id, code, Request.Scheme); await _emailSender.SendEmailConfirmationAsync(model.Email, callbackUrl); await _signInManager.SignInAsync(user, isPersistent : false); _logger.LogInformation("User created a new account with password."); #region Creating passenger and Attach him the new user data var passenger = model.Passenger; passenger.User = user; passenger.ApplicationUserID = user.Id; _DbContext.Passengers.Add(passenger); await _DbContext.SaveChangesAsync(); #endregion return(RedirectToLocal(returnUrl)); } AddErrors(result); } else if (isAlreadyExistPassenger) { ModelState.AddModelError(string.Empty, "Passenger is already exist"); } } catch { ModelState.AddModelError(string.Empty, "Opps... something went wrong... pls try again later!"); } // If we got this far, something failed, redisplay form return(View(model)); }
//Creating Admin Role to handle admin options like edit and delete a flight for example. public static async Task CreateRolesandUsers(RoleManager <IdentityRole> roleManager, UserManager <ApplicationUser> userManager, TakeAFlightContext takeAFlight) { bool isRoleExist = await roleManager.RoleExistsAsync("Admin"); if (!isRoleExist) { // first we create Admin rool var role = new IdentityRole(); role.Name = "Admin"; await roleManager.CreateAsync(role); //Here we create a Admin super user who will maintain the website var user = new ApplicationUser(); user.UserName = "******"; user.Email = "*****@*****.**"; string userPWD = "Admin1234!"; IdentityResult chkUser = await userManager.CreateAsync(user, userPWD); //Add default User to Role Admin if (chkUser.Succeeded) { var result1 = await userManager.AddToRoleAsync(user, "Admin"); } var passenger = new Passenger { FirstName = "roei", ApplicationUserID = user.Id, DateOfBirth = new DateTime(), Gender = Sex.Man, IdPassenger = 123456789, LastName = "bbb", Nationality = Nationality.Europe, User = user }; await takeAFlight.AddAsync(passenger); await takeAFlight.SaveChangesAsync(); } }
public async Task <IActionResult> Order(int[] flight_id, int[] quantity, string[] dest_details, string[] departure) { var tupleList = new List <Tuple <string, string, int> > { }; for (int i = 0; i < flight_id.Length; i++) { if (!FlightExists(flight_id[i])) { ViewData["Message"] = tupleList; return(View()); } } var user = _Userscontext.Users.FirstOrDefault(obj => obj.UserName == User.Identity.Name); var passanger = _context.Passengers.FirstOrDefault(obj => obj.ApplicationUserID == user.Id); for (int i = 0; i < flight_id.Length; i++) { if (quantity[i] <= 0) { continue; } FlightOrder fo = new FlightOrder { FlightID = flight_id[i], PassengerID = passanger.ID, Quantity = quantity[i] }; _context.FlightOrders.Add(fo); tupleList.Add(Tuple.Create(dest_details[i], departure[i], quantity[i])); } await _context.SaveChangesAsync(); ViewData["Message"] = tupleList; return(View()); }