Userverwaltung improved
This commit is contained in:
+5
-1
@@ -4,7 +4,7 @@ using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace timetracker.Data;
|
||||
|
||||
public class AuthService(IDbContextFactory<TimetrackerDbContext> factory)
|
||||
public class AuthService(IDbContextFactory<TimetrackerDbContext> factory, UserNotificationService notifier)
|
||||
{
|
||||
public async Task<User?> LoginAsync(string username, string password)
|
||||
{
|
||||
@@ -31,6 +31,8 @@ public class AuthService(IDbContextFactory<TimetrackerDbContext> factory)
|
||||
{
|
||||
db.Users.Remove(user);
|
||||
await db.SaveChangesAsync();
|
||||
await notifier.NotifyUserDeletedAsync(userId);
|
||||
await notifier.NotifyUsersChangedAsync();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -48,6 +50,7 @@ public class AuthService(IDbContextFactory<TimetrackerDbContext> factory)
|
||||
|
||||
user.Username = newUsername;
|
||||
await db.SaveChangesAsync();
|
||||
await notifier.NotifyUsersChangedAsync();
|
||||
return null;
|
||||
}
|
||||
|
||||
@@ -65,6 +68,7 @@ public class AuthService(IDbContextFactory<TimetrackerDbContext> factory)
|
||||
var user = new User { Username = username, PasswordHash = hash, PasswordSalt = salt };
|
||||
db.Users.Add(user);
|
||||
await db.SaveChangesAsync();
|
||||
await notifier.NotifyUsersChangedAsync();
|
||||
return (user, null);
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,19 @@
|
||||
namespace timetracker.Data;
|
||||
|
||||
public class UserNotificationService
|
||||
{
|
||||
public event Func<Task>? OnUsersChanged;
|
||||
public event Func<int, Task>? OnUserDeleted;
|
||||
|
||||
public async Task NotifyUsersChangedAsync()
|
||||
{
|
||||
if (OnUsersChanged != null)
|
||||
await OnUsersChanged.Invoke();
|
||||
}
|
||||
|
||||
public async Task NotifyUserDeletedAsync(int userId)
|
||||
{
|
||||
if (OnUserDeleted != null)
|
||||
await OnUserDeleted.Invoke(userId);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user