public async Task <IActionResult> Manage() { var userId = Guid.Parse(HttpContext.User.FindFirst(claim => claim.Type == Auth.Claim.UserId).Value); var userRepo = (Repository.IUser) this._services.GetService(typeof(Repository.IUser)); var locationRepo = (Repository.ILocation) this._services.GetService(typeof(Repository.ILocation)); var defaultLocation = await userRepo.GetDefaultLocation(userId); var allLocations = locationRepo.GetLocations(); var model = new Model.Input.AccountManagement(); model.StorePicked = defaultLocation.LocationId.ToString(); foreach (var loc in allLocations) { model.Stores.Add(new SelectListItem { Value = loc.LocationId.ToString(), Text = loc.Name }); } var user = await userRepo.GetUserById(userId); model.FirstName = user.FirstName; model.LastName = user.LastName; var userAddress = await userRepo.GetAddressByUserId(userId); if (userAddress != null) { model.AddressLine1 = userAddress.Line1 != null ? userAddress.Line1.Data : null; model.AddressLine2 = userAddress.Line2 != null ? userAddress.Line2.Data : null; model.City = userAddress.City != null ? userAddress.City.Name : null; model.StatePicked = userAddress.State != null ? userAddress.State.Name : null; model.Zip = userAddress.Zip != null ? userAddress.Zip.Zip : null; } return(View("Manage", model)); }
public async Task <IActionResult> UpdateAccountInfo(Model.Input.AccountManagement model) { if (!ModelState.IsValid) { this.SetFlashError("There was an error submitting your information. Please try again."); return(RedirectToAction("Manage")); } Guid defaultLocationId; if (!Guid.TryParse(model.StorePicked, out defaultLocationId)) { this.SetFlashError("Unable to find the selected store. Please choose another store."); return(RedirectToAction("Manage")); } var userId = Guid.Parse(HttpContext.User.FindFirst(claim => claim.Type == Auth.Claim.UserId).Value); var userRepo = (Repository.IUser) this._services.GetService(typeof(Repository.IUser)); var locationRepo = (Repository.ILocation) this._services.GetService(typeof(Repository.ILocation)); var user = await userRepo.GetUserById(userId); var location = await locationRepo.GetById(defaultLocationId); userRepo.SetDefaultLocation(user, location); var businessRules = (Business.IBusinessRules) this._services.GetService(typeof(Business.IBusinessRules)); if (!String.IsNullOrEmpty(model.FirstName)) { if (!businessRules.ValidateUserName(model.FirstName.Trim())) { this.SetFlashError("Names may not contain numbers or special characters."); return(RedirectToAction("Manage")); } } if (!String.IsNullOrEmpty(model.LastName)) { if (!businessRules.ValidateUserName(model.LastName.Trim())) { this.SetFlashError("Names may not contain numbers or special characters."); return(RedirectToAction("Manage")); } } var updateOk = await userRepo.UpdateUserPersonalInfo(user.UserId, model); if (!updateOk) { this.SetFlashError("There was an error saving your information. Please try again."); return(RedirectToAction("Manage")); } var allLocations = locationRepo.GetLocations(); foreach (var loc in allLocations) { model.Stores.Add(new SelectListItem { Value = loc.LocationId.ToString(), Text = loc.Name }); } this.SetFlashInfo("Your information has been successfully updated."); return(RedirectToAction("Manage")); }
public IActionResult RedirectUpdateAccountInfo(Model.Input.AccountManagement model) { return(RedirectToAction("Manage")); }