Это мой уровень хранилища для обновления:
Код: Выделить всё
public async Task UpdateUserAsync(entities.User entity)
{
_dbContext.Users.Update(entity);
await _dbContext.SaveChangesAsync();
return entity;
}
public async Task GetUserAsync(string userName)
{
var result = await _dbContext.Users
.FirstOrDefaultAsync(x => x.UserName == userName);
return result;
}
Код: Выделить всё
public async Task UpdateUserAsync(UserUpdateRequestDto dto)
{
var user = await _userRepository.GetUserAsync(dto.UserName);
if (user == null)
{
throw new Exception("User not found.");
}
user.Email = dto.Email ?? user.Email;
user.Name = dto.Name ?? user.Name;
user.Family = dto.Family ?? user.Family;
user.Password = dto.Password ?? user.Password;
user.ConfirmPassword = dto.ConfirmPassword ?? user.ConfirmPassword;
user.RoleId = dto.RoleId != 0 ? dto.RoleId : user.RoleId;
await _userRepository.UpdateUserAsync(user);
}
Код: Выделить всё
[HttpPut]
[Route("update-user")]
public async Task UpdateUserAsync(UserUpdateRequestDto dto)
{
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}
try
{
await _userService.UpdateUserAsync(dto);
return Ok("User updated successfully.");
}
catch (Exception ex)
{
return BadRequest(ex.Message);
}
}
Как решить эту проблему?
Подробнее здесь: https://stackoverflow.com/questions/790 ... chitecture