public async Task <IActionResult> Index(AddJokeViewModel theJoke) { var userId = _userManager.GetUserId(HttpContext.User); var thisUser = _repo.GetUser(userId); var userStats = new UserStatsViewModel(); if (thisUser != null) { userStats.UserName = thisUser.UserName; userStats.Jokes = thisUser.Jokes; userStats.Ratings = thisUser.Ratings; } else { userStats.UserName = ""; } if (ModelState.IsValid) { var jokeToAdd = Mapper.Map <Joke>(theJoke); jokeToAdd.DateCreated = DateTime.Now; jokeToAdd.Uploader = _userManager.GetUserName(HttpContext.User); _repo.AddJoke(jokeToAdd); if (await _repo.Save()) { return(RedirectToAction("Joke", new { id = jokeToAdd.Id })); } } return(View(userStats)); }
public async Task <IActionResult> Edit(ProfileViewModel profile) { ViewBag.Title = "Profile"; var user = await _userManager.GetUserAsync(HttpContext.User); ViewBag.User = user.UserName; var profileVM = Mapper.Map <ProfileViewModel>(user); if (!ModelState.IsValid) { } else if (!Regex.IsMatch(profile.ZipCode, @"^\d+$") || Int32.Parse(profile.ZipCode) < 0 || Int32.Parse(profile.ZipCode) > 99999) { ModelState.AddModelError("ZipCode", "Please enter a valid zip code."); } else { user.FirstName = profile.FirstName; user.LastName = profile.LastName; user.Email = profile.Email; user.City = profile.City; user.State = profile.State; user.ZipCode = profile.ZipCode; _repo.UpdateUser(user); if (await _repo.Save()) { return(RedirectToAction("Index")); } else { ModelState.AddModelError("", "There was an error updating your profile. Please try again."); } } return(View(profileVM)); }