65 lines
2.3 KiB
C#
65 lines
2.3 KiB
C#
// src/Webshop.Application/Services/Admin/AdminUserService.cs
|
|
using Microsoft.AspNetCore.Identity;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Threading.Tasks;
|
|
using Webshop.Application.DTOs.Users;
|
|
using Webshop.Domain.Entities; // Wichtiges using für ApplicationUser
|
|
|
|
namespace Webshop.Application.Services.Admin
|
|
{
|
|
public class AdminUserService : IAdminUserService
|
|
{
|
|
// Wir verwenden nun den UserManager mit unserer neuen ApplicationUser-Klasse
|
|
private readonly UserManager<ApplicationUser> _userManager;
|
|
|
|
public AdminUserService(UserManager<ApplicationUser> userManager)
|
|
{
|
|
_userManager = userManager;
|
|
}
|
|
|
|
public async Task<IEnumerable<UserDto>> GetAllUsersAsync()
|
|
{
|
|
// Alle Benutzer aus der Datenbank laden
|
|
var users = await _userManager.Users.ToListAsync();
|
|
var userDtos = new List<UserDto>();
|
|
|
|
// Für jeden Benutzer ein DTO erstellen und die Daten mappen
|
|
foreach (var user in users)
|
|
{
|
|
userDtos.Add(new UserDto
|
|
{
|
|
Id = user.Id,
|
|
Email = user.Email ?? string.Empty,
|
|
UserName = user.UserName ?? string.Empty,
|
|
CreatedDate = user.CreatedDate, // Dieses Feld ist jetzt verfügbar!
|
|
EmailConfirmed = user.EmailConfirmed,
|
|
Roles = (await _userManager.GetRolesAsync(user)).ToList() // Rollen des Benutzers abrufen
|
|
});
|
|
}
|
|
|
|
return userDtos;
|
|
}
|
|
|
|
public async Task<UserDto?> GetUserByIdAsync(string userId)
|
|
{
|
|
var user = await _userManager.FindByIdAsync(userId);
|
|
if (user == null)
|
|
{
|
|
return null;
|
|
}
|
|
|
|
// Den gefundenen Benutzer in ein DTO umwandeln
|
|
return new UserDto
|
|
{
|
|
Id = user.Id,
|
|
Email = user.Email ?? string.Empty,
|
|
UserName = user.UserName ?? string.Empty,
|
|
CreatedDate = user.CreatedDate,
|
|
EmailConfirmed = user.EmailConfirmed,
|
|
Roles = (await _userManager.GetRolesAsync(user)).ToList()
|
|
};
|
|
}
|
|
}
|
|
} |