public async Task <ActionResult> UpdateUser(User model) { try { var roleRepo = new RoleRepository(_context); var user = new UserRepository(_context).Get(model.UserName); var role = roleRepo.GetByName(model.Role); if (user == null) { return(NotFound("Updating user not found. Please update an existing user")); } user.Name = model.Name; user.UpdatedAt = DateTime.Now.ToUniversalTime(); user.PhoneNumber = model.PhoneNumber; user.Email = model.Email; //user.Locked = false; new UserRepository(_context).Update(user); //Remove old role roleRepo.RemoveFromAllRoles(user.Id); //Add to role roleRepo.AddToRole(user.Id, role.Name); return(Created("UpdateUser", new { user.Id, Message = "User has been updated successfully" })); } catch (Exception ex) { return(BadRequest(WebHelpers.ProcessException(ex))); } }
public async Task <ActionResult> CreateUser(User model) { try { var roleRepo = new RoleRepository(_context); var role = roleRepo.GetByName(model.Role); //todo: do validations model.Id = Guid.NewGuid().ToString(); model.Locked = false; var result = await userManager.CreateAsync(model, model.Password).ConfigureAwait(true); if (result.Succeeded) { var user = userManager.FindByNameAsync(model.UserName).Result; //Add to role roleRepo.AddToRole(user.Id, role.Name); //var rslt = userManager.AddToRoleAsync(user, model.Role); } else { return(BadRequest(WebHelpers.ProcessException(result))); } return(Created("CreateUser", new { model.Id, Message = "User has been created Successfully" })); } catch (Exception ex) { return(BadRequest(WebHelpers.ProcessException(ex))); } }