Adding crud operations for users

This commit is contained in:
2025-06-17 23:08:21 +02:00
parent 8986e3d77e
commit e1a249c07a
9 changed files with 493 additions and 28 deletions

View File

@@ -16,7 +16,10 @@ public interface IUserService
Task<User?> GetUserByUsernameAndPassword(string email, string password);
Task<bool> CheckIfEmailIsValid(string email, string? guid = "");
Task<User?> CreateUserAsync(CreateUserRequestData data, Role role);
Task<User?> UpdateUserAsync(UpdateUserRequestData data, User user);
Task<bool?> DeleteUserAsync(User user);
Task<User?> UpdateUserPasswordAsync(User user, string newPassword);
Task<User?> UpdateUserRoleAsync(User user, Role newRole);
}
public class UserService : BaseService, IUserService
@@ -134,6 +137,31 @@ public class UserService : BaseService, IUserService
return user;
}
public async Task<User?> UpdateUserAsync(UpdateUserRequestData data, User user)
{
using var transaction = await _sqlServerContext.Database.BeginTransactionAsync();
try
{
user.FirstName = data.FirstName ?? user.FirstName;
user.LastName = data.LastName ?? user.LastName;
user.UpdateTime = DateTime.UtcNow;
user.UpdateUserId = this.GetCurrentUserId();
_sqlServerContext.Users.Update(user);
await _sqlServerContext.SaveChangesAsync();
await transaction.CommitAsync();
}
catch (Exception exception)
{
Logger.Error(exception, $"[UserService][UpdateUserAsync] | {transaction.TransactionId}");
await transaction.RollbackAsync();
throw new UpdateException($"An error occurred while updating the user for transaction ID {transaction.TransactionId}.", exception);
}
return user;
}
public async Task<bool?> DeleteUserAsync(User user)
{
bool? deleted = false;
@@ -151,6 +179,59 @@ public class UserService : BaseService, IUserService
return deleted;
}
public async Task<User?> UpdateUserPasswordAsync(User user, string newPassword)
{
using var transaction = await _sqlServerContext.Database.BeginTransactionAsync();
try
{
var salt = _appSettings.EncryptionSettings?.Salt ?? String.Empty;
var pepper = CryptUtils.GeneratePepper();
var iterations = _appSettings.EncryptionSettings?.Iterations ?? 10;
user.PasswordSalt = salt;
user.PasswordPepper = pepper;
user.PasswordIterations = iterations;
user.Password = CryptUtils.GeneratePassword(newPassword, salt, iterations, pepper);
user.UpdateTime = DateTime.UtcNow;
user.UpdateUserId = this.GetCurrentUserId();
_sqlServerContext.Users.Update(user);
await _sqlServerContext.SaveChangesAsync();
await transaction.CommitAsync();
}
catch (Exception exception)
{
Logger.Error(exception, $"[UserService][UpdateUserPasswordAsync] | {transaction.TransactionId}");
await transaction.RollbackAsync();
throw new UpdateException($"An error occurred while updating the user for transaction ID {transaction.TransactionId}.", exception);
}
return user;
}
public async Task<User?> UpdateUserRoleAsync(User user, Role newRole)
{
using var transaction = await _sqlServerContext.Database.BeginTransactionAsync();
try
{
user.Role = newRole;
user.UpdateTime = DateTime.UtcNow;
user.UpdateUserId = this.GetCurrentUserId();
_sqlServerContext.Users.Update(user);
await _sqlServerContext.SaveChangesAsync();
await transaction.CommitAsync();
}
catch (Exception exception)
{
Logger.Error(exception, $"[UserService][UpdateUserRoleAsync] | {transaction.TransactionId}");
await transaction.RollbackAsync();
throw new UpdateException($"An error occurred while updating the user for transaction ID {transaction.TransactionId}.", exception);
}
return user;
}
}