public async Task <IActionResult> Create([Bind("CoachProfileId,CoachId,Education,Interests,Experience,Skills,Biography,ImageFile")] CoachProfile coachProfile) { if (ModelState.IsValid) { // upload image to wwwroot/image string wwwRootPath = _hostEnvironment.WebRootPath; string fileName = Path.GetFileNameWithoutExtension(coachProfile.ImageFile.FileName); string extension = Path.GetExtension(coachProfile.ImageFile.FileName); coachProfile.ImagePath = fileName = fileName + DateTime.Now.ToString("yymmssfff") + extension; string path = Path.Combine(wwwRootPath + "/image/", fileName); using (var fileStream = new FileStream(path, FileMode.Create)) { await coachProfile.ImageFile.CopyToAsync(fileStream); } // assign the coach id of the coach currently logged in to the new coach profile coachProfile.CoachId = _userManager.GetUserId(User); _context.Add(coachProfile); await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Details))); } return(View(coachProfile)); }
public async Task <IActionResult> Details(int?id) { CoachProfile coachProfile = new CoachProfile(); if (User.IsInRole("Coach")) { // show the profile of the coach that currently logged in var CoachId = _userManager.GetUserId(User); coachProfile = await _context.CoachProfiles .Include(c => c.Coach) .FirstOrDefaultAsync(m => m.CoachId == CoachId); // redirect to no profile page if the coach has not created a profile yet if (coachProfile == null) { return(RedirectToAction("NoProfile")); } } // if the current usr is not a coach, find the profile using id that is passed in this function else { coachProfile = await _context.CoachProfiles .Include(c => c.Coach) .FirstOrDefaultAsync(m => m.CoachProfileId == id); } return(View(coachProfile)); }
// GET: CoachProfile/Details/5 public async Task <IActionResult> Details(int?id) { CoachProfile coachProfile = new CoachProfile(); if (User.IsInRole("Coach")) { // show the profile of the coach that currently logged in var CoachId = _userManager.GetUserId(User); coachProfile = await _context.CoachProfiles .Include(c => c.Coach) .FirstOrDefaultAsync(m => m.CoachId == CoachId); // redirect to no profile page if the coach has not created a profile yet if (coachProfile == null) { return(RedirectToAction("NoProfile")); } } // if the current usr is a member, find the profile by ID passed in as a parameter else if (User.IsInRole("Member")) { coachProfile = await _context.CoachProfiles .Include(c => c.Coach) .FirstOrDefaultAsync(m => m.CoachProfileId == id); //Console.WriteLine("id type: " + id.GetType().Name) //if(_userManager.FindByIdAsync(id).Result != null) //{ // var CoachId = id; // coachProfile = await _context.CoachProfiles // .Include(c => c.Coach) // .FirstOrDefaultAsync(m => m.CoachId == CoachId); //} //else //{ // coachProfile = await _context.CoachProfiles // .Include(c => c.Coach) // .FirstOrDefaultAsync(m => m.CoachProfileId == id); //} } return(View(coachProfile)); }
public async Task <IActionResult> Edit(int id, [Bind("CoachProfileId,CoachId,Education,Interests,Experience,Skills,Biography,ImageFile")] CoachProfile coachProfile) { coachProfile.CoachId = _userManager.GetUserId(User); if (id != coachProfile.CoachProfileId) { return(NotFound()); } // upload image to wwwroot/image string wwwRootPath = _hostEnvironment.WebRootPath; string fileName = Path.GetFileNameWithoutExtension(coachProfile.ImageFile.FileName); string extension = Path.GetExtension(coachProfile.ImageFile.FileName); coachProfile.ImagePath = fileName = fileName + DateTime.Now.ToString("yymmssfff") + extension; string path = Path.Combine(wwwRootPath + "/image/", fileName); using (var fileStream = new FileStream(path, FileMode.Create)) { await coachProfile.ImageFile.CopyToAsync(fileStream); } if (ModelState.IsValid) { try { _context.Update(coachProfile); await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!CoachProfileExists(coachProfile.CoachProfileId)) { return(NotFound()); } else { throw; } } return(RedirectToAction(nameof(Details))); } return(View(coachProfile)); }
public IActionResult CoachProfile(string id) { if (id == null) { return(NotFound()); } // get the coach profile id based on the coach id passed in the function CoachProfile coachProfile = _context.CoachProfiles.Where(p => p.CoachId == id).FirstOrDefault(); if (coachProfile == null) { return(RedirectToAction("NoProfile", "CoachProfile")); } var profileId = coachProfile.CoachProfileId; // redirect to the profile page using the profile id return(Redirect("../../CoachProfile/Details/" + profileId)); }
public async Task <IActionResult> Register(RegisterViewModel model, string returnUrl = null) { ViewData["ReturnUrl"] = returnUrl; if (ModelState.IsValid) { string email = model.Email; var userName = email.Substring(0, email.IndexOf('@')); var user = new ApplicationUser { UserName = model.Email, Email = model.Email }; // initialize the new Application User entity var result = await _userManager.CreateAsync(user, model.Password); // confirm new Application User was created successfully if (result.Succeeded) // create the necessary athlete/ coach profile, stats, and assign the User to its Role { var roleModel = _roleManager.Roles.SingleOrDefault(r => r.Id == model.RoleId); switch (roleModel.Name) { case "Athlete": var athleteProfile = new AthleteProfile { UserId = user.Id }; // initialize a new Athlete Profile entity var athleteStats = new AthleteStats { UserId = user.Id, DateOFEntry = DateTime.Now }; // initialize a new Athlete Stats entity and add to Athlete role _context.AthleteProfiles.Add(athleteProfile); _context.AthleteStats.Add(athleteStats); await _userManager.AddToRoleAsync(user, roleModel.Name); break; case "Coach": var coachProfile = new CoachProfile { UserId = user.Id }; // initialize a new Coach Profile entity and add to Coach role _context.CoachProfiles.Add(coachProfile); await _userManager.AddToRoleAsync(user, roleModel.Name); break; default: break; } await _userManager.SetUserNameAsync(user, userName); // set the Username within the User.Identity await _userManager.UpdateAsync(user); _context.SaveChanges(); _logger.LogInformation("User created a new account with password."); _logger.LogInformation("Profile created"); 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."); return(RedirectToLocal(returnUrl)); } AddErrors(result); } // If we got this far, something failed, redisplay form return(View(model)); }