bugs
This commit is contained in:
@@ -1,12 +1,61 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
// src/Webshop.Application/Services/Admin/AdminUserService.cs
|
||||
using Microsoft.AspNetCore.Identity;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Webshop.Application.DTOs.Users; // UserDto
|
||||
using System.Collections.Generic; // Sicherstellen, dass für IEnumerable vorhanden
|
||||
|
||||
namespace Webshop.Application.Services.Admin
|
||||
{
|
||||
public class AdminUserService
|
||||
{
|
||||
private readonly UserManager<IdentityUser> _userManager;
|
||||
|
||||
public AdminUserService(UserManager<IdentityUser> userManager)
|
||||
{
|
||||
_userManager = userManager;
|
||||
}
|
||||
|
||||
public async Task<IEnumerable<UserDto>> GetAllUsersAsync()
|
||||
{
|
||||
var users = await _userManager.Users.ToListAsync();
|
||||
|
||||
var userDtos = new List<UserDto>();
|
||||
foreach (var user in users)
|
||||
{
|
||||
var roles = await _userManager.GetRolesAsync(user);
|
||||
userDtos.Add(new UserDto
|
||||
{
|
||||
Id = user.Id,
|
||||
Email = user.Email ?? string.Empty,
|
||||
UserName = user.UserName ?? string.Empty,
|
||||
Roles = roles.ToList(),
|
||||
// LockoutEnd kann als Näherung für CreatedDate verwendet werden,
|
||||
// da IdentityUser keine direkte CreatedDate-Eigenschaft hat.
|
||||
// Ideal wäre es, ein CustomUser-Modell zu verwenden, das von IdentityUser erbt.
|
||||
CreatedDate = user.LockoutEnd ?? DateTimeOffset.MinValue,
|
||||
EmailConfirmed = user.EmailConfirmed
|
||||
});
|
||||
}
|
||||
return userDtos;
|
||||
}
|
||||
|
||||
public async Task<UserDto?> GetUserByIdAsync(string userId)
|
||||
{
|
||||
var user = await _userManager.FindByIdAsync(userId);
|
||||
if (user == null) return null;
|
||||
|
||||
var roles = await _userManager.GetRolesAsync(user);
|
||||
return new UserDto
|
||||
{
|
||||
Id = user.Id,
|
||||
Email = user.Email ?? string.Empty,
|
||||
UserName = user.UserName ?? string.Empty,
|
||||
Roles = roles.ToList(),
|
||||
CreatedDate = user.LockoutEnd ?? DateTimeOffset.MinValue,
|
||||
EmailConfirmed = user.EmailConfirmed
|
||||
};
|
||||
}
|
||||
|
||||
// TODO: Methoden zum Aktualisieren von Rollen, Löschen von Benutzern etc.
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user