private async Task <ActionResult <UserResponse> > UpdateUserAsync(int id, UserRequest userUpdate) { var db_user = await _context.Users.AsNoTracking().Where(u => u.Id == id).FirstOrDefaultAsync(); if (db_user is null) { return(NotFound()); } var db_password = await _context.Passwords.AsNoTracking().Where(p => p.UserId == db_user.Id).OrderBy(p => p.TimeStamp).LastAsync(); if (db_password.Password != Sha256Generator.ComputeString(userUpdate.Password)) { var new_password = new PasswordDbModel() { Password = Sha256Generator.ComputeString(userUpdate.Password), TimeStamp = DateTime.Now, UserId = id }; await _context.Passwords.AddAsync(new_password); } var updatedUser = _mapper.Map <UserDbModel>(userUpdate); updatedUser.Id = id; _context.Users.Update(updatedUser); await _context.SaveChangesAsync(); return(_mapper.Map <UserResponse>(updatedUser)); }
public void CreateHash_ResultShouldNotBeEqualToInputText(string inputText) { var hashedText = Sha256Generator.ComputeString(inputText); Assert.That(hashedText, Is.Not.EqualTo(inputText)); Assert.That(hashedText.Length, Is.EqualTo(64)); }
private async Task <ActionResult <UserResponse> > CreateNewUserAsync(UserRequest user) { if (await _context.Users.Where(u => u.Email.ToLower() == user.Email.ToLower()).SingleOrDefaultAsync() is not null) { return(Conflict("Email address already in use")); } var db_user = _mapper.Map <UserDbModel>(user); var db_password = new PasswordDbModel() { Password = Sha256Generator.ComputeString(user.Password), TimeStamp = DateTime.Now, User = db_user }; await _context.Users.AddAsync(db_user); await _context.Passwords.AddAsync(db_password); await _context.SaveChangesAsync(); return(_mapper.Map <UserResponse>(db_user)); }