public IActionResult Contributor(CreateHubViewModel model) { var generatePassCode = new GeneratePasscode(); var generateEmail = new GenerateCustomEmail(); if (ModelState.IsValid) { Hubb newHub = new Hubb() { HubName = model.HubName, Address = model.Address, State = model.State, Location = "test", Status = HubStatus.Suggested, //Lga = model.Lga, Passcode = generatePassCode.PassCode(), CustomMail = generateEmail.CustomMail(model.HubName) }; _hubdataStore.Post(newHub); return(RedirectToAction("Index", "Home")); } return(View()); }
public async Task <IActionResult> ContributorTwo(CreateHubViewModel model) { var generatePassCode = new GeneratePasscode(); var generateEmail = new GenerateCustomEmail(); var passcode = generatePassCode.PassCode(); var email = generateEmail.CustomMail(model.HubName); try { if (ModelState.IsValid) { //add hub name, customEmail and passcode to AspNetUsers Table var user = new ApplicationUser { FullName = model.HubName, UserName = email, Email = email, EmailConfirmed = true }; var result = await _userManager.CreateAsync(user, passcode); var ownerId = _userManager.GetUserId(User); //if result is successful create owner role for new hub added if (result.Succeeded) { var roleExists = await _roleManager.RoleExistsAsync("Owner"); IdentityResult roleResult; if (roleExists) { roleResult = await _userManager.AddToRoleAsync(user, "Owner"); } else { await _roleManager.CreateAsync(new IdentityRole("Owner")); roleResult = await _userManager.AddToRoleAsync(user, "Owner"); } if (roleResult.Succeeded) { var userid = Guid.Parse(user.Id); // var ownerDetails = _ownerService.GetOwnerByEmail(User.Identity.Name); //Add Hub to Hubbs Table var newHub = new Hubb() { HubName = model.HubName, Address = model.Address, State = model.State, City = model.City, Location = "test", Status = HubStatus.Suggested, Email = email, Passcode = passcode, CustomMail = email, OwnerId = userid }; //Add HubName and custom email to Owners Table // {helps when claimer wants to update profile} var owner = new Owner() { OwnerId = userid, FullName = model.HubName, Email = email, }; var saveResult = _ownerdataStore.Post(owner); await _signInManager.SignInAsync(user, isPersistent : false); if (saveResult == null) { await _userManager.RemoveFromRoleAsync(user, "Owner"); await _userManager.DeleteAsync(user); } _hubdataStore.Post(newHub); return(RedirectToAction("landing", "home")); } else { foreach (var item in roleResult.Errors) { ModelState.AddModelError("", item.Description); } } } foreach (var error in result.Errors) { ModelState.AddModelError(string.Empty, error.Description); } } } catch (Exception error) { // throw error; return(View(error)); } return(View(model)); }