public async Task Update(int id, UpdateConsumerDto updateConsumerData) { if (updateConsumerData == null) { throw new ArgumentNullException(nameof(updateConsumerData)); } var consumerToUpdate = await _repository.GetById(id); if (consumerToUpdate == null) { throw new InvalidOperationException(); } if (updateConsumerData.Email != consumerToUpdate.Email) { // email has changed so check if the new email is already taken if (_repository.FindByEmail(updateConsumerData.Email).Result != null) { throw new Exception("Email " + updateConsumerData.Email + " is already taken"); } } _mapper.Map(updateConsumerData, consumerToUpdate); await _repository.Update(consumerToUpdate); await _userService.Update(id, updateConsumerData); }
public async Task Update(int id, UpdateConsumerDto updateConsumerDto) { var userToUpdate = await _userManager.Users.FirstOrDefaultAsync(x => x.ConsumerId == id); if (userToUpdate == null) { throw new InvalidOperationException(); } var currentRole = await _userManager.GetRolesAsync(userToUpdate); // Roles that the user has currently if (!await _userManager.IsInRoleAsync(userToUpdate, updateConsumerDto.Role)) { await _userManager.AddToRoleAsync(userToUpdate, updateConsumerDto.Role); await _userManager.RemoveFromRolesAsync(userToUpdate, currentRole); } }
public async Task <IActionResult> Put(int id, [FromBody] UpdateConsumerDto updateConsumerDto) { await _consumersService.Update(id, updateConsumerDto); return(NoContent()); }