public async Task <bool> Update(int id, League item) { if (item == null) { return(false); } item.Id = id; db.Entry(item).State = EntityState.Modified; await db.SaveChangesAsync(); return(true); }
// Update a User from the database (the financial adviser is retrieved automatically) public async Task <bool> Update(string id, UserModel item) { if (item == null) { return(false); } // we get the user in the database and change the name / password (if not null) IdentityUser editedUser = db.Users.Find(id); if (editedUser != null) { if (!String.IsNullOrEmpty(item.Name)) { editedUser.UserName = item.Name; } // if the password is blank, we reuse the previous one if (!String.IsNullOrEmpty(item.Password)) { editedUser.PasswordHash = new PasswordHasher().HashPassword(item.Password); editedUser.SecurityStamp = Guid.NewGuid().ToString(); } // Managing the admin role of the user if (item.AdministratorRole) { // if the item has the administrator role at true, we add the role to the user if he doesn't have it if (!await this.userManager.IsInRoleAsync(id, AuthenticationRoles.AdministratorRole)) { await this.userManager.AddToRoleAsync(id, AuthenticationRoles.AdministratorRole); } } else { // if the item has the administrator role at false, we remove the role to the user if he has it if (await this.userManager.IsInRoleAsync(id, AuthenticationRoles.AdministratorRole)) { userManager.RemoveFromRole(id, AuthenticationRoles.AdministratorRole); } } db.Entry(editedUser).State = EntityState.Modified; await db.SaveChangesAsync(); return(true); } else { return(false); } }