public void UpdateUser(ApplicationUser oldUser, ApplicationUser newUser, UserMutationDto userProfile, string?oldPassword, string?newPassword) { oldUser.Email = newUser.Email; oldUser.UserName = newUser.UserName; IdentityResult result = _userManager.UpdateAsync(oldUser).Result; if (result.Succeeded) { if (oldPassword != null && newPassword != null) { var passChangeRes = _userManager.ChangePasswordAsync(oldUser, oldPassword, newPassword).Result; if (passChangeRes.Succeeded) { var userProfiles = _profileService.GetUserDetails().Result; var profId = userProfiles.Find(u => u.Username == oldUser.UserName).UserDetailsID; _profileService.UpdateUserDetails(userProfile, profId); } } else { var userProfiles = _profileService.GetUserDetails().Result; var profId = userProfiles.Find(u => u.Username == oldUser.UserName).UserDetailsID; _profileService.UpdateUserDetails(userProfile, profId); } } else { throw new Exception(result.Errors.ToList()[0].Description); } }
public async Task InsertUserDetails(UserMutationDto userDetails) { //HttpContent postContent = new StringContent(content: JsonConvert.SerializeObject(userDetails), encoding: Encoding.UTF8, mediaType: "application/json"); HttpResponseMessage res = await _profileService.PostAsJsonAsync <UserMutationDto>(API + USER_DETAILS, userDetails); if (!res.IsSuccessStatusCode) { throw new Exception(res.Content.ToString()); } }
public async Task UpdateUserDetails(UserMutationDto newUserDetails, Guid id) { HttpContent putContent = new StringContent(JsonConvert.SerializeObject(newUserDetails)); HttpResponseMessage res = await _profileService.PutAsJsonAsync(API + USER_DETAILS + id, putContent); if (!res.IsSuccessStatusCode) { throw new Exception(res.Content.ToString()); } }
public ApplicationUser CreatePersonalUser(ApplicationUser user, UserMutationDto personalUserProfile, string password) { user.Id = Guid.NewGuid(); IdentityResult result = _userManager.CreateAsync(user, password).Result; if (result.Succeeded) { _userManager.AddToRoleAsync(user, "RegularUser").Wait(); personalUserProfile.Username = user.UserName; _profileService.InsertUserDetails(personalUserProfile).Wait(); return(user); } else { throw new Exception(result.Errors.ToList()[0].Description); } }
public async Task <ActionResult <UserDetails> > PostUserDetails(UserMutationDto userDetails) { if (_cityService.GetCityById(userDetails.CityId) == null) { return(BadRequest("City with given id doesnt exist")); } try { var userDetailsForCreation = _mapper.Map <UserDetails>(userDetails); _userDetailsService.InsertUserDetails(userDetailsForCreation); } catch (Exception) { return(StatusCode(StatusCodes.Status500InternalServerError, "Error on the server")); } return(CreatedAtAction("GetUserDetails", userDetails)); }
public async Task <IActionResult> PutUserDetails(Guid id, UserMutationDto newUserDetailsDto) { try { var oldUserDetails = _userDetailsService.GetUserDetailsById(id); if (oldUserDetails == null) { return(BadRequest("UserDetails with given id doesnt exist")); } if (_cityService.GetCityById(newUserDetailsDto.CityId) == null) { return(BadRequest("City with given id doesnt exist")); } var newUserDetails = _mapper.Map <UserDetails>(newUserDetailsDto); _userDetailsService.UpdateUserDetails(oldUserDetails, newUserDetails); } catch (Exception) { return(StatusCode(StatusCodes.Status500InternalServerError, "Error on the server")); } return(NoContent()); }