I've been trying to write Put (update) action without the id property, by using DTO. But every time I'm trying I'm getting the existing object and not the updated one, and I can't figure out why and how to change it so it will work.
My repository:
public User Update(Guid id, User user)
{
var userToUpdate=_context.Users.FirstOrDefault(x => x.Id == id);
_context.Entry(userToUpdate).State = EntityState.Modified;
_context.SaveChanges();
return userToUpdate;
}
My DTO:
public class UserPostDTO
{
public UserPostDTO()
{
}
public UserPostDTO(User user)
{
UserName= user.UserName;
Password= user.Password;
LastLogin= user.LastLogin;
}
[StringLength(255)]
public string UserName { get; set; } = null!;
[StringLength(255)]
public string Password { get; set; } = null!;
[Column(TypeName = "datetime")]
public DateTime? LastLogin { get; set; }
public User ToPostUser()
{
var user = new User();
user.UserName = UserName;
user.Password = Password;
user.LastLogin = LastLogin;
return user;
}
}
My Controller:
public class UserController : ControllerBase
{
private readonly IUserRepository _userRepository;
public UserController(IUserRepository userRepository)
{
_userRepository = userRepository;
}
[HttpPut("{id}")]
public IActionResult Put(Guid id, [FromBody] UserPostDTO user)
{
_userRepository.Update(id, user.ToPostUser());
return Ok();
}
Didn't see you updating the User
object with the new value.
Probably this is what you need:
public User Update(Guid id, User user)
{
var userToUpdate = _context.Users.FirstOrDefault(x => x.Id == id)
.AsNoTracking();
if (userToUpdate == null)
{
// Handle ID is not existed
throw new ArgumentNullException("ID is not existed");
}
user.Id = userToUpdate.Id;
_context.Entry(user).State = EntityState.Modified;
_context.SaveChanges();
return user;
}