I think my code is very polluted with a lot of if.
[ValidateModelAttribute]
[HttpPut("{id}")]
public IActionResult Put(int id, [FromBody]UserDto model)
{
try
{
if (model == null || model.Id != id)
return BadRequest();
var originalUser = context.User.AsNoTracking().FirstOrDefault(x => x.Id == id);
if (originalUser == null)
return NotFound();
var updatedUser = model.MapTo<User>();
var userBd = context.User.FirstOrDefault(x => x.Login == updatedUser.Login && x.Id != updatedUser.Id);
if (userBd != null)
return StatusCode((int)HttpStatusCode.Conflict, userBd);
context.Entry(originalUser).Context.Update(updatedUser);
context.SaveChanges();
return Ok(updatedUser);
}
catch (Exception ex)
{
return BadRequest(ex.Message);
}
}
Is there a more elegant way?