public async Task UpdateUserAsync(UpdateApplicationUserDTO updateUser) { var uri = API.User.UpdateUser(_remoteServiceBaseUrl); var updateUserContent = new StringContent(JsonConvert.SerializeObject(updateUser), System.Text.Encoding.UTF8, "application/json"); var response = await _httpClient.PutAsync(uri, updateUserContent); response.TrySaveModelStateErrors(ref modelStateErrors); }
public async Task <IActionResult> PutAsync(int id, [FromBody] UpdateApplicationUserDTO model) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } var applicationUser = await _applicationUserService.GetApplicationUserAsync(id); if (applicationUser != null) { applicationUser = _mapper.Map <UpdateApplicationUserDTO, ApplicationUser> (model, applicationUser); await _applicationUserService.UpdateApplicationUserAsync(applicationUser); return(NoContent()); } return(NotFound()); }
public async Task <IActionResult> UpdateUser([FromServices] IContactService contactService, [FromServices] ApplicationDbContext dbContext, UpdateApplicationUserDTO model) { //var userInDb = await _userManager.FindByIdAsync(model.Id); var userInDb = await dbContext.Users.AsTracking().SingleOrDefaultAsync(u => u.Id == model.Id); if (userInDb == null) { return(BadRequest()); } if (await IsUnique(username: userInDb.UserName == model.UserName ? "" : model.UserName, email: userInDb.Email == model.Email ? "" : model.Email) == false) { return(BadRequest(ModelState)); } int?contactId = null; // CREATING NEW CONTACT FOR THE USER if (model.ContactAssign?.ToLower() == "new") { var id = await contactService.CreateContact(new NewContactDTO() { FirstName = model.NewContactFirstName, LastName = model.NewContactLastName, Email = model.NewContactEmail, Phone = model.NewContactPhone }); if (id < 1) // id invalid,operation didn't succeed { contactService.Validate(ModelState); if (!ModelState.IsValid) // there are model errors for creating contacts { return(BadRequest(ModelState)); } else { return(BadRequest()); } } contactId = id; } if (model.ContactAssign?.ToLower() == "existing") { contactId = model.ExistingContactId; } // UPDATE PROPERTIES userInDb.UserName = model.UserName; userInDb.Email = model.Email; userInDb.FirstName = model.FirstName; userInDb.LastName = model.LastName; userInDb.PhoneNumber = model.PhoneNumber; if (model.Active) { if (userInDb.LockoutEnd.HasValue) { userInDb.LockoutEnd = null; } } else { userInDb.LockoutEnd = DateTime.Now.AddYears(100); } userInDb.ContactId = contactId; try { var res = await dbContext.SaveChangesAsync(); return(NoContent()); } catch (Exception ex) { if (model.ContactAssign.ToLower() == "new") // rollback contact create { await contactService.DeleteContacts(new List <int>() { contactId.Value }); } return(StatusCode((int)HttpStatusCode.InternalServerError)); } }